feat: new profile able to edit name and desc

This commit is contained in:
GyDi 2022-02-13 19:33:24 +08:00
parent fc48aa7155
commit 5b779b4f14
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
2 changed files with 79 additions and 6 deletions

View File

@ -0,0 +1,64 @@
import { useState } from "react";
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
TextField,
} from "@mui/material";
import Notice from "../base/base-notice";
interface Props {
open: boolean;
onClose: () => void;
onSubmit: (name: string, desc: string) => void;
}
const ProfileNew = (props: Props) => {
const { open, onClose, onSubmit } = props;
const [name, setName] = useState("");
const [desc, setDesc] = useState("");
const onCreate = () => {
if (!name.trim()) {
Notice.error("`Name` should not be null");
return;
}
onSubmit(name, desc);
};
return (
<Dialog open={open} onClose={onClose}>
<DialogTitle>Create Profile</DialogTitle>
<DialogContent sx={{ width: 320, pb: 0.5 }}>
<TextField
autoFocus
fullWidth
label="Name"
margin="dense"
variant="outlined"
value={name}
onChange={(e) => setName(e.target.value)}
/>
<TextField
fullWidth
label="Descriptions"
margin="normal"
variant="outlined"
value={desc}
onChange={(e) => setDesc(e.target.value)}
/>
</DialogContent>
<DialogActions sx={{ px: 2, pb: 2 }}>
<Button onClick={onClose}>Cancel</Button>
<Button onClick={onCreate} variant="contained">
Create
</Button>
</DialogActions>
</Dialog>
);
};
export default ProfileNew;

View File

@ -10,9 +10,10 @@ import {
} from "../services/cmds"; } from "../services/cmds";
import { getProxies, updateProxy } from "../services/api"; import { getProxies, updateProxy } from "../services/api";
import noop from "../utils/noop"; import noop from "../utils/noop";
import Notice from "../components/notice"; import Notice from "../components/base/base-notice";
import BasePage from "../components/base-page"; import BasePage from "../components/base/base-page";
import ProfileItem from "../components/profile-item"; import ProfileItem from "../components/profile/profile-item";
import ProfileNew from "../components/profile/profile-new";
const ProfilePage = () => { const ProfilePage = () => {
const [url, setUrl] = useState(""); const [url, setUrl] = useState("");
@ -96,13 +97,15 @@ const ProfilePage = () => {
}; };
const lockNewRef = useRef(false); const lockNewRef = useRef(false);
const onNew = async () => { const [dialogOpen, setDialogOpen] = useState(false);
const onNew = async (name: string, desc: string) => {
if (lockNewRef.current) return; if (lockNewRef.current) return;
lockNewRef.current = true; lockNewRef.current = true;
try { try {
await newProfile("New Profile", "no desc"); await newProfile(name, desc);
mutate("getProfiles"); mutate("getProfiles");
setDialogOpen(false);
} catch (err: any) { } catch (err: any) {
err && Notice.error(err.toString()); err && Notice.error(err.toString());
} finally { } finally {
@ -131,7 +134,7 @@ const ProfilePage = () => {
> >
Import Import
</Button> </Button>
<Button variant="contained" onClick={onNew}> <Button variant="contained" onClick={() => setDialogOpen(true)}>
New New
</Button> </Button>
</Box> </Box>
@ -148,6 +151,12 @@ const ProfilePage = () => {
</Grid> </Grid>
))} ))}
</Grid> </Grid>
<ProfileNew
open={dialogOpen}
onClose={() => setDialogOpen(false)}
onSubmit={onNew}
/>
</BasePage> </BasePage>
); );
}; };