()
| 59 | } |
| 60 | |
| 61 | function NewPluginForm() { |
| 62 | const session = useSession(); |
| 63 | const navigate = useNavigate(); |
| 64 | |
| 65 | const [name, setName] = useState(""); |
| 66 | const [shortDescription, setShortDescription] = useState(""); |
| 67 | const [longDescription, setLongDescription] = useState(""); |
| 68 | |
| 69 | const [isCreating, setIsCreating] = useState(false); |
| 70 | |
| 71 | const [errors, setErrors] = useState<{ name?: string, short?: string, long?: string, general?: string }>({}) |
| 72 | |
| 73 | async function create() { |
| 74 | setIsCreating(true); |
| 75 | setErrors({}); |
| 76 | |
| 77 | let result = await session.apiClient.createPlugin({ |
| 78 | name: name, |
| 79 | short_description: shortDescription, |
| 80 | long_description: longDescription, |
| 81 | }) |
| 82 | |
| 83 | if (isErrorResponse(result)) { |
| 84 | setErrors({ |
| 85 | general: result.response?.description, |
| 86 | name: result.getFieldError("name"), |
| 87 | short: result.getFieldError("short_description"), |
| 88 | long: result.getFieldError("long_description"), |
| 89 | }) |
| 90 | setIsCreating(false); |
| 91 | } else { |
| 92 | navigate(`/user/plugins/${result.id}`) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return <Stack direction={"column"} spacing={2}> |
| 97 | <TextField label="Name" variant="outlined" |
| 98 | error={Boolean(errors.name)} helperText={errors.name} |
| 99 | onChange={(evt) => setName(evt.target.value)} value={name} /> |
| 100 | <TextField label="Short description" variant="outlined" |
| 101 | error={Boolean(errors.short)} helperText={errors.short} |
| 102 | onChange={(evt) => setShortDescription(evt.target.value)} value={shortDescription} /> |
| 103 | <TextField label="Long description" multiline variant="outlined" |
| 104 | error={Boolean(errors.long)} helperText={errors.long} |
| 105 | onChange={(evt) => setLongDescription(evt.target.value)} value={longDescription} /> |
| 106 | |
| 107 | <Typography variant="body1" color={"error"}>{errors.general}</Typography> |
| 108 | <Button disabled={isCreating} color="success" onClick={() => create()}>Create!</Button> |
| 109 | </Stack> |
| 110 | } |
nothing calls this directly
no test coverage detected