feat: notify errors
This commit is contained in:
@@ -23,9 +23,38 @@ const ProxyForm: React.FC = () => {
|
|||||||
password: '',
|
password: '',
|
||||||
});
|
});
|
||||||
const [requiresAuth, setRequiresAuth] = useState<boolean>(false);
|
const [requiresAuth, setRequiresAuth] = useState<boolean>(false);
|
||||||
|
const [errors, setErrors] = useState({
|
||||||
|
server: '',
|
||||||
|
username: '',
|
||||||
|
password: '',
|
||||||
|
});
|
||||||
|
|
||||||
const { notify } = useGlobalInfoStore();
|
const { notify } = useGlobalInfoStore();
|
||||||
|
|
||||||
|
const validateForm = () => {
|
||||||
|
let valid = true;
|
||||||
|
let errorMessages = { server: '', username: '', password: '' };
|
||||||
|
|
||||||
|
if (!proxyConfig.server) {
|
||||||
|
errorMessages.server = 'Server URL is required';
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requiresAuth) {
|
||||||
|
if (!proxyConfig.username) {
|
||||||
|
errorMessages.username = 'Username is required for authenticated proxies';
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
if (!proxyConfig.password) {
|
||||||
|
errorMessages.password = 'Password is required for authenticated proxies';
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setErrors(errorMessages);
|
||||||
|
return valid;
|
||||||
|
};
|
||||||
|
|
||||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
setProxyConfig({ ...proxyConfig, [name]: value });
|
setProxyConfig({ ...proxyConfig, [name]: value });
|
||||||
@@ -35,11 +64,16 @@ const ProxyForm: React.FC = () => {
|
|||||||
setRequiresAuth(e.target.checked);
|
setRequiresAuth(e.target.checked);
|
||||||
if (!e.target.checked) {
|
if (!e.target.checked) {
|
||||||
setProxyConfig({ ...proxyConfig, username: '', password: '' });
|
setProxyConfig({ ...proxyConfig, username: '', password: '' });
|
||||||
|
setErrors({ ...errors, username: '', password: '' });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async (e: React.FormEvent) => {
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
if (!validateForm()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await sendProxyConfig(proxyConfig).then((response) => {
|
await sendProxyConfig(proxyConfig).then((response) => {
|
||||||
if (response) {
|
if (response) {
|
||||||
notify('success', 'Proxy configuration submitted successfully');
|
notify('success', 'Proxy configuration submitted successfully');
|
||||||
@@ -63,13 +97,14 @@ const ProxyForm: React.FC = () => {
|
|||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
fullWidth
|
fullWidth
|
||||||
required
|
required
|
||||||
helperText="e.g., http://proxy-server.com:8080"
|
error={!!errors.server}
|
||||||
|
helperText={errors.server || 'e.g., http://proxy-server.com:8080'}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
control={<Switch checked={requiresAuth} onChange={handleAuthToggle} />}
|
control={<Switch checked={requiresAuth} onChange={handleAuthToggle} />}
|
||||||
label="Does The Proxy Require Authentication?"
|
label="Requires Authentication?"
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
{requiresAuth && (
|
{requiresAuth && (
|
||||||
@@ -81,7 +116,9 @@ const ProxyForm: React.FC = () => {
|
|||||||
value={proxyConfig.username}
|
value={proxyConfig.username}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
fullWidth
|
fullWidth
|
||||||
required
|
required={requiresAuth}
|
||||||
|
error={!!errors.username}
|
||||||
|
helperText={errors.username || ''}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
@@ -92,12 +129,20 @@ const ProxyForm: React.FC = () => {
|
|||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
type="password"
|
type="password"
|
||||||
fullWidth
|
fullWidth
|
||||||
required
|
required={requiresAuth}
|
||||||
|
error={!!errors.password}
|
||||||
|
helperText={errors.password || ''}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<Button variant="contained" color="primary" type="submit" fullWidth>
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
type="submit"
|
||||||
|
fullWidth
|
||||||
|
disabled={!proxyConfig.server || (requiresAuth && (!proxyConfig.username || !proxyConfig.password))}
|
||||||
|
>
|
||||||
Add Proxy
|
Add Proxy
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
@@ -105,4 +150,4 @@ const ProxyForm: React.FC = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ProxyForm;
|
export default ProxyForm;
|
||||||
|
|||||||
Reference in New Issue
Block a user