Skip to content

Commit

Permalink
refactor: lookup object rather than switch/case
Browse files Browse the repository at this point in the history
  • Loading branch information
BillClinton committed Aug 15, 2023
1 parent c876375 commit 585cd68
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions static-webapps/slate-task-library/src/machines/TaskFormMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,10 @@ const TaskFormMachine = createMachine(
}),

// User Notifications
toastSuccess: sendParent((_, event) => {
let message;

switch (event.type) {
case "done.invoke.ApiCreate":
message = "task created successfully";
break;
case "done.invoke.ApiUpdate":
message = "task updated successfully";
break;
case "done.invoke.ApiArchive":
message = "task archived successfully";
break;
case "done.invoke.ApiUnarchive":
message = "task unarchived successfully";
break;
default:
message = "operation successful";
}

return {
type: "send.toast",
message,
};
}),
toastSuccess: sendParent((_, event) => ({
type: "send.toast",
message: successMessages[event.type] || "operation successful",
})),

toastFailure: sendParent((context, event) => ({
type: "send.toast",
Expand Down Expand Up @@ -264,4 +243,11 @@ const TaskFormMachine = createMachine(
}
);

const successMessages = {
"done.invoke.ApiCreate": "task created successfully",
"done.invoke.ApiUpdate": "task updated successfully",
"done.invoke.ApiArchive": "task archived successfully",
"done.invoke.ApiUnarchive": "task unarchived successfully",
};

export { TaskFormMachine };

0 comments on commit 585cd68

Please sign in to comment.