-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsettings-form.tsx
93 lines (89 loc) · 2.49 KB
/
settings-form.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
import { FC, useCallback, FormEvent } from 'react';
import { useQuery, useMutation, gql } from '@apollo/client';
import {
SettingsQueryData,
SettingsMutationVariables,
} from '../types/settings';
const GET_SETTINGS = gql`
query GetSettings {
settings {
enableNotifications
profileVisibility
}
}
`;
const MUTATE_SETTINGS = gql`
mutation MutateSettings(
$enableNotifications: Boolean!
$profileVisibility: String!
) {
mutateSettings(
enableNotifications: $enableNotifications
profileVisibility: $profileVisibility
) {
enableNotifications
profileVisibility
}
}
`;
export const SettingsForm: FC = () => {
const settingsQuery = useQuery<SettingsQueryData>(GET_SETTINGS);
const [settingsMutation, settingsMutationMeta] = useMutation<
null,
SettingsMutationVariables
>(MUTATE_SETTINGS);
// eslint-disable-next-line @typescript-eslint/no-empty-function
const handleFormSubmit = useCallback((event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
void settingsMutation({
variables: {
enableNotifications: false,
profileVisibility: 'public',
},
});
}, []);
return (
<div>
<h1>Settings</h1>
{settingsQuery.loading ? (
'Loading...'
) : settingsQuery.error ? (
<div>Failed to load settings</div>
) : (
<form onSubmit={handleFormSubmit}>
<div>
<label htmlFor="enableNotifications">Notifications</label>
<input
type="checkbox"
id="enableNotifications"
name="enableNotifications"
defaultChecked={settingsQuery.data?.settings.enableNotifications}
/>
</div>
<div>
<label htmlFor="profileVisibility">Profile visibility</label>
<select
id="profileVisibility"
name="profileVisibility"
defaultValue={settingsQuery.data?.settings.profileVisibility}
>
<option value="public">Public</option>
<option value="private">Private</option>
</select>
</div>
<button type="submit">Update</button>
</form>
)}
{settingsMutationMeta.called && (
<div>
<br />
<div role="alert">
{settingsMutationMeta.error
? 'Failed to update settings'
: 'Successfully updated settings!'}
</div>
</div>
)}
</div>
);
};