-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathAddToCollectionList.jsx
More file actions
110 lines (94 loc) · 3.21 KB
/
Copy pathAddToCollectionList.jsx
File metadata and controls
110 lines (94 loc) · 3.21 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
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';
import { Overlay } from '../../App/components/Overlay';
import CollectionCreate from '../../User/components/CollectionCreate';
import { Button } from '../../../common/Button';
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;
const [createCollectionVisible, setCreateCollectionVisible] = useState(false);
useEffect(() => {
dispatch(getCollections(username)).then(() => setHasLoadedData(true));
}, [dispatch, username, createCollectionVisible]);
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>
<Button onClick={() => setCreateCollectionVisible(true)}>
{t('DashboardView.CreateCollection')}
</Button>
{createCollectionVisible && (
<Overlay
title={t('DashboardView.CreateCollectionOverlay')}
closeOverlay={() => setCreateCollectionVisible(false)}
isCompactHeight
>
<CollectionCreate />
</Overlay>
)}
{getContent()}
</QuickAddWrapper>
</CollectionAddSketchWrapper>
);
};
AddToCollectionList.propTypes = {
projectId: PropTypes.string.isRequired
};
export default AddToCollectionList;