-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathCollectionMetadata.jsx
More file actions
125 lines (108 loc) · 3.84 KB
/
CollectionMetadata.jsx
File metadata and controls
125 lines (108 loc) · 3.84 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
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch, useSelector } from 'react-redux';
import { Link } from 'react-router-dom';
import { Button } from '../../../common/Button';
import { Overlay } from '../../App/components/Overlay';
import { editCollection } from '../../IDE/actions/collections';
import AddToCollectionSketchList from '../../IDE/components/AddToCollectionSketchList';
import EditableInput from '../../IDE/components/EditableInput';
import { SketchSearchbar } from '../../IDE/components/Searchbar';
import { getCollection } from '../../IDE/selectors/collections';
import { ShareURL } from './CollectionShareButton';
function CollectionMetadata({ collectionId }) {
const { t } = useTranslation();
const dispatch = useDispatch();
const collection = useSelector((state) => getCollection(state, collectionId));
const currentUsername = useSelector((state) => state.user.username);
const [isAddingSketches, setIsAddingSketches] = useState(false);
if (!collection) {
return null;
}
const { id, name, description, items, owner } = collection;
const { username } = owner;
const isOwner = !!currentUsername && currentUsername === username;
const hostname = window.location.origin;
const handleEditCollectionName = (value) => {
if (value === name) {
return;
}
dispatch(editCollection(id, { name: value }));
};
const handleEditCollectionDescription = (value) => {
if (value === description) {
return;
}
dispatch(editCollection(id, { description: value }));
};
// TODO: Implement UI for editing slug
return (
<header
className={classNames(
'collection-metadata',
isOwner && 'collection-metadata--is-owner'
)}
>
<div className="collection-metadata__columns">
<div className="collection-metadata__column--left">
<h2 className="collection-metadata__name">
{isOwner ? (
<EditableInput
value={name}
onChange={handleEditCollectionName}
aria-label={t('Collection.NameARIA')}
validate={(value) => value !== ''}
/>
) : (
name
)}
</h2>
<p className="collection-metadata__description">
{isOwner ? (
<EditableInput
InputComponent="textarea"
value={description}
onChange={handleEditCollectionDescription}
aria-label={t('Collection.DescriptionARIA')}
emptyPlaceholder={t('Collection.DescriptionPlaceholder')}
/>
) : (
description
)}
</p>
<p className="collection-metadata__user">
{t('Collection.By')}
<Link to={`/${username}/sketches`}>{username}</Link>
</p>
<p className="collection-metadata__user">
{t('Collection.NumSketches', { count: items.length })}
</p>
</div>
<div className="collection-metadata__column--right">
<ShareURL value={`${hostname}/${username}/collections/${id}`} />
{isOwner && (
<Button onClick={() => setIsAddingSketches(true)}>
{t('Collection.AddSketch')}
</Button>
)}
</div>
</div>
{isAddingSketches && (
<Overlay
title={t('Collection.AddSketch')}
actions={<SketchSearchbar />}
closeOverlay={() => setIsAddingSketches(false)}
isFixedHeight
>
<AddToCollectionSketchList collection={collection} />
</Overlay>
)}
</header>
);
}
CollectionMetadata.propTypes = {
collectionId: PropTypes.string.isRequired
};
export default CollectionMetadata;