@@ -4,9 +4,9 @@ import { Helmet } from 'react-helmet';
44import { useDispatch , useSelector } from 'react-redux' ;
55import { useTranslation } from 'react-i18next' ;
66import { addToCollection , removeFromCollection } from '../actions/collections' ;
7- import { getProjects } from '../actions/projects' ;
8- import getSortedSketches from '../selectors/projects' ;
7+ import { getProjectsForCollectionList } from '../actions/projects' ;
98import { Loader } from '../../App/components/Loader' ;
9+ import Pagination from './Pagination' ;
1010import QuickAddList from './QuickAddList' ;
1111import {
1212 CollectionAddSketchWrapper ,
@@ -20,16 +20,39 @@ const AddToCollectionSketchList = ({ collection }) => {
2020
2121 const username = useSelector ( ( state ) => state . user . username ) ;
2222
23- const sketches = useSelector ( getSortedSketches ) ;
23+ const sketches = useSelector (
24+ ( state ) => state . collectionsListProjects . projects
25+ ) ;
26+
27+ const paginationMeta = useSelector (
28+ ( state ) => state . collectionsListProjects . metadata
29+ ) ;
30+
31+ const q = useSelector ( ( state ) => state . search . sketchSearchTerm ) ;
32+
33+ const hasSketches = ( ) => sketches ?. length > 0 ;
34+
35+ const [ page , setPage ] = useState ( 1 ) ;
36+ const limit = 10 ;
2437
2538 // TODO: improve loading state
2639 const loading = useSelector ( ( state ) => state . loading ) ;
2740 const [ hasLoadedData , setHasLoadedData ] = useState ( false ) ;
2841 const showLoader = loading && ! hasLoadedData ;
2942
3043 useEffect ( ( ) => {
31- dispatch ( getProjects ( username ) ) . then ( ( ) => setHasLoadedData ( true ) ) ;
32- } , [ dispatch , username ] ) ;
44+ dispatch (
45+ getProjectsForCollectionList ( username , {
46+ page,
47+ limit,
48+ q
49+ } )
50+ ) . finally ( ( ) => setHasLoadedData ( true ) ) ;
51+ } , [ dispatch , username , page , q ] ) ;
52+
53+ useEffect ( ( ) => {
54+ setPage ( 1 ) ;
55+ } , [ q ] ) ;
3356
3457 const handleCollectionAdd = ( sketch ) => {
3558 dispatch ( addToCollection ( collection . id , sketch . id ) ) ;
@@ -43,7 +66,8 @@ const AddToCollectionSketchList = ({ collection }) => {
4366 ...sketch ,
4467 url : `/${ username } /sketches/${ sketch . id } ` ,
4568 isAdded : collection . items . some (
46- ( item ) => item . projectId === sketch . id && ! item . isDeleted
69+ ( item ) =>
70+ ( item . projectId || item . project ?. id ) === sketch . id && ! item . isDeleted
4771 )
4872 } ) ) ;
4973
@@ -55,11 +79,23 @@ const AddToCollectionSketchList = ({ collection }) => {
5579 return t ( 'AddToCollectionSketchList.NoCollections' ) ;
5680 }
5781 return (
58- < QuickAddList
59- items = { sketchesWithAddedStatus }
60- onAdd = { handleCollectionAdd }
61- onRemove = { handleCollectionRemove }
62- />
82+ < >
83+ < QuickAddList
84+ items = { sketchesWithAddedStatus }
85+ onAdd = { handleCollectionAdd }
86+ onRemove = { handleCollectionRemove }
87+ />
88+ { hasSketches ( ) && (
89+ < Pagination
90+ page = { page }
91+ totalPages = { paginationMeta . totalPages }
92+ onPageChange = { setPage }
93+ limit = { limit }
94+ totalSketches = { paginationMeta . totalProjects }
95+ isOverlay
96+ />
97+ ) }
98+ </ >
6399 ) ;
64100 } ;
65101
@@ -81,7 +117,10 @@ AddToCollectionSketchList.propTypes = {
81117 name : PropTypes . string . isRequired ,
82118 items : PropTypes . arrayOf (
83119 PropTypes . shape ( {
84- projectId : PropTypes . string . isRequired ,
120+ projectId : PropTypes . string ,
121+ project : PropTypes . shape ( {
122+ id : PropTypes . string
123+ } ) ,
85124 isDeleted : PropTypes . bool
86125 } )
87126 )
0 commit comments