aboutsummaryrefslogtreecommitdiff
path: root/components/hero/searchAni.js
blob: 390165ad5e9fc33f3a38dafb72db2c34d47cdec7 (plain) (blame)
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
import React from "react";
import { useQuery, gql } from "@apollo/client";
import Link from "next/link";

const SearchAni = ({ searchQuery }) => {
  const ANIME_QUERY = gql`
    query (
      $id: Int
      $page: Int
      $perPage: Int
      $search: String
      $sort: [MediaSort]
    ) {
      Page(page: $page, perPage: $perPage) {
        pageInfo {
          total
          currentPage
          lastPage
          hasNextPage
          perPage
        }
        media(id: $id, search: $search, sort: $sort, type: ANIME) {
          id
          idMal
          title {
            romaji
            english
          }
          coverImage {
            large
          }
        }
      }
    }
  `;

  // use useQuery hook to execute query and get data
  const { loading, error, data } = useQuery(ANIME_QUERY, {
    variables: {
      search: searchQuery,
      page: 1,
      perPage: 5,
      sort: "TRENDING_DESC",
    },
  });

  // render component
  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error :(</p>;

  const { media } = data.Page;

  // const cleanDescription = description.replace(/<br>/g, '').replace(/\n/g, '  ');

  return (
    <main className="flex flex-col">
      <div className="my-10 mx-[1rem] flex flex-col gap-10 md:mx-auto md:w-full">
        {media.map((anime) => {})}
      </div>
    </main>
  );
};

export default SearchAni;