From a4594392290a573385807c8929dbde9e4d0ea782 Mon Sep 17 00:00:00 2001 From: Patrik Zita Date: Thu, 11 Jan 2024 07:24:43 +0100 Subject: [PATCH] feat: remove fetching from page pagination-with-filters --- src/pages/offset-pagination/index.tsx | 50 ++++-- src/pages/pagination-with-filters/index.tsx | 161 +++++++++++++------- 2 files changed, 136 insertions(+), 75 deletions(-) diff --git a/src/pages/offset-pagination/index.tsx b/src/pages/offset-pagination/index.tsx index 6b8f95f..f6c5a84 100644 --- a/src/pages/offset-pagination/index.tsx +++ b/src/pages/offset-pagination/index.tsx @@ -1,3 +1,4 @@ +import ExampleExplanation from "@/components/ExampleExplanation"; import { Button } from "@/components/ui/button"; import { GetDealsOffsetBasedDocument, @@ -5,8 +6,14 @@ import { } from "@/generated/graphql"; import { addApolloState, initializeApollo } from "@/utils/apolloClient"; +const EXPLANATION = { + title: "Offset Pagination - single field query", + description: + "Nejjednodušší způsob offset-based paginace, pro sprváný cachování je potřeba nastavit typePolicy v InMemoryCache.", +}; + export default function BasedPagitanionPage() { - const { data, fetchMore, loading } = useGetDealsOffsetBasedQuery({ + const { data, fetchMore, loading, error } = useGetDealsOffsetBasedQuery({ variables: { limit: 3, offset: 0, @@ -23,26 +30,38 @@ export default function BasedPagitanionPage() { }); }; - console.log(data); + if (data?.dealsOffsetBased && data?.dealsOffsetBased.length !== 0) { + return ( +
+ + +
+ {data.dealsOffsetBased.map((deal) => ( +
+

{deal.title}

+
+ ))} +
+ +
+ ); + } + if (loading) { return
loading
; } - return ( -
-
- {data.dealsOffsetBased.map((deal) => ( -
-

{deal.title}

-
- ))} -
- -
- ); + if (error) { + return
Error
; + } + + return
Nic tu není
; } -/* export async function getServerSideProps() { +export async function getServerSideProps() { const apolloClient = initializeApollo(); await apolloClient.query({ @@ -57,4 +76,3 @@ export default function BasedPagitanionPage() { props: {}, }); } - */ diff --git a/src/pages/pagination-with-filters/index.tsx b/src/pages/pagination-with-filters/index.tsx index 17e9901..53f8539 100644 --- a/src/pages/pagination-with-filters/index.tsx +++ b/src/pages/pagination-with-filters/index.tsx @@ -1,74 +1,117 @@ +import ExampleExplanation from "@/components/ExampleExplanation"; import { Button } from "@/components/ui/button"; -import { useGetDealsOffsetWithFilterQuery } from "@/generated/graphql"; +import { + GetDealsOffsetWithFilterDocument, + useGetDealsOffsetWithFilterQuery, +} from "@/generated/graphql"; import { cn } from "@/lib/utils"; +import { addApolloState, initializeApollo } from "@/utils/apolloClient"; +import { useState } from "react"; + +const EXPLANATION = { + title: "Offset Pagination - s filtry", + description: "Trošku náročnější koncept paginace s filtry", +}; export default function BasedPaginationWithFiltersPage() { - const { data, loading, refetch } = useGetDealsOffsetWithFilterQuery({ + const [filter, setFilter] = useState(); + + const { data, loading, error, fetchMore } = useGetDealsOffsetWithFilterQuery({ variables: { limit: 3, offset: 0, + isActive: filter, }, - fetchPolicy: "cache-first", }); + const loadMore = () => { + // TODO: merge cache objects + const currentLength = data.dealsOffsetWithFilter.deals.length || 0; + fetchMore({ + variables: { + offset: currentLength, + limit: 3, + }, + }); + }; + + if (data?.dealsOffsetWithFilter?.deals) { + return ( +
+ +
+

Poznámky

+
    +
  • + - Mění se zde data, díky tomu že se změní state filters a celá + komponenta se re-renderuje +
  • +
  • + - Nepoužívá se zde refetch pro změnu filtrů, protože nebere v + potaz cache. +
  • +
+
+
+
+ + + +
+
+ {data.dealsOffsetWithFilter.deals && + data.dealsOffsetWithFilter.deals.map((deal) => ( +
+

+ {deal.title} + + ({deal.isActive ? "true" : "false"}) + +

+
+ ))} +
+
+ +
+
+ ); + } + if (loading) { return
loading
; } - return ( -
-
- {data.dealsOffsetWithFilter.deals && - data.dealsOffsetWithFilter.deals.map((deal) => ( -
-

- {deal.title}{" "} - - ({deal.isActive ? "true" : "false"}) - -

-
- ))} -
-
- - - -
-
- ); + if (error) { + return
Error
; + } + + return
Asi se sem nedostanu
; +} + +export async function getServerSideProps() { + const apolloClient = initializeApollo(); + + await apolloClient.query({ + query: GetDealsOffsetWithFilterDocument, + variables: { + limit: 3, + offset: 0, + }, + }); + + return addApolloState(apolloClient, { + props: {}, + }); }