Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EditLabelModal.tsx « modals « labels « containers « components « packages - github.com/ProtonMail/WebClients.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12c099ae9523fb47267f0adee62d15b8d1752ff2 (plain)
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
import { useEffect, useState } from 'react';

import { c } from 'ttag';

import { Button } from '@proton/atoms';
import { checkLabelAvailability, create as createLabel, updateLabel } from '@proton/shared/lib/api/labels';
import { ACCENT_COLORS, LABEL_TYPE, ROOT_FOLDER } from '@proton/shared/lib/constants';
import { omit } from '@proton/shared/lib/helpers/object';
import { Folder } from '@proton/shared/lib/interfaces/Folder';
import { Label } from '@proton/shared/lib/interfaces/Label';
import noop from '@proton/utils/noop';
import randomIntFromInterval from '@proton/utils/randomIntFromInterval';

import {
    ModalProps,
    ModalTwo,
    ModalTwoContent,
    ModalTwoFooter,
    ModalTwoHeader,
    useFormErrors,
} from '../../../components';
import { useApi, useEventManager, useLoading, useNotifications } from '../../../hooks';
import NewLabelForm from '../NewLabelForm';

export interface LabelModel extends Pick<Folder | Label, 'Name' | 'Color' | 'Type'> {
    ID?: string;
    ParentID?: string | number;
    Notify?: number;
    Expanded?: number;
    Order?: number;
    Path?: string;
}

interface Props extends ModalProps {
    type?: 'label' | 'folder';
    label?: LabelModel;
    mode?: 'create' | 'edition' | 'checkAvailable';
    onAdd?: (label: LabelModel) => void;
    onEdit?: (label: LabelModel) => void;
    onCheckAvailable?: (label: LabelModel) => void;
    onCloseCustomAction?: () => void;
}

const prepareLabel = (label: LabelModel) => {
    if (label.ParentID === ROOT_FOLDER) {
        return omit(label, ['ParentID']);
    }
    return label;
};

const EditLabelModal = ({
    label,
    mode = 'create',
    onAdd = noop,
    onEdit = noop,
    onCheckAvailable = noop,
    type = 'label',
    onCloseCustomAction,
    ...rest
}: Props) => {
    const { call } = useEventManager();
    const { createNotification } = useNotifications();
    const api = useApi();
    const [loading, withLoading] = useLoading();
    const { validator, onFormSubmit } = useFormErrors();

    const { onClose } = rest;

    const [model, setModel] = useState<LabelModel>(
        label || {
            Name: '',
            Color: ACCENT_COLORS[randomIntFromInterval(0, ACCENT_COLORS.length - 1)],
            Type: type === 'folder' ? LABEL_TYPE.MESSAGE_FOLDER : LABEL_TYPE.MESSAGE_LABEL,
            ParentID: type === 'folder' ? ROOT_FOLDER : undefined,
            Notify: type === 'folder' ? 1 : 0,
        }
    );

    useEffect(() => {
        setModel(
            label || {
                Name: '',
                Color: ACCENT_COLORS[randomIntFromInterval(0, ACCENT_COLORS.length - 1)],
                Type: type === 'folder' ? LABEL_TYPE.MESSAGE_FOLDER : LABEL_TYPE.MESSAGE_LABEL,
                ParentID: type === 'folder' ? ROOT_FOLDER : undefined,
                Notify: type === 'folder' ? 1 : 0,
            }
        );
    }, [type]);

    const handleClose = () => {
        onCloseCustomAction?.();
        onClose?.();
    };

    const create = async (label: LabelModel) => {
        const { Label } = await api(createLabel(prepareLabel(label)));
        await call();
        createNotification({
            text: c('label/folder notification').t`${Label.Name} created`,
        });
        onAdd(Label);
        handleClose();
    };

    const update = async (label: LabelModel) => {
        if (label.ID) {
            const { Label } = await api(updateLabel(label.ID, prepareLabel(label)));
            await call();
            createNotification({
                text: c('Filter notification').t`${Label.Name} updated`,
            });
            onEdit(Label);
        }
        handleClose();
    };

    const checkIsAvailable = async (label: LabelModel) => {
        await api(checkLabelAvailability(label));
        onCheckAvailable(model);
        handleClose();
    };

    const handleSubmit = async () => {
        if (!onFormSubmit()) {
            return;
        }

        switch (mode) {
            case 'create':
                await withLoading(create(model));
                return;
            case 'edition':
                await withLoading(update(model));
                return;
            case 'checkAvailable':
                await withLoading(checkIsAvailable(model));
                return;
            default:
                return undefined;
        }
    };

    const handleChangeColor = (Color: string) => {
        setModel({
            ...model,
            Color,
        });
    };

    const handleChangeName = (value: string) => {
        setModel({
            ...model,
            Name: value,
        });
    };

    const handleChangeParentID = (ParentID: string | number) => {
        setModel({
            ...model,
            ParentID,
        });
    };

    const handleChangeNotify = (Notify: number) => {
        setModel({
            ...model,
            Notify,
        });
    };

    // we might divide it to just getTitle function and getFolderTestId or something
    const getTitleAndTestId = () => {
        const isFolder = model.Type === LABEL_TYPE.MESSAGE_FOLDER;
        if (mode === 'create') {
            return {
                title: isFolder ? c('Label/folder modal').t`Create folder` : c('Label/folder modal').t`Create label`,
                testId: isFolder ? 'create-folder-modal' : 'create-label-modal',
            };
        }
        return {
            title: isFolder ? c('Label/folder modal').t`Edit folder` : c('Label/folder modal').t`Edit label`,
            testId: isFolder ? 'edit-folder-modal' : 'edit-label-modal',
        };
    };


    return (
        <ModalTwo size="large" data-testid={getTitleAndTestId().testId} {...rest}>
            <ModalTwoHeader title={getTitleAndTestId().title} />
            <ModalTwoContent>
                <NewLabelForm
                    label={model}
                    onChangeName={handleChangeName}
                    onChangeColor={handleChangeColor}
                    onChangeParentID={handleChangeParentID}
                    onChangeNotify={handleChangeNotify}
                    validator={validator}
                />
            </ModalTwoContent>
            <ModalTwoFooter>
                <Button onClick={handleClose}>{c('Action').t`Cancel`}</Button>
                <Button color="norm" loading={loading} onClick={handleSubmit}>{c('Action').t`Save`}</Button>
            </ModalTwoFooter>
        </ModalTwo>
    );
};

export default EditLabelModal;