Skip to content

Commit

Permalink
Merge pull request #385 from syrok94/undo-action
Browse files Browse the repository at this point in the history
feat/undo action for deleting a solve
  • Loading branch information
bryanlundberg authored Nov 10, 2024
2 parents 9e1476f + 46fd536 commit abca960
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
27 changes: 26 additions & 1 deletion src/components/menu-solve-options/menu-solve-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ export default function MenuSolveOptions({
const dialog = useDialogSolve();

if (!selectedCube) return null;
const handleUndoSolve = async (solve : Solve | null) => {
debugger
if (solve && selectedCube) {
await updateSolve({
solveId: solve.id,
selectedCube: selectedCube,
type: "UNDO",
deletedSolve:solve,
});

toast("", {
description: "Restored last deleted Solve",
duration: 1000,
});

const lastCube = await getCubeById(selectedCube.id);
if (lastCube) {
setSelectedCube({ ...lastCube });
}
}
};

const handleDeleteSolve = async () => {
if (solve && selectedCube) {
Expand Down Expand Up @@ -68,7 +89,11 @@ export default function MenuSolveOptions({

toast("", {
description: "Deleted solve",
duration: 1000,
duration: 2000,
action: {
label: 'Undo',
onClick: () => handleUndoSolve(solve)
},
});

onDeleteSolve();
Expand Down
34 changes: 21 additions & 13 deletions src/lib/updateSolve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { saveCube } from "@/db/dbOperations";
import { Cube } from "@/interfaces/Cube";
import { Solve } from "@/interfaces/Solve";
import { useState } from "react";

/**
* Updates the specified solve in the cubes' solves arrays.
Expand All @@ -14,27 +15,34 @@ export default async function updateSolve({
solveId,
type,
comment,
deletedSolve,
}: {
selectedCube: Cube;
solveId: string;
type: "+2" | "DNF" | "COMMENT" | "BOOKMARK" | "DELETE";
type: "+2" | "DNF" | "COMMENT" | "BOOKMARK" | "DELETE" | "UNDO";
comment?: string;
deletedSolve?:Solve;
}): Promise<Cube | null> {
const updateSolveArray = (solveArray: Solve[]) => {
const solveIndex = solveArray.findIndex((solve) => solve.id === solveId);

if (solveIndex !== -1) {
const solveToUpdate = solveArray[solveIndex];

if (type === "+2") {
solveToUpdate.plus2 = !solveToUpdate.plus2;
solveToUpdate.time += solveToUpdate.plus2 ? 2000 : -2000;
} else if (type === "COMMENT") {
solveToUpdate.comment = comment ?? "";
} else if (type === "BOOKMARK") {
solveToUpdate.bookmark = !solveToUpdate.bookmark;
} else if (type === "DELETE") {
solveArray.splice(solveIndex, 1); // Remove the solve from the array
if (solveIndex !== -1 || (type === "UNDO" && deletedSolve)) {
const solveToUpdate = type === "UNDO" ? deletedSolve : solveArray[solveIndex];

if(solveToUpdate){
if (type === "+2") {
solveToUpdate.plus2 = !solveToUpdate.plus2;
solveToUpdate.time += solveToUpdate.plus2 ? 2000 : -2000;
} else if (type === "COMMENT") {
solveToUpdate.comment = comment ?? "";
} else if (type === "BOOKMARK") {
solveToUpdate.bookmark = !solveToUpdate.bookmark;
} else if (type === "DELETE") {
deletedSolve = solveToUpdate;
solveArray.splice(solveIndex, 1); // Remove the solve from the array
}else if(type === "UNDO" && deletedSolve) {
solveArray.push(deletedSolve);
}
}
}
};
Expand Down

0 comments on commit abca960

Please sign in to comment.