feat: check if proxy requires auth & accordingly show username & password fields

This commit is contained in:
karishmas6
2024-10-02 23:33:16 +05:30
parent b1115218e7
commit 992c75bf00

View File

@@ -1,6 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { styled } from '@mui/system'; import { styled } from '@mui/system';
import { TextField, Button, RadioGroup, FormControlLabel, Radio, Box, Typography } from '@mui/material'; import { TextField, Button, Switch, FormControlLabel, Box, Typography } from '@mui/material';
import { sendProxyConfig } from '../../api/proxy'; import { sendProxyConfig } from '../../api/proxy';
import { useGlobalInfoStore } from '../../context/globalInfo'; import { useGlobalInfoStore } from '../../context/globalInfo';
@@ -22,6 +22,7 @@ const ProxyForm: React.FC = () => {
username: '', username: '',
password: '', password: '',
}); });
const [requiresAuth, setRequiresAuth] = useState<boolean>(false);
const { notify } = useGlobalInfoStore(); const { notify } = useGlobalInfoStore();
@@ -30,6 +31,13 @@ const ProxyForm: React.FC = () => {
setProxyConfig({ ...proxyConfig, [name]: value }); setProxyConfig({ ...proxyConfig, [name]: value });
}; };
const handleAuthToggle = (e: React.ChangeEvent<HTMLInputElement>) => {
setRequiresAuth(e.target.checked);
if (!e.target.checked) {
setProxyConfig({ ...proxyConfig, username: '', password: '' });
}
};
const handleSubmit = async (e: React.FormEvent) => { const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
await sendProxyConfig(proxyConfig).then((response) => { await sendProxyConfig(proxyConfig).then((response) => {
@@ -44,7 +52,9 @@ const ProxyForm: React.FC = () => {
return ( return (
<FormContainer> <FormContainer>
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
<Typography variant="subtitle1" gutterBottom style={{ marginBottom: '20px', marginTop: '20px' }}>Proxy Configuration</Typography> <Typography variant="subtitle1" gutterBottom style={{ marginBottom: '20px', marginTop: '20px' }}>
Proxy Configuration
</Typography>
<FormControl> <FormControl>
<TextField <TextField
label="Proxy Server URL" label="Proxy Server URL"
@@ -56,25 +66,37 @@ const ProxyForm: React.FC = () => {
helperText="e.g., http://proxy-server.com:8080" helperText="e.g., http://proxy-server.com:8080"
/> />
</FormControl> </FormControl>
<FormControl>
<FormControlLabel
control={<Switch checked={requiresAuth} onChange={handleAuthToggle} />}
label="Requires Authentication?"
/>
</FormControl>
{requiresAuth && (
<>
<FormControl> <FormControl>
<TextField <TextField
label="Username (Optional)" label="Username"
name="username" name="username"
value={proxyConfig.username} value={proxyConfig.username}
onChange={handleChange} onChange={handleChange}
fullWidth fullWidth
required
/> />
</FormControl> </FormControl>
<FormControl> <FormControl>
<TextField <TextField
label="Password (Optional)" label="Password"
name="password" name="password"
value={proxyConfig.password} value={proxyConfig.password}
onChange={handleChange} onChange={handleChange}
type="password" type="password"
fullWidth fullWidth
required
/> />
</FormControl> </FormControl>
</>
)}
<Button variant="contained" color="primary" type="submit" fullWidth> <Button variant="contained" color="primary" type="submit" fullWidth>
Add Proxy Add Proxy
</Button> </Button>