Files
parcer/src/components/organisms/ProxyForm.tsx

155 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-09-30 17:20:16 +05:30
import React, { useState } from 'react';
2024-10-01 00:06:56 +05:30
import { styled } from '@mui/system';
import { TextField, Button, Switch, FormControlLabel, Box, Typography } from '@mui/material';
2024-10-01 00:06:56 +05:30
import { sendProxyConfig } from '../../api/proxy';
2024-10-01 00:09:06 +05:30
import { useGlobalInfoStore } from '../../context/globalInfo';
2024-09-30 17:20:16 +05:30
const FormContainer = styled(Box)({
2024-09-30 23:29:06 +05:30
display: 'flex',
flexDirection: 'column',
gap: '16px',
padding: '20px',
borderRadius: '8px',
2024-09-30 17:20:16 +05:30
});
2024-09-30 23:26:24 +05:30
const FormControl = styled(Box)({
2024-09-30 23:29:06 +05:30
marginBottom: '16px',
2024-09-30 23:26:24 +05:30
});
2024-09-30 17:20:16 +05:30
const ProxyForm: React.FC = () => {
2024-09-30 23:29:06 +05:30
const [proxyConfig, setProxyConfig] = useState({
2024-10-02 23:40:46 +05:30
server_url: '',
2024-09-30 23:29:06 +05:30
username: '',
password: '',
});
const [requiresAuth, setRequiresAuth] = useState<boolean>(false);
2024-10-02 23:39:08 +05:30
const [errors, setErrors] = useState({
2024-10-02 23:40:46 +05:30
server_url: '',
2024-10-02 23:39:08 +05:30
username: '',
password: '',
});
2024-10-02 23:39:19 +05:30
2024-10-01 00:09:06 +05:30
const { notify } = useGlobalInfoStore();
2024-10-02 23:39:08 +05:30
const validateForm = () => {
let valid = true;
2024-10-02 23:40:46 +05:30
let errorMessages = { server_url: '', username: '', password: '' };
2024-10-02 23:39:08 +05:30
2024-10-02 23:40:46 +05:30
if (!proxyConfig.server_url) {
errorMessages.server_url = 'Server URL is required';
2024-10-02 23:39:08 +05:30
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;
};
2024-09-30 23:29:06 +05:30
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setProxyConfig({ ...proxyConfig, [name]: value });
};
2024-09-30 17:20:16 +05:30
const handleAuthToggle = (e: React.ChangeEvent<HTMLInputElement>) => {
setRequiresAuth(e.target.checked);
if (!e.target.checked) {
setProxyConfig({ ...proxyConfig, username: '', password: '' });
2024-10-02 23:39:08 +05:30
setErrors({ ...errors, username: '', password: '' });
}
};
2024-09-30 23:29:06 +05:30
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
2024-10-02 23:39:08 +05:30
if (!validateForm()) {
return;
}
2024-10-02 23:39:19 +05:30
2024-10-01 00:06:56 +05:30
await sendProxyConfig(proxyConfig).then((response) => {
if (response) {
2024-10-01 00:09:32 +05:30
notify('success', 'Proxy configuration submitted successfully');
2024-10-01 00:06:56 +05:30
} else {
2024-10-01 00:09:06 +05:30
notify('error', 'Failed to submit proxy configuration. Try again.');
2024-10-01 00:06:56 +05:30
}
});
2024-09-30 23:29:06 +05:30
};
2024-09-30 17:20:16 +05:30
2024-09-30 23:29:06 +05:30
return (
<FormContainer>
<form onSubmit={handleSubmit}>
<Typography variant="subtitle1" gutterBottom style={{ marginBottom: '20px', marginTop: '20px' }}>
Proxy Configuration
</Typography>
2024-09-30 23:29:06 +05:30
<FormControl>
<TextField
label="Proxy Server URL"
2024-10-02 23:40:46 +05:30
name="server_url"
value={proxyConfig.server_url}
2024-09-30 23:29:06 +05:30
onChange={handleChange}
fullWidth
required
2024-10-02 23:40:46 +05:30
error={!!errors.server_url}
2024-10-06 01:15:21 +05:30
helperText={errors.server_url || `Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example http://myproxy.com:3128 or
socks5://myproxy.com:3128. Short form myproxy.com:3128 is considered an HTTP proxy.`}
2024-09-30 23:29:06 +05:30
/>
</FormControl>
<FormControl>
<FormControlLabel
control={<Switch checked={requiresAuth} onChange={handleAuthToggle} />}
2024-10-02 23:39:08 +05:30
label="Requires Authentication?"
2024-09-30 23:29:06 +05:30
/>
</FormControl>
{requiresAuth && (
<>
<FormControl>
<TextField
label="Username"
name="username"
value={proxyConfig.username}
onChange={handleChange}
fullWidth
2024-10-02 23:39:08 +05:30
required={requiresAuth}
error={!!errors.username}
helperText={errors.username || ''}
/>
</FormControl>
<FormControl>
<TextField
label="Password"
name="password"
value={proxyConfig.password}
onChange={handleChange}
type="password"
fullWidth
2024-10-02 23:39:08 +05:30
required={requiresAuth}
error={!!errors.password}
helperText={errors.password || ''}
/>
</FormControl>
</>
)}
2024-10-02 23:39:08 +05:30
<Button
variant="contained"
color="primary"
type="submit"
fullWidth
2024-10-02 23:40:46 +05:30
disabled={!proxyConfig.server_url || (requiresAuth && (!proxyConfig.username || !proxyConfig.password))}
2024-10-02 23:39:08 +05:30
>
2024-09-30 23:29:06 +05:30
Add Proxy
</Button>
</form>
</FormContainer>
);
2024-09-30 17:20:16 +05:30
};
2024-10-02 23:39:08 +05:30
export default ProxyForm;