feat: proxy config

This commit is contained in:
karishmas6
2024-10-26 06:19:56 +05:30
parent 2a90685f1d
commit 3ca8e7d1e2

View File

@@ -16,7 +16,7 @@ const FormControl = styled(Box)({
}); });
const ProxyForm: React.FC = () => { const ProxyForm: React.FC = () => {
const [proxyConfig, setProxyConfig] = useState({ const [proxyConfigForm, setProxyConfigForm] = useState({
server_url: '', server_url: '',
username: '', username: '',
password: '', password: '',
@@ -29,6 +29,7 @@ const ProxyForm: React.FC = () => {
}); });
const [tabIndex, setTabIndex] = useState(0); const [tabIndex, setTabIndex] = useState(0);
const [isProxyConfigured, setIsProxyConfigured] = useState(false); const [isProxyConfigured, setIsProxyConfigured] = useState(false);
const [proxy, setProxy] = useState({ proxy_url: '', auth: false });
const { notify } = useGlobalInfoStore(); const { notify } = useGlobalInfoStore();
@@ -36,17 +37,17 @@ const ProxyForm: React.FC = () => {
let valid = true; let valid = true;
let errorMessages = { server_url: '', username: '', password: '' }; let errorMessages = { server_url: '', username: '', password: '' };
if (!proxyConfig.server_url) { if (!proxyConfigForm.server_url) {
errorMessages.server_url = 'Server URL is required'; errorMessages.server_url = 'Server URL is required';
valid = false; valid = false;
} }
if (requiresAuth) { if (requiresAuth) {
if (!proxyConfig.username) { if (!proxyConfigForm.username) {
errorMessages.username = 'Username is required for authenticated proxies'; errorMessages.username = 'Username is required for authenticated proxies';
valid = false; valid = false;
} }
if (!proxyConfig.password) { if (!proxyConfigForm.password) {
errorMessages.password = 'Password is required for authenticated proxies'; errorMessages.password = 'Password is required for authenticated proxies';
valid = false; valid = false;
} }
@@ -58,13 +59,13 @@ const ProxyForm: React.FC = () => {
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 }); setProxyConfigForm({ ...proxyConfigForm, [name]: value });
}; };
const handleAuthToggle = (e: React.ChangeEvent<HTMLInputElement>) => { const handleAuthToggle = (e: React.ChangeEvent<HTMLInputElement>) => {
setRequiresAuth(e.target.checked); setRequiresAuth(e.target.checked);
if (!e.target.checked) { if (!e.target.checked) {
setProxyConfig({ ...proxyConfig, username: '', password: '' }); setProxyConfigForm({ ...proxyConfigForm, username: '', password: '' });
setErrors({ ...errors, username: '', password: '' }); setErrors({ ...errors, username: '', password: '' });
} }
}; };
@@ -75,13 +76,14 @@ const ProxyForm: React.FC = () => {
return; return;
} }
await sendProxyConfig(proxyConfig).then((response) => { try {
const response = await sendProxyConfig(proxyConfigForm);
if (response) { if (response) {
notify('success', 'Proxy configuration submitted successfully'); notify('success', 'Proxy configuration submitted successfully');
} else {
notify('error', 'Failed to submit proxy configuration. Try again.');
} }
}); } catch (error: any) {
notify('error', error);
}
}; };
const handleTabChange = (event: React.SyntheticEvent, newValue: number) => { const handleTabChange = (event: React.SyntheticEvent, newValue: number) => {
@@ -99,14 +101,16 @@ const ProxyForm: React.FC = () => {
}; };
const fetchProxyConfig = async () => { const fetchProxyConfig = async () => {
await getProxyConfig().then((response) => { try {
const response = await getProxyConfig();
if (response.proxy_url) { if (response.proxy_url) {
setIsProxyConfigured(true); setIsProxyConfigured(true);
notify('success', 'Proxy configuration fetched successfully'); setProxy(response);
} else { notify('success', 'Proxy configuration fetched successfully');
notify('error', 'Failed to fetch proxy configuration. Try again.');
} }
}); } catch (error:any) {
notify('error', error);
}
}; };
const removeProxy = async () => { const removeProxy = async () => {
@@ -114,6 +118,7 @@ const ProxyForm: React.FC = () => {
if (response) { if (response) {
notify('success', 'Proxy configuration removed successfully'); notify('success', 'Proxy configuration removed successfully');
setIsProxyConfigured(false); setIsProxyConfigured(false);
setProxy({ proxy_url: '', auth: false });
} else { } else {
notify('error', 'Failed to remove proxy configuration. Try again.'); notify('error', 'Failed to remove proxy configuration. Try again.');
} }
@@ -132,10 +137,31 @@ const ProxyForm: React.FC = () => {
<Typography variant="body1" gutterBottom component="div"> <Typography variant="body1" gutterBottom component="div">
Proxy is already configured. You can test the configuration below. Proxy is already configured. You can test the configuration below.
</Typography> </Typography>
<br /> <Box sx={{ maxWidth: 600, width: '100%', textAlign: 'center', marginTop: '20px' }}>
<Typography variant="h6" gutterBottom component="div">
Current Proxy Configuration
</Typography>
<table style={{ width: '100%', borderCollapse: 'collapse', marginTop: '20px' }}>
<thead>
<tr>
<th style={{ border: '1px solid #ddd', padding: '8px' }}>Proxy URL</th>
<th style={{ border: '1px solid #ddd', padding: '8px' }}>Requires Authentication</th>
</tr>
</thead>
<tbody>
<tr>
<td style={{ border: '1px solid #ddd', padding: '8px' }}>{proxy.proxy_url}</td>
<td style={{ border: '1px solid #ddd', padding: '8px' }}>{proxy.auth ? 'Yes' : 'No'}</td>
</tr>
</tbody>
</table>
</Box>
<Button variant="contained" color="primary" onClick={testProxy} sx={{ marginTop: '20px' }}> <Button variant="contained" color="primary" onClick={testProxy} sx={{ marginTop: '20px' }}>
Test Proxy Configuration Test Proxy Configuration
</Button> </Button>
<Button variant="contained" color="secondary" onClick={removeProxy} sx={{ marginTop: '20px' }}>
Remove Proxy Configuration
</Button>
</Box> </Box>
) : ( ) : (
<FormContainer> <FormContainer>
@@ -152,7 +178,7 @@ const ProxyForm: React.FC = () => {
<TextField <TextField
label="Proxy Server URL" label="Proxy Server URL"
name="server_url" name="server_url"
value={proxyConfig.server_url} value={proxyConfigForm.server_url}
onChange={handleChange} onChange={handleChange}
fullWidth fullWidth
required required
@@ -174,7 +200,7 @@ const ProxyForm: React.FC = () => {
<TextField <TextField
label="Username" label="Username"
name="username" name="username"
value={proxyConfig.username} value={proxyConfigForm.username}
onChange={handleChange} onChange={handleChange}
fullWidth fullWidth
required={requiresAuth} required={requiresAuth}
@@ -186,7 +212,7 @@ const ProxyForm: React.FC = () => {
<TextField <TextField
label="Password" label="Password"
name="password" name="password"
value={proxyConfig.password} value={proxyConfigForm.password}
onChange={handleChange} onChange={handleChange}
type="password" type="password"
fullWidth fullWidth
@@ -202,7 +228,7 @@ const ProxyForm: React.FC = () => {
color="primary" color="primary"
type="submit" type="submit"
fullWidth fullWidth
disabled={!proxyConfig.server_url || (requiresAuth && (!proxyConfig.username || !proxyConfig.password))} disabled={!proxyConfigForm.server_url || (requiresAuth && (!proxyConfigForm.username || !proxyConfigForm.password))}
> >
Add Proxy Add Proxy
</Button> </Button>