Skip to content

Commit

Permalink
Merge pull request #137 from zluipaiva/reset-cube-on-delete
Browse files Browse the repository at this point in the history
Reset selected cube after delete
  • Loading branch information
bryanlundberg authored Oct 29, 2023
2 parents 11c949a + d08808b commit deae6fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/hooks/useModalCube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function useModalCube() {
setCubeName,
} = useCubesModalStore();
const { lang } = useSettingsModalStore();
const { setCubes } = useTimerStore();
const { setCubes, setSelectedCube, setNewScramble, selectedCube } = useTimerStore();
const [error, setError] = useState<boolean>(false);

const handleClickRadio = (category: Categories) => {
Expand Down Expand Up @@ -73,6 +73,12 @@ export default function useModalCube() {
if (!editingCube) return;
const updatedCubeDB = cubeDB.filter((cube) => cube.id !== editingCube.id);
window.localStorage.setItem("cubes", JSON.stringify(updatedCubeDB));

if (selectedCube && selectedCube.id === editingCube.id) {
setSelectedCube(null);
setNewScramble(null);
}

setCubes(updatedCubeDB);
setModalOpen(false);
setEditingCube(null);
Expand Down
10 changes: 7 additions & 3 deletions src/store/timerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ export const useTimerStore = create<TimerStore>((set) => ({
lastSolve: null,
solvingTime: 0,
isSolving: false,
setNewScramble: (cube: Cube) => {
setNewScramble: (cube: Cube | null) => {
set((state) => ({
...state,
scramble: genScramble(cube.category),
scramble: cube ? genScramble(cube.category) : null,
}));
},
setCubes: (cubes: Cube[]) => {
set((state) => ({ ...state, cubes }));
},
setSelectedCube: (cube: Cube) => {
setSelectedCube: (cube: Cube | null) => {
set((state: any) => {
if (!cube) {
return { ...state, event: null, selectedCube: null };
}

const selectedEvent = cubeCollection.find(
(item) => item.name === cube.category
);
Expand Down

0 comments on commit deae6fd

Please sign in to comment.