-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathAddToCollectionList.jsx
More file actions
129 lines (110 loc) · 3.43 KB
/
AddToCollectionList.jsx
File metadata and controls
129 lines (110 loc) · 3.43 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import PropTypes from 'prop-types';
import React, { useEffect, useState } from 'react';
import { Helmet } from 'react-helmet';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import styled from 'styled-components';
import { Loader } from '../../App/components/Loader';
import {
addToCollection,
getCollectionsForCollectionList,
removeFromCollection
} from '../actions/collections';
import getSortedCollections from '../selectors/collections';
import QuickAddList from './QuickAddList';
import { remSize } from '../../../theme';
import Pagination from './Pagination';
import { sketchResponse } from '../../../testData/testServerResponses';
export const CollectionAddSketchWrapper = styled.div`
width: ${remSize(600)};
max-width: 100%;
overflow: auto;
`;
export const QuickAddWrapper = styled.div`
width: ${remSize(600)};
max-width: 100%;
padding: ${remSize(24)};
height: 100%;
`;
const AddToCollectionList = ({ projectId }) => {
const { t } = useTranslation();
const dispatch = useDispatch();
const username = useSelector((state) => state.user.username);
const collections = useSelector(
(state) => state.collectionsListCollections.collections
);
const paginationMeta = useSelector(
(state) => state.collectionsListCollections.metadata
);
const q = useSelector((state) => state.search.collectionSearchTerm);
const hasCollections = () => collections?.length > 0;
const [page, setPage] = useState(1);
const limit = 10;
// TODO: improve loading state
const loading = useSelector((state) => state.loading);
const [hasLoadedData, setHasLoadedData] = useState(false);
const showLoader = loading && !hasLoadedData;
useEffect(() => {
dispatch(
getCollectionsForCollectionList(username, {
page,
limit,
q
})
).finally(() => setHasLoadedData(true));
}, [dispatch, username, page, q]);
useEffect(() => {
setPage(1);
}, [q]);
const handleCollectionAdd = (collection) => {
dispatch(addToCollection(collection.id, projectId));
};
const handleCollectionRemove = (collection) => {
dispatch(removeFromCollection(collection.id, projectId));
};
const collectionWithSketchStatus = collections.map((collection) => ({
...collection,
url: `/${collection.owner.username}/collections/${collection.id}`,
isAdded: collection.items.some((item) => item.projectId === projectId)
}));
const getContent = () => {
if (showLoader) {
return <Loader />;
} else if (collections.length === 0) {
return t('AddToCollectionList.Empty');
}
return (
<>
<QuickAddList
items={collectionWithSketchStatus}
onAdd={handleCollectionAdd}
onRemove={handleCollectionRemove}
/>
{hasCollections() && (
<Pagination
page={page}
totalPages={paginationMeta.totalPages}
onPageChage={setPage}
limit={limit}
totalCollectoins={paginationMeta.totalCollections}
isOverlay
/>
)}
</>
);
};
return (
<CollectionAddSketchWrapper>
<QuickAddWrapper>
<Helmet>
<title>{t('AddToCollectionList.Title')}</title>
</Helmet>
{getContent()}
</QuickAddWrapper>
</CollectionAddSketchWrapper>
);
};
AddToCollectionList.propTypes = {
projectId: PropTypes.string.isRequired
};
export default AddToCollectionList;