-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathCollectionModal.jsx
More file actions
357 lines (342 loc) · 12.5 KB
/
Copy pathCollectionModal.jsx
File metadata and controls
357 lines (342 loc) · 12.5 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import React, { useContext, useEffect, useState } from 'react';
import Input, { Label, TextArea } from '../UI/Input/Input';
import Loader from '../UI/Loader/Loader';
import Modal from '../UI/Modal/Modal';
import { useDispatch } from 'react-redux';
import cancelIcon from '../../assets/cancel.svg';
import CancelWhite from '../../assets/darkMode/cancelWhite.svg';
import IconButton from '../UI/IconButton/IconButton';
import Button from '../UI/Button/Button';
import {
createCollection,
updateCollection,
} from '../../api-services/collectionService';
import { addCollection } from '../../store/Slices/collection.slice';
import { updateCollectionData } from '../../store/Slices/bookmarks.slice';
import { switchMode } from '../../hooks/switchMode';
import Multiselect from 'multiselect-react-dropdown';
export const CollectionModal = ({
isOpen,
modalCloseHandler,
originalCollectionData = {}, // it will be only availble if we edit a collection
isEditing,
collectionId = null, // it will be only availble if we edit a collection
}) => {
const [isLoading, setIsLoding] = React.useState(false);
const [collectionData, setCollectionData] = useState({
title: '',
description: '',
tags: [],
image: null,
isPublic: false,
});
const dispatch = useDispatch();
useEffect(() => {
// If we are editing the data then only we need to set the data as default collection data
if (isEditing) {
setCollectionData({ ...originalCollectionData, image: null });
}
}, [isOpen]);
// Privacy Handler
const handleSwitchPrivacy = privacy => {
setCollectionData(prevData => ({ ...prevData, isPublic: privacy }));
};
// Collection title, desc handler
const onChangeHandler = e => {
setCollectionData(prevCollectionData => ({
...prevCollectionData,
[e.target.name]: e.target.value,
}));
};
const onFileUploadHandler = e => {
setCollectionData(prevData => ({
...prevData,
image: e.target.files[0],
}));
};
// Data validation
const isValidName =
collectionData.title?.length >= 3 && collectionData.title.length <= 40;
const isValidDescription = collectionData.description?.length <= 240;
const isValidTags = collectionData.tags?.length <= 3;
// File max size
const MAXED_ALLOWED_SIZE = 3 * 1024 * 1024;
// if no file is selected that means iamge is null so it will be always true as image is not mandatory data
const isValidFileSize = !collectionData.image
? true
: collectionData.image.size <= MAXED_ALLOWED_SIZE;
const isCorrectData =
isValidName && isValidDescription && isValidTags && isValidFileSize;
// it will be checked only when we are gonna edit the collection otherwise default value will be false
const sameObjectChecker = () => {
return (
originalCollectionData.title === collectionData.title &&
originalCollectionData.description === collectionData.description &&
originalCollectionData.tags?.map(
tag => collectionData.tags?.findIndex(tItem => tItem == tag) >= 0
) &&
originalCollectionData.isPublic === collectionData.isPublic &&
!collectionData.image
);
};
const isSameData = isEditing ? sameObjectChecker() : false;
const onSubmit = async () => {
// If the data is not correct form will be not submitted
if (!isCorrectData || isSameData) {
return;
}
setIsLoding(true);
try {
// Preparing form data
const { title, description, tags, image, isPublic } = collectionData;
const collectionFormData = new FormData();
collectionFormData.append('title', title);
collectionFormData.append('description', description);
collectionFormData.append('isPublic', isPublic);
image && collectionFormData.append('image', image);
tags?.length > 0 && collectionFormData.append('tags', tags);
// As the model is taking care of both edit and new collection addition
// that is why we need to send the request to different request-> according to that condition
let res = {};
if (isEditing) {
res = await updateCollection(collectionId, collectionFormData);
dispatch(updateCollectionData({ updatedCollection: res.data.data }));
} else {
res = await createCollection(collectionFormData);
dispatch(addCollection({ collection: res.data.data }));
}
resetDataAndClose();
} catch (e) {
console.log(e);
setIsLoding(false);
}
};
// Before closing the modal we should reset all the data and then close if it is not in Editing mode
// this function will colse the modal, before that it will reset all the state
const resetDataAndClose = () => {
setCollectionData({
title: '',
description: '',
tags: [],
image: null,
isPublic: false,
});
setIsLoding(false);
modalCloseHandler();
};
// getting current selected mode
const { selectedMode } = useContext(switchMode);
return (
<Modal isOpen={isOpen} onClose={modalCloseHandler}>
<div className="flex flex-col items-center justify-between h-full gap-3 px-3">
{/* Heading */}
<div className="flex justify-between w-full">
<h1
className={`text-start font-medium text-[20px] ${
selectedMode === 'light' ? 'text-black' : 'text-neutral-50'
} `}
>
{isEditing ? 'Edit Collection' : 'Create Collection'}
</h1>
<IconButton onClick={resetDataAndClose}>
{selectedMode === 'light' ? (
<img src={cancelIcon} name="cancel" />
) : (
<img src={CancelWhite} name="cancel" />
)}
</IconButton>
</div>
<hr
className={`w-[97%] border ${
selectedMode === 'light'
? 'border-neutral-300'
: 'border-dark-secondary'
} `}
/>
{/* Data */}
<div className="flex flex-col items-center justify-center w-full gap-3">
{/* Collection Name Input */}
<div className="w-[100%]">
<div className="flex items-center justify-between w-full">
<Label name="Collection Title" htmlFor="title" />
<small
className={`text-xs ${
selectedMode === 'light' ? '' : 'text-neutral-300'
} `}
>
<span
className={`${
collectionData.title?.length > 40 ? 'text-error-500' : ''
}`}
>
{collectionData.title?.length}
</span>
/{40}
</small>
</div>
<Input
type="text"
name="title"
variant={selectedMode === 'light' ? '' : 'darkDefault'}
onChange={onChangeHandler}
value={collectionData.title}
/>
</div>
{/* Collection Description Input */}
<div className="w-full">
<div className="flex items-center justify-between w-full">
<Label name="Description" htmlFor="description" />
<small
className={`text-xs ${
selectedMode === 'light' ? '' : 'text-neutral-300'
} `}
>
<span
className={`${
collectionData.description?.length > 240
? 'text-error-500'
: ''
}`}
>
{collectionData.description?.length}
</span>
/{240}
</small>
</div>
<TextArea
onChange={onChangeHandler}
name="description"
value={collectionData.description}
/>
</div>
{/* Collection Tag */}
<div className="w-full">
<Label name="Tags" htmlFor="tags" />
<Multiselect
options={[
{ name: 'Design', id: 1 },
{ name: 'Product', id: 2 },
{ name: 'Tech', id: 3 },
{ name: 'Animation', id: 4 },
{ name: 'Reading list', id: 5 },
{ name: 'AI', id: 6 },
{ name: 'Web development', id: 7 },
{ name: 'Twitter threads', id: 8 },
]}
selectedValues={this.state.selectedValues}
onSelect={this.onSelect}
onRemove={this.onRemove}
displayValue="name"
showCheckbox
maximumSelectionLength={3}
onChange={onChangeHandler}
/>
</div>
{/* Collection Thumbnail Input */}
<div className="w-full">
<Label name="Upload Thumnail" htmlFor="image" />
<Input
type={'file'}
name="image"
variant={selectedMode === 'light' ? 'file' : 'darkFile'}
placeholder="Upload image"
onChange={onFileUploadHandler}
accept="image/*"
/>
{!isValidFileSize && (
<small className="text-xs text-error-500">
image should be at most 3MB
</small>
)}
</div>
{/* Collection Privacy Input */}
<div className="flex items-center justify-between w-full">
<p
className={`${
selectedMode === 'light'
? 'text-neutral-700'
: 'text-neutral-50'
} `}
>
Select Privacy
</p>
{/* Switch */}
<div
className={`flex relative items-start justify-center gap-1.5 p-1 w-40 sm:w-40 h-[32px] ${
selectedMode === 'light'
? 'border-neutral-300 bg-neutral-300'
: 'border-dark-border bg-dark-secondary'
} border-2 rounded-[100px]`}
>
<div
className={`relative z-10 rounded w-1/2 h-full cursor-pointer transition-all duration-300 py-1.5 px-2.5 flex items-center justify-center ${
selectedMode === 'light'
? !collectionData.isPublic
? 'text-white'
: 'text-black'
: 'text-white'
} text-sm sm:text-base font-normal`}
onClick={() => handleSwitchPrivacy(false)}
>
<span>Private</span>
</div>
<div
className={`relative z-10 rounded text-sm sm:text-base w-1/2 h-full cursor-pointer transition-all duration-300 py-1.5 px-2.5 flex items-center justify-center ${
selectedMode === 'light'
? collectionData.isPublic
? 'text-white'
: 'text-black'
: 'text-white'
} font-normal`}
onClick={() => handleSwitchPrivacy(true)}
>
<span>Public</span>
</div>
<div
className={`absolute w-[49%] h-[85%] transition-transform duration-200 top-1/2 -translate-y-1/2 left-0 rounded-[102px] z-[1] bg-primary-500 ${
!collectionData.isPublic
? 'translate-x-[5%]'
: 'translate-x-[100%]'
}`}
></div>
</div>
</div>
<div className="text-neutral-600 w-full text-[0.8rem]">
<span className="text-error-500">Note: </span>Public Collections
will be featured on the explore page
</div>
</div>
{/* Actions */}
<div
className={`flex w-full sm:justify-between justify-evenly items-center ${
isEditing && 'gap-1'
}`}
>
<Button
variant={'primary'}
disabled={!isCorrectData || isSameData} // button will be disabled untli all required data is correct
onClick={onSubmit}
isLoading={isLoading}
>
{isLoading ? (
<Loader />
) : (
<span>{isEditing ? 'Save' : 'Create'}</span>
)}
</Button>
{isEditing && (
<Button
variant={
selectedMode === 'light' ? 'secondaryOutline' : 'darkOutlined'
}
onClick={resetDataAndClose}
>
<span>Cancel</span>
</Button>
)}
</div>
</div>
</Modal>
);
};
export default CollectionModal;