-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathServerSettings.jsx
167 lines (150 loc) · 5.83 KB
/
ServerSettings.jsx
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
import Panel from "../components/Panel";
import React, {useEffect, useState} from "react";
import settingsResource from "../../api/resources/settings";
import Input from "../components/Input";
import Label from "../components/Label";
import Checkbox from "../components/Checkbox";
import InputPassword from "../components/InputPassword";
import Button from "../components/Button";
import {useForm} from "react-hook-form";
const ServerSettings = () => {
const [settings, setSettings] = useState();
const [numberInputs, setNumberInputs] = useState([]);
const {register: registerForm, handleSubmit, formState: {errors}, control} = useForm();
const fetchSettings = async () => {
const res = await settingsResource.server.list();
setSettings(res);
};
const saveServerSettings = data => {
data.tags = data.tags.split(',');
data.admins = data.admins.split(',');
numberInputs.forEach(numberInput => {
data[numberInput] = parseInt(data[numberInput]);
});
Object.keys(settings).map(key => {
if (key.startsWith("_comment")) {
data[key] = settings[key];
}
});
settingsResource.server.update(data)
.then(() => {
fetchSettings()
.then(() => window.flash("Settings saved.", "green"))
});
}
useEffect(() => {
fetchSettings();
}, []);
const formTypeField = (name, value, label = null) => {
if (name.startsWith("_comment_")) {
return null;
}
const register = registerForm(name)
switch (typeof value) {
case "undefined":
break;
case "function":
break;
case "symbol":
break;
case "bigint":
break;
case "number":
if(numberInputs.indexOf(name) === -1) {
setNumberInputs(old => [...old, name])
}
return (
<>
<Label htmlFor={name} text={label}/>
<Input type="number" register={register} valueAsNumber="double" defaultValue={value} />
</>
)
case "string":
if (name.includes("password")) {
return (
<>
<Label htmlFor={name} text={label}/>
<InputPassword name={name} register={register} defaultValue={value}/>
</>
)
} else {
return (
<>
<Label htmlFor={name} text={label}/>
<Input name={name} register={register} defaultValue={value}/>
</>
)
}
case "boolean":
return (
<Checkbox checked={value} text={label} register={register} name={name}/>
)
case "object":
if (Array.isArray(value)) {
return (
<>
<Label htmlFor={name} text={label}/>
<Input name={name} register={register} defaultValue={value}/>
</>
)
} else if (name.includes("visibility")) {
return (
<>
<Label text="Visibility"/>
<div className="flex">
{Object.keys(value).map(key => {
const register = registerForm(`visibility[${key}]`)
return (
<div className="mr-4" key={`visibility-${key}`}>
<Checkbox checked={value[key]} register={register} text={key}/>
</div>
)
})}
</div>
</>
)
}
break;
default:
return (
<>
<Label htmlFor={name} text={label}/>
<Input name={name} register={register} defaultValue={value}/>
</>
)
}
}
return (
<form className="mb-4" onSubmit={handleSubmit(saveServerSettings)}>
<Panel
title="Server Settings"
content={
<>
{settings && Object.keys(settings).map(key => {
if (key.startsWith("_comment_")) {
return (
<div key={key}>
{formTypeField(key, value)}
</div>
);
}
const value = settings[key]
const label = key.replaceAll('_', ' ')
const comment = settings["_comment_" + key]
return (
<div className="mb-4" key={`wrapper-${key}`}>
{formTypeField(key, value, label)}
<p className="text-sm italic">{comment}</p>
</div>
)
})}
</>
}
actions={
<Button isSubmit={true} type="success">Save</Button>
}
/>
</form>
)
}
export default ServerSettings;