Skip to content

Commit

Permalink
fix: fix onremove handle
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 26, 2024
1 parent de12238 commit 1dd2de8
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions web/src/components/form-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ export default function FormEditor({
description: string;
items: FormItem[];
onUpsert: (name: string, data: Record<string, unknown>) => Promise<void>;
onRemove: () => Promise<void>;
onRemove?: () => Promise<void>;
created?: boolean;
currentNames?: string[];
}) {
Expand Down Expand Up @@ -961,14 +961,17 @@ export default function FormEditor({
setProcessing(false);
}
};

const doRemove = async () => {
if (processing) {
return;
}
setProcessing(true);
try {
setShowSuccess(true);
await onRemove();
if (onRemove) {
await onRemove();
}
} catch (err) {
setShowError({
open: true,
Expand All @@ -978,8 +981,13 @@ export default function FormEditor({
setProcessing(false);
}
};
let showRemove = false;
if (onRemove) {
showRemove = true;
}
let createNewItem = <></>;
if (created) {
showRemove = false;
createNewItem = (
<Grid item xs={12}>
<FormControl fullWidth={true}>
Expand All @@ -995,6 +1003,27 @@ export default function FormEditor({
</Grid>
);
}
let removeGrip = <></>;
let submitSpan = 12;
if (showRemove) {
submitSpan = 6;
removeGrip = (
<Grid item xs={6}>
<Button
disabled={created}
fullWidth
variant="outlined"
size="large"
onClick={() => {
doRemove();
}}
>
{processing ? "Removing" : "Remove"}
</Button>
</Grid>
);
}

return (
<React.Fragment>
<CardContent>
Expand All @@ -1012,7 +1041,7 @@ export default function FormEditor({
<Grid container spacing={2}>
{createNewItem}
{list}
<Grid item xs={6}>
<Grid item xs={submitSpan}>
<Button
disabled={!updated}
fullWidth
Expand All @@ -1025,19 +1054,7 @@ export default function FormEditor({
{processing ? "Submitting" : "Submit"}
</Button>
</Grid>
<Grid item xs={6}>
<Button
disabled={created}
fullWidth
variant="outlined"
size="large"
onClick={() => {
doRemove();
}}
>
{processing ? "Removing" : "Remove"}
</Button>
</Grid>
{removeGrip}
</Grid>
</form>
</CardContent>
Expand Down

0 comments on commit 1dd2de8

Please sign in to comment.