-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathCreateCollectionDialog.tsx
More file actions
97 lines (80 loc) · 2.84 KB
/
CreateCollectionDialog.tsx
File metadata and controls
97 lines (80 loc) · 2.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
import React from 'react';
import {I18n} from 'i18n';
import {useDispatch, useSelector} from 'react-redux';
import {ErrorCode} from 'shared/constants';
import type {CreateCollectionResponse} from 'shared/schema';
import type {AppDispatch} from 'store';
import DialogManager from '../../components/DialogManager/DialogManager';
import {createCollection} from '../../store/actions/collectionsStructure';
import {selectCreateCollectionIsLoading} from '../../store/selectors/collectionsStructure';
import {CollectionDialog, type CollectionDialogValues} from './CollectionDialog';
import {useCollectionEntityDialogState} from './hooks/useCollectionEntityDialogState';
const i18n = I18n.keyset('component.collections-structure');
export const DIALOG_CREATE_COLLECTION = Symbol('DIALOG_CREATE_COLLECTION');
export type OpenDialogCreateCollectionArgs = {
id: typeof DIALOG_CREATE_COLLECTION;
props: Props;
};
type Props = {
parentId: string | null;
open: boolean;
onClose: () => void;
onApply?: (result: CreateCollectionResponse | null) => void;
};
export const CreateCollectionDialog: React.FC<Props> = (props) => {
const dispatch: AppDispatch = useDispatch();
const {open, onClose} = props;
const {
values: dialogValues,
errors: dialogErrors,
handleChange: handleDialogChange,
handleError: handleDialogError,
} = useCollectionEntityDialogState({
title: '',
description: '',
});
const handleApply = async (
{title, description}: CollectionDialogValues,
dialogOnClose: () => void,
) => {
const {parentId, onApply} = props;
try {
const result = await dispatch(
createCollection(
{
title,
description,
parentId,
},
true,
),
);
if (onApply) {
onApply(result);
}
dialogOnClose();
return result;
} catch (error) {
if (error.code === ErrorCode.CollectionAlreadyExists) {
handleDialogError({title: i18n('label_collection-already-exists-error')});
}
return Promise.resolve();
}
};
const isLoading = useSelector(selectCreateCollectionIsLoading);
return (
<CollectionDialog
values={dialogValues}
errors={dialogErrors}
title={i18n('action_create-collection')}
textButtonApply={i18n('action_create')}
open={open}
isLoading={isLoading}
onChange={handleDialogChange}
onApply={handleApply}
onClose={onClose}
titleAutoFocus
/>
);
};
DialogManager.registerDialog(DIALOG_CREATE_COLLECTION, CreateCollectionDialog);