diff --git a/react/src/pages/Search.js b/react/src/pages/Search.js new file mode 100644 index 0000000..fc13c82 --- /dev/null +++ b/react/src/pages/Search.js @@ -0,0 +1,111 @@ +import React, { useState } from "react"; +import { Link } from "@reach/router"; +import styled from "styled-components"; + +const Page = styled.div` + text-align: center; +`; + +const SearchBox = styled.div` + input { + height: 50px; + padding: 10px; + border-radius: 25px; + width: 800px; + outline-width: 0; + display: inline-block; + } + + button { + height: 50px; + width: 150px; + background: #ffeb3b; + border: none; + color: #000; + border-radius: 25px; + outline: 0; + cursor: pointer; + } + + button:hover, + .form-box button:active { + background: #43a047; + } +`; + +const List = styled.ul` + list-style-type: none; + display: flex; + flex-direction: column; + align-items: flex-start; + gap: 12px; + margin-bottom: 50px; +`; + +const MovieImg = styled.img` + height: 250px; +`; + +function Search() { + const [search, setSearch] = useState(""); + const [results, setResults] = useState([]); + const [error, setError] = useState(""); + + const searchMovies = async e => { + e.preventDefault(); + + if (!search) return; + + const Token = window.localStorage.getItem("Token"); + + try { + const response = await fetch( + "https://mkr.thefeathers.in/search/" + search, + { + method: "GET", + headers: new Headers({ Authentication: Token }), + }, + ); + + const data = await response.json(); + if (!data.success || !data.results) { + return setError("Error occured. " + data.error || ""); + } else { + setError(""); + setResults(data.results); + } + } catch (e) { + setError(e.message); + } + }; + + return ( + + {error ?

{error}

: ""} + + setSearch(e.target.value)} + placeholder="Search movies" + /> + + + + {results.map(result => { + return ( +
  • + + +

    {result.title}

    + +
  • + ); + })} +
    +
    + ); +} + +export default Search;