Skip to content

Commit 3d5d876

Browse files
committed
Added LocalStorage support
1 parent 4f6abc1 commit 3d5d876

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/hooks/useTerminal.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { useReducer, useState } from "preact/compat";
22
import taskReducer from "../reducers/taskReducer";
33
import moment from "moment";
4-
import { HistoryType } from "../types";
4+
import { HistoryType, TaskType } from "../types";
55

66
export const useTerminal = () => {
77
const [command, setCommand] = useState<string>("");
88
const [history, setHistory] = useState<HistoryType>([]);
9-
const [tasks, dispatch] = useReducer(taskReducer, []);
9+
10+
const loadTasks = (): TaskType[] => {
11+
const savedTasks = localStorage.getItem("tasks");
12+
return savedTasks ? JSON.parse(savedTasks) : [];
13+
};
14+
15+
const [tasks, dispatch] = useReducer(taskReducer, [], loadTasks);
1016

1117
const executeCommand = (command: string) => {
1218
// "add" command
@@ -78,6 +84,7 @@ export const useTerminal = () => {
7884
}
7985

8086
return `
87+
Task ID: ${task.id}\n
8188
Task name: ${task.name}\n
8289
Task status: ${task.status}\n
8390
Date created: ${moment(task.createdAt).format("Do MMM, YYYY")}

src/reducers/taskReducer.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
1-
import { ActionType, TasksType, TaskType } from "../types";
1+
import { ActionType, TaskType } from "../types";
22

3-
let taskIdCounter = 1;
3+
let retrievedId = localStorage.getItem("taskIdCounter");
4+
let taskIdCounter = retrievedId ? parseInt(retrievedId, 10) : 1;
5+
6+
const taskReducer = (state: TaskType[], action: ActionType): TaskType[] => {
7+
let updatedState = state;
48

5-
const taskReducer = (state: TasksType, action: ActionType): TasksType => {
69
switch (action.type) {
710
case "ADD_TASK":
811
const newTask: TaskType = {
9-
id: taskIdCounter++,
12+
id: taskIdCounter,
1013
name: action.payload.name,
1114
status: "pending",
1215
createdAt: new Date(),
1316
completedAt: null,
1417
};
1518

16-
return [...state, newTask];
19+
taskIdCounter++;
20+
localStorage.setItem("taskIdCounter", JSON.stringify(taskIdCounter));
21+
22+
updatedState = [...state, newTask];
23+
break;
1724

1825
case "MARK_DONE":
19-
return state.map((task) =>
26+
updatedState = state.map((task) =>
2027
task.id === action.payload.id
2128
? { ...task, status: "done", completedAt: new Date() }
2229
: task
2330
);
31+
break;
2432

2533
case "REMOVE_TASK":
26-
return state.filter((task) => task.id !== action.payload.id);
34+
updatedState = state.filter((task) => task.id !== action.payload.id);
35+
36+
if (updatedState.length === 0) {
37+
taskIdCounter = 1;
38+
localStorage.setItem("taskIdCounter", "1");
39+
}
40+
break;
2741

2842
default:
2943
return state;
3044
}
45+
46+
localStorage.setItem("tasks", JSON.stringify(updatedState));
47+
return updatedState;
3148
};
3249

3350
export default taskReducer;

0 commit comments

Comments
 (0)