1
1
"use client" ;
2
2
3
- import { useCallback , useEffect , useMemo , useState } from "react" ;
4
- import { useSearchParams } from "next/navigation" ;
3
+ import { useCallback , useEffect , useMemo } from "react" ;
5
4
import { Box , Grid } from "@mui/material" ;
6
5
import { useCategories } from "@/providers" ;
7
- import { getCategoryIdsFromLabels } from "@/helpers" ;
8
- import { CATEGORY_TYPE } from "@/types" ;
9
- import { useGetCategories , useGetLabels } from "@/api" ;
6
+ import { useGetCategories } from "@/api" ;
10
7
import { CategoryCard } from "./CategoryCard" ;
11
8
import { CategoryCardSkeleton } from "./CategoryCardSkeleton" ;
12
9
import { LoadingSpinner } from "../LoadingSpinner" ;
13
10
import { useInView } from "react-intersection-observer" ;
11
+ import { isCategoryLabelsEdited } from "@/helpers" ;
14
12
15
13
export const CategoriesList = ( ) => {
16
14
const {
@@ -25,40 +23,18 @@ export const CategoriesList = () => {
25
23
( ) => categoriesResponse ?. pages . flatMap ( ( page ) => page . data ) || [ ] ,
26
24
[ categoriesResponse ]
27
25
) ;
28
- const { data : labels } = useGetLabels ( { labelType : CATEGORY_TYPE } ) ;
29
26
const { currentCategories, setCurrentCategories } = useCategories ( ) ;
30
- const searchParams = useSearchParams ( ) ;
31
- const labelIds = searchParams . getAll ( "label" ) ;
32
- const [ filteredCategories , setFilteredCategories ] = useState ( categories ) ;
33
27
34
28
useEffect ( ( ) => {
35
- // Initial categories filtering when label ids are in search params
36
- if ( labels ?. length && categories . length && labelIds . length ) {
37
- const categoryIds = getCategoryIdsFromLabels ( { labelIds, labels } ) ;
38
-
39
- const filteredCategories = categories . filter ( ( category ) =>
40
- categoryIds . includes ( category . id )
41
- ) ;
42
-
43
- setFilteredCategories ( filteredCategories ) ;
44
- setCurrentCategories ( filteredCategories ) ;
45
-
46
- return ;
47
- }
48
-
49
- // Initial set of categories without filters
29
+ // Initial set of categories
50
30
if ( ( ! categories . length && currentCategories . length ) || categories . length ) {
51
31
setCurrentCategories ( categories ) ;
52
32
}
53
-
54
- //eslint-disable-next-line
55
- } , [ labels , categories , setCurrentCategories , searchParams ] ) ;
33
+ } , [ categories , setCurrentCategories ] ) ;
56
34
57
35
const onFinishCreatingCategory = useCallback ( ( ) => {
58
- labelIds . length
59
- ? setCurrentCategories ( filteredCategories )
60
- : setCurrentCategories ( categories ) ;
61
- } , [ categories , labelIds , filteredCategories , setCurrentCategories ] ) ;
36
+ setCurrentCategories ( categories ) ;
37
+ } , [ categories , setCurrentCategories ] ) ;
62
38
63
39
const { ref, inView } = useInView ( {
64
40
triggerOnce : false ,
@@ -85,6 +61,11 @@ export const CategoriesList = () => {
85
61
onFinishCreatingCategory = { onFinishCreatingCategory }
86
62
categoryId = { category . id }
87
63
labels = { category . labels || [ ] }
64
+ isCategoryLabelsEdited = { isCategoryLabelsEdited ( {
65
+ categories,
66
+ categoryId : category . id ,
67
+ newLabels : category . labels || [ ] ,
68
+ } ) }
88
69
/>
89
70
</ Grid >
90
71
) ) }
0 commit comments