feat: profile item adjust

This commit is contained in:
GyDi
2022-03-05 19:04:20 +08:00
parent 08fa5205b0
commit f44039b628
10 changed files with 262 additions and 108 deletions

View File

@@ -1,63 +1,105 @@
import { useEffect, useState } from "react";
import { useSWRConfig } from "swr";
import { useLockFn, useSetState } from "ahooks";
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
FormControl,
InputLabel,
MenuItem,
Select,
TextField,
} from "@mui/material";
import { createProfile } from "../../services/cmds";
import Notice from "../base/base-notice";
interface Props {
open: boolean;
onClose: () => void;
onSubmit: (name: string, desc: string) => void;
}
// create a new profile
// remote / local file / merge / script
const ProfileNew = (props: Props) => {
const { open, onClose, onSubmit } = props;
const [name, setName] = useState("");
const [desc, setDesc] = useState("");
const { open, onClose } = props;
const onCreate = () => {
if (!name.trim()) {
Notice.error("`Name` should not be null");
const { mutate } = useSWRConfig();
const [form, setForm] = useSetState({
name: "",
desc: "",
type: "remote",
url: "",
});
const onCreate = useLockFn(async () => {
if (!form.type) {
Notice.error("`Type` should not be null");
return;
}
onSubmit(name, desc);
};
useEffect(() => {
if (!open) {
setName("");
setDesc("");
try {
await createProfile({ ...form });
setForm({ name: "", desc: "", type: "remote", url: "" });
mutate("getProfiles");
onClose();
} catch (err: any) {
Notice.error(err.message || err.toString());
}
}, [open]);
});
const textFieldProps = {
fullWidth: true,
size: "small",
margin: "normal",
variant: "outlined",
} as const;
return (
<Dialog open={open} onClose={onClose}>
<DialogTitle>Create Profile</DialogTitle>
<DialogContent sx={{ width: 320, pb: 0.5 }}>
<DialogTitle sx={{ pb: 0.5 }}>Create Profile</DialogTitle>
<DialogContent sx={{ width: 336, pb: 1 }}>
<TextField
{...textFieldProps}
autoFocus
fullWidth
label="Name"
margin="dense"
variant="outlined"
value={name}
onChange={(e) => setName(e.target.value)}
value={form.name}
onChange={(e) => setForm({ name: e.target.value })}
/>
<FormControl size="small" fullWidth sx={{ mt: 2, mb: 1 }}>
<InputLabel>Type</InputLabel>
<Select
label="Type"
value={form.type}
onChange={(e) => setForm({ type: e.target.value })}
>
<MenuItem value="remote">Remote</MenuItem>
<MenuItem value="local">Local</MenuItem>
<MenuItem value="script">Script</MenuItem>
<MenuItem value="merge">Merge</MenuItem>
</Select>
</FormControl>
<TextField
fullWidth
{...textFieldProps}
label="Descriptions"
margin="normal"
variant="outlined"
value={desc}
onChange={(e) => setDesc(e.target.value)}
value={form.desc}
onChange={(e) => setForm({ desc: e.target.value })}
/>
{form.type === "remote" && (
<TextField
{...textFieldProps}
label="Subscription Url"
value={form.url}
onChange={(e) => setForm({ url: e.target.value })}
/>
)}
</DialogContent>
<DialogActions sx={{ px: 2, pb: 2 }}>
<Button onClick={onClose}>Cancel</Button>
<Button onClick={onCreate} variant="contained">