File size: 5,205 Bytes
0879a03
 
0527ba9
1a805b3
0879a03
 
 
 
 
 
0527ba9
1a805b3
0879a03
0527ba9
 
 
 
 
 
 
0879a03
0527ba9
0879a03
 
 
 
0527ba9
 
0879a03
 
 
 
 
0527ba9
 
 
0879a03
0527ba9
 
0879a03
 
 
0527ba9
 
 
 
 
 
0879a03
0527ba9
 
 
 
 
 
0879a03
 
 
 
0527ba9
 
 
 
 
 
 
 
 
0879a03
 
 
 
 
 
 
 
0527ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
1a805b3
0879a03
1a805b3
0527ba9
 
 
 
 
 
 
 
0879a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0527ba9
 
 
 
1a805b3
0879a03
1a805b3
0527ba9
0879a03
0527ba9
 
 
 
 
 
 
0879a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0527ba9
 
 
 
1a805b3
 
 
0527ba9
0879a03
0527ba9
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"use client";
import { useState, useEffect, useRef } from "react";
import { useRouter } from "next/navigation";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
  faSearch,
  faCaretLeft,
  faCaretRight,
} from "@fortawesome/free-solid-svg-icons";
import { search } from "@/api/searchApi";
import "./searchPage.css";
import MovieCard from "@/components/MovieCard";
import TvShowCard from "@/components/TvShowCard";

const SearchPage = () => {
  const [query, setQuery] = useState("");
  const [results, setResults] = useState({ films: [], tv_series: [] });
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(null);
  const [debouncedQuery, setDebouncedQuery] = useState("");
  const [loadingDelay, setLoadingDelay] = useState(null);

  const router = useRouter();
  const timeoutRef = useRef(null);
  const moviesRef = useRef(null);
  const seriesRef = useRef(null);

  useEffect(() => {
    if (timeoutRef.current) {
      clearTimeout(timeoutRef.current);
    }

    timeoutRef.current = setTimeout(() => {
      if (debouncedQuery) {
        handleSearch();
      }
    }, 200);

    return () => {
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
      }
    };
  }, [debouncedQuery]);

  const handleSearch = async () => {
    setLoading(true);
    setError(null);

    try {
      const data = await search(debouncedQuery);
      setResults(data);
    } catch (err) {
      setError("Failed to fetch search results.");
    } finally {
      if (loadingDelay) {
        clearTimeout(loadingDelay);
      }
      setLoadingDelay(setTimeout(() => setLoading(false), 800));
    }
  };

  const handleChange = (e) => {
    setQuery(e.target.value);
    setDebouncedQuery(e.target.value);
  };

  const handleItemClick = (path) => {
    router.push(path);
  };

  const scroll = (ref, direction) => {
    if (ref.current) {
      const scrollAmount = direction === "left" ? -400 : 400;
      ref.current.scrollBy({ left: scrollAmount, behavior: "smooth" });
    }
  };

  return (
    <div className="search-page">
      <div className="search-container">
        <input
          type="text"
          value={query}
          onChange={handleChange}
          placeholder="Search for films or TV shows..."
          className="search-input"
        />
        {loading && (
          <div className="loading-indicator">
            <div className="search-icon">
              <FontAwesomeIcon icon={faSearch} size="xl" />
            </div>
            <div className="spinner"></div>
          </div>
        )}
      </div>
      {error && <div className="error-message">{error}</div>}
      <div className="results-container">
        {results.films.length > 0 && (
          <div className="results-section">
            <div className="result-scroll">
              <h2>Films</h2>
              <div className="scroll-controls">
                <button
                  onClick={() => scroll(moviesRef, "left")}
                  className="scroll-button"
                >
                  <FontAwesomeIcon icon={faCaretLeft} size="2xl" />
                </button>
                <button
                  onClick={() => scroll(moviesRef, "right")}
                  className="scroll-button"
                >
                  <FontAwesomeIcon icon={faCaretRight} size="2xl" />
                </button>
              </div>
            </div>
            <ul className="results-list" ref={moviesRef}>
              {results.films.map((film, index) => (
                <li
                  key={index}
                  className="result-item"
                  onClick={() =>
                    handleItemClick(`/movie/${encodeURIComponent(film)}`)
                  }
                >
                  <MovieCard title={film} />
                </li>
              ))}
            </ul>
          </div>
        )}
        {results.tv_series.length > 0 && (
          <div className="results-section">
            <div className="result-scroll">
              <h2>TV Series</h2>
              <div className="scroll-controls">
                <button
                  onClick={() => scroll(seriesRef, "left")}
                  className="scroll-button"
                >
                  <FontAwesomeIcon icon={faCaretLeft} size="2xl" />
                </button>
                <button
                  onClick={() => scroll(seriesRef, "right")}
                  className="scroll-button"
                >
                  <FontAwesomeIcon icon={faCaretRight} size="2xl" />
                </button>
              </div>
            </div>
            <ul className="results-list" ref={seriesRef}>
              {results.tv_series.map((series, index) => (
                <li
                  key={index}
                  className="result-item"
                  onClick={() =>
                    handleItemClick(`/tvshow/${encodeURIComponent(series)}`)
                  }
                >
                  <TvShowCard title={series} />
                </li>
              ))}
            </ul>
          </div>
        )}
      </div>
    </div>
  );
};

export default SearchPage;