1
- import React , { FunctionComponent , useState , useCallback } from 'react'
1
+ import React , { FunctionComponent , useState } from 'react'
2
2
import dynamic from 'next/dynamic'
3
3
import { Root , HeadingWrapper , Heading , Title , TopBarFilterToggleButton , ProductsWrapper } from './Category.styled'
4
4
import { useNetworkStatus } from '~/hooks/useNetworkStatus'
@@ -10,13 +10,12 @@ import { Skeleton } from '@storystore/ui/dist/components/Skeleton'
10
10
import TopBar from '@storystore/ui/dist/components/TopBar'
11
11
import FiltersIcon from 'remixicon/icons/System/list-settings-line.svg'
12
12
import FiltersCloseIcon from 'remixicon/icons/System/list-settings-fill.svg'
13
- import { QueryResult , useQuery } from '@apollo/client'
14
- import Products , { PRODUCTS_QUERY } from '~/components/Products'
13
+ import { useQuery } from '@apollo/client'
14
+ import Products from '~/components/Products'
15
15
import Icon from '@storystore/ui/dist/components/Icon'
16
- import Sidebar from '@storystore/ui/dist/components/Sidebar'
17
- import { Filters , FilterVariables , FilterSelected } from '~/components/Filters'
18
16
import { PageSkeleton } from '~/components/Page/Page.skeleton'
19
17
import PageBuilder from '~/components/PageBuilder'
18
+ import { CATEGORY_QUERY } from '.'
20
19
21
20
const Error = dynamic ( ( ) => import ( '../Error' ) )
22
21
@@ -28,28 +27,27 @@ const TitleSkeleton = ({ ...props }) => {
28
27
)
29
28
}
30
29
31
- export const Category : FunctionComponent < QueryResult > = ( { loading, data } ) => {
32
- const [ panelOpen , setPanelOpen ] = useState ( false )
30
+ export type CategoryProps = {
31
+ id : string
32
+ }
33
+
34
+ export const Category : FunctionComponent < CategoryProps > = ( { id } ) => {
35
+ const { loading, data } = useQuery ( CATEGORY_QUERY , {
36
+ variables : { id } ,
37
+ fetchPolicy : 'cache-and-network' ,
38
+ returnPartialData : true ,
39
+ } )
33
40
34
- const [ filters , setFilters ] = useState < { selected : FilterSelected ; variables : FilterVariables } > ( { selected : { } , variables : { } } )
41
+ const [ toggleFilters , setToggleFilters ] = useState ( false )
35
42
36
- const categoryUrlSuffix = data ?. storeConfig . categoryUrlSuffix
43
+ const [ hasFiltersSelected , setHasFiltersSelected ] = useState ( false )
37
44
38
45
const page = data ?. categoryList && data . categoryList [ 0 ]
39
46
40
47
const mode = page ?. mode || 'PRODUCTS'
41
48
42
49
const online = useNetworkStatus ( )
43
50
44
- const products = useQuery ( PRODUCTS_QUERY , {
45
- variables : { filters : { category_id : { eq : page ?. id } , ...filters . variables } } ,
46
- skip : ! page || ! / P R O D U C T S / . test ( mode ) ,
47
- } )
48
-
49
- const handleOnFiltersUpdate = useCallback ( ( { selected, variables } ) => {
50
- setFilters ( { selected, variables } )
51
- } , [ ] )
52
-
53
51
if ( ! online && ! data ?. categoryList ) return < Error type = "Offline" fullScreen />
54
52
55
53
if ( ! loading && ! data ?. categoryList ) {
@@ -86,7 +84,7 @@ export const Category: FunctionComponent<QueryResult> = ({ loading, data }) => {
86
84
} ,
87
85
count,
88
86
text,
89
- href : '/' + href + categoryUrlSuffix ,
87
+ href : '/' + href + ( page . urlSuffix ?? '' ) ,
90
88
} ) ) }
91
89
/>
92
90
) }
@@ -103,26 +101,30 @@ export const Category: FunctionComponent<QueryResult> = ({ loading, data }) => {
103
101
id,
104
102
mode,
105
103
} ,
106
- href : '/' + href + categoryUrlSuffix ,
104
+ href : '/' + href + ( page . urlSuffix ?? '' ) ,
107
105
text,
108
106
} ) ) }
109
107
/>
110
108
) }
111
109
</ Heading >
112
110
</ HeadingWrapper >
113
111
114
- < TopBarFilterToggleButton onClick = { ( ) => setPanelOpen ( ! panelOpen ) } >
115
- < Icon svg = { panelOpen ? FiltersCloseIcon : FiltersIcon } aria-label = "Filters" attention = { Object . keys ( filters . selected ) . length > 0 } />
112
+ < TopBarFilterToggleButton onClick = { ( ) => setToggleFilters ( ! toggleFilters ) } >
113
+ < Icon svg = { toggleFilters ? FiltersCloseIcon : FiltersIcon } aria-label = "Filters" attention = { hasFiltersSelected } />
116
114
</ TopBarFilterToggleButton >
117
115
</ TopBar >
118
116
119
- < ProductsWrapper >
120
- < Products { ...products } loading = { loading || products . loading } />
121
- </ ProductsWrapper >
122
-
123
- < Sidebar position = "right" onClose = { ( ) => setPanelOpen ( false ) } button = { { text : 'Done' , onClick : ( ) => setPanelOpen ( false ) } } >
124
- { panelOpen && < Filters { ...products } defaultSelected = { { ...filters . selected } } onUpdate = { handleOnFiltersUpdate } /> }
125
- </ Sidebar >
117
+ { / P R O D U C T S / . test ( mode ) && (
118
+ < ProductsWrapper >
119
+ < Products
120
+ loading = { loading }
121
+ filters = { { category_id : { eq : page ?. id . toString ( ) } } }
122
+ openFilters = { toggleFilters }
123
+ onToggleFilters = { state => setToggleFilters ( state ) }
124
+ onUpdatedFilters = { values => setHasFiltersSelected ( ! ! values ) }
125
+ />
126
+ </ ProductsWrapper >
127
+ ) }
126
128
</ React . Fragment >
127
129
) }
128
130
</ Root >
0 commit comments