forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddToCollectionList.jsx
93 lines (78 loc) · 2.52 KB
/
AddToCollectionList.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
93
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,
getCollections,
removeFromCollection
} from '../actions/collections';
import getSortedCollections from '../selectors/collections';
import QuickAddList from './QuickAddList';
import { remSize } from '../../../theme';
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(getSortedCollections);
// TODO: improve loading state
const loading = useSelector((state) => state.loading);
const [hasLoadedData, setHasLoadedData] = useState(false);
const showLoader = loading && !hasLoadedData;
useEffect(() => {
dispatch(getCollections(username)).then(() => setHasLoadedData(true));
}, [dispatch, username]);
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}
/>
);
};
return (
<CollectionAddSketchWrapper>
<QuickAddWrapper>
<Helmet>
<title>{t('AddToCollectionList.Title')}</title>
</Helmet>
{getContent()}
</QuickAddWrapper>
</CollectionAddSketchWrapper>
);
};
AddToCollectionList.propTypes = {
projectId: PropTypes.string.isRequired
};
export default AddToCollectionList;