-
Notifications
You must be signed in to change notification settings - Fork 69
/
UserDialog.tsx
167 lines (139 loc) · 6.38 KB
/
UserDialog.tsx
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
/*
* Notifo.io
*
* @license
* Copyright (c) Sebastian Stehle. All rights reserved.
*/
import * as React from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { useDispatch } from 'react-redux';
import { Button, Form, Modal, ModalBody, ModalFooter, ModalHeader, Nav, NavItem, NavLink } from 'reactstrap';
import { FormError, Loader, Types, useEventCallback } from '@app/framework';
import { Clients, UpsertUserDto, UserDto } from '@app/service';
import { Forms, NotificationsForm } from '@app/shared/components';
import { upsertUser, useApp, useCore, useUsers } from '@app/state';
import { texts } from '@app/texts';
export interface UserDialogProps {
// The user to edit.
user?: UserDto;
// Invoked when closed.
onClose?: () => void;
}
export const UserDialog = (props: UserDialogProps) => {
const { onClose } = props;
const dispatch = useDispatch<any>();
const app = useApp()!;
const appId = app.id;
const coreLanguages = useCore(x => x.languages);
const coreTimezones = useCore(x => x.timezones);
const upserting = useUsers(x => x.upserting?.isRunning);
const upsertingError = useUsers(x => x.upserting?.error);
const [dialogUser, setDialogUser] = React.useState(props.user);
const [dialogTab, setDialogTab] = React.useState(0);
const [wasUpserting, setWasUpserting] = React.useState(false);
const dialogUserId = dialogUser?.id;
React.useEffect(() => {
async function loadData(id: string) {
try {
const newUser = await Clients.Users.getUser(appId, id);
setDialogUser(newUser);
} catch {
}
}
if (dialogUserId) {
loadData(dialogUserId);
}
}, [appId, dialogUserId]);
React.useEffect(() => {
if (upserting) {
setWasUpserting(true);
}
}, [upserting]);
React.useEffect(() => {
if (!upserting && wasUpserting && !upsertingError) {
onClose && onClose();
}
}, [dispatch, onClose, upserting, upsertingError, wasUpserting]);
const doSave = useEventCallback((params: UpsertUserDto) => {
dispatch(upsertUser({ appId, params }));
});
const allProperties = React.useMemo(() => {
const properties = [...dialogUser?.userProperties || []];
if (dialogUser?.properties) {
for (const name of Object.keys(dialogUser.properties)) {
if (!properties.find(x => x.name === name)) {
properties.push({ name, editorLabel: name });
}
}
}
return properties.sortByString(x => x.name);
}, [dialogUser]);
const form = useForm<UpsertUserDto>();
React.useEffect(() => {
form.reset(Types.clone(dialogUser || {}));
}, [dialogUser, form]);
return (
<Modal isOpen={true} size='lg' backdrop={false} toggle={onClose}>
<FormProvider {...form}>
<Form onSubmit={form.handleSubmit(doSave)}>
<ModalHeader toggle={onClose}>
<Nav className='nav-tabs2'>
<NavItem>
<NavLink onClick={() => setDialogTab(0)} active={dialogTab === 0}>{dialogUser ? texts.users.editHeader : texts.users.createHeader}</NavLink>
</NavItem>
<NavItem>
<NavLink onClick={() => setDialogTab(1)} active={dialogTab === 1}>{texts.common.channels}</NavLink>
</NavItem>
</Nav>
</ModalHeader>
<ModalBody>
<fieldset className='mt-3' disabled={upserting}>
{dialogTab === 0 ? (
<>
<Forms.Text name='id'
label={texts.common.id} />
<Forms.Text name='fullName'
label={texts.common.name} />
<Forms.Text name='emailAddress'
label={texts.common.emailAddress} />
<Forms.Text name='phoneNumber'
label={texts.common.phoneNumber} />
<Forms.Select name='preferredLanguage' options={coreLanguages}
label={texts.common.language} />
<Forms.Select name='preferredTimezone' options={coreTimezones}
label={texts.common.timezone} />
{allProperties.length > 0 &&
<>
<hr />
{allProperties.map(x =>
<Forms.Text key={x.name} name={`properties.${x.name}`} hints={x.editorDescription}
label={x.editorLabel || x.name} />,
)}
</>
}
</>
) : (
<>
<NotificationsForm.Settings field='settings'
disabled={upserting} />
<NotificationsForm.Scheduling field='scheduling'
disabled={upserting} />
</>
)}
</fieldset>
<FormError error={upsertingError} />
</ModalBody>
<ModalFooter className='justify-content-between'>
<Button type='button' color='none' onClick={onClose} disabled={upserting}>
{texts.common.cancel}
</Button>
<Button type='submit' color='primary' disabled={upserting}>
<Loader light small visible={upserting} /> {texts.common.save}
</Button>
</ModalFooter>
</Form>
</FormProvider>
</Modal>
);
};