Merge pull request #377 from Ashutosh-Bhadauriya/enhancement/add-save-button

Enhance: Add save button in deleteDialogue when creating survey
This commit is contained in:
Johannes
2023-06-15 11:18:21 -05:00
committed by GitHub
2 changed files with 35 additions and 20 deletions

View File

@@ -63,6 +63,29 @@ export default function SurveyMenuBar({
}
};
const saveSurveyAction = (shouldNavigateBack = false) => {
triggerSurveyMutate({ ...localSurvey })
.then(async (response) => {
if (!response?.ok) {
throw new Error(await response?.text());
}
const updatedSurvey = await response.json();
setLocalSurvey(updatedSurvey);
toast.success("Changes saved.");
if (shouldNavigateBack) {
router.back();
} else {
if (localSurvey.status !== "draft") {
router.push(`/environments/${environmentId}/surveys/${localSurvey.id}/summary`);
} else {
router.push(`/environments/${environmentId}/surveys`);
}
}
})
.catch(() => {
toast.error(`Error saving changes`);
});
};
return (
<div className="border-b border-slate-200 bg-white px-5 py-3 sm:flex sm:items-center sm:justify-between">
<div className="flex items-center space-x-2 whitespace-nowrap">
@@ -102,25 +125,7 @@ export default function SurveyMenuBar({
variant={localSurvey.status === "draft" ? "secondary" : "darkCTA"}
className="mr-3"
loading={isMutatingSurvey}
onClick={() => {
triggerSurveyMutate({ ...localSurvey })
.then(async (response) => {
if (!response?.ok) {
throw new Error(await response?.text());
}
const updatedSurvey = await response.json();
setLocalSurvey(updatedSurvey); // update local survey state
toast.success("Changes saved.");
if (localSurvey.status !== "draft") {
router.push(`/environments/${environmentId}/surveys/${localSurvey.id}/summary`);
} else {
router.push(`/environments/${environmentId}/surveys`);
}
})
.catch(() => {
toast.error(`Error saving changes`);
});
}}>
onClick={() => saveSurveyAction()}>
Save
</Button>
{localSurvey.status === "draft" && audiencePrompt && (
@@ -157,6 +162,8 @@ export default function SurveyMenuBar({
setOpen={setDeleteDialogOpen}
onDelete={() => deleteSurveyAction(localSurvey)}
text="Do you want to delete this draft?"
useSaveInsteadOfCancel={true}
onSave={() => saveSurveyAction(true)}
/>
</div>
);

View File

@@ -10,6 +10,8 @@ interface DeleteDialogProps {
onDelete: () => void;
text?: string;
isDeleting?: boolean;
useSaveInsteadOfCancel?: boolean;
onSave?: () => void;
}
export default function DeleteDialog({
@@ -19,6 +21,9 @@ export default function DeleteDialog({
onDelete,
text,
isDeleting,
useSaveInsteadOfCancel = false,
onSave,
}: DeleteDialogProps) {
return (
<Modal open={open} setOpen={setOpen} title={`Delete ${deleteWhat}`}>
@@ -27,9 +32,12 @@ export default function DeleteDialog({
<Button
variant="secondary"
onClick={() => {
if (useSaveInsteadOfCancel && onSave) {
onSave();
}
setOpen(false);
}}>
Cancel
{useSaveInsteadOfCancel ? 'Save' : 'Cancel'}
</Button>
<Button variant="warn" onClick={onDelete} loading={isDeleting}>
Delete