-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathAddToCollectionSketchList.jsx
92 lines (78 loc) · 2.59 KB
/
AddToCollectionSketchList.jsx
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
import PropTypes from 'prop-types';
import React, { useEffect, useState } from 'react';
import { Helmet } from 'react-helmet';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { addToCollection, removeFromCollection } from '../actions/collections';
import { getProjects } from '../actions/projects';
import getSortedSketches from '../selectors/projects';
import Loader from '../../App/components/loader';
import QuickAddList from './QuickAddList';
import {
CollectionAddSketchWrapper,
QuickAddWrapper
} from './AddToCollectionList';
const AddToCollectionSketchList = ({ collection }) => {
const { t } = useTranslation();
const dispatch = useDispatch();
const username = useSelector((state) => state.user.username);
const sketches = useSelector(getSortedSketches);
// TODO: improve loading state
const loading = useSelector((state) => state.loading);
const [hasLoadedData, setHasLoadedData] = useState(false);
const showLoader = loading && !hasLoadedData;
useEffect(() => {
dispatch(getProjects(username)).then(() => setHasLoadedData(true));
}, [dispatch, username]);
const handleCollectionAdd = (sketch) => {
dispatch(addToCollection(collection.id, sketch.id));
};
const handleCollectionRemove = (sketch) => {
dispatch(removeFromCollection(collection.id, sketch.id));
};
const sketchesWithAddedStatus = sketches.map((sketch) => ({
...sketch,
url: `/${username}/sketches/${sketch.id}`,
isAdded: collection.items.some(
(item) => item.projectId === sketch.id && !item.isDeleted
)
}));
const getContent = () => {
if (showLoader) {
return <Loader />;
} else if (sketches.length === 0) {
// TODO: shouldn't it be NoSketches? -Linda
return t('AddToCollectionSketchList.NoCollections');
}
return (
<QuickAddList
items={sketchesWithAddedStatus}
onAdd={handleCollectionAdd}
onRemove={handleCollectionRemove}
/>
);
};
return (
<CollectionAddSketchWrapper>
<QuickAddWrapper>
<Helmet>
<title>{t('AddToCollectionSketchList.Title')}</title>
</Helmet>
{getContent()}
</QuickAddWrapper>
</CollectionAddSketchWrapper>
);
};
AddToCollectionSketchList.propTypes = {
collection: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
items: PropTypes.arrayOf(
PropTypes.shape({
projectId: PropTypes.string.isRequired,
isDeleted: PropTypes.bool
})
)
}).isRequired
};
export default AddToCollectionSketchList;