forked from Kiranism/next-shadcn-dashboard-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.ts
79 lines (72 loc) · 2.23 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { create } from "zustand";
import { v4 as uuid } from "uuid";
import { persist } from "zustand/middleware";
import { Column } from "@/components/kanban/board-column";
import { UniqueIdentifier } from "@dnd-kit/core";
export type Status = "TODO" | "IN_PROGRESS" | "DONE";
const defaultCols = [
{
id: "TODO" as const,
title: "Todo",
},
] satisfies Column[];
export type ColumnId = (typeof defaultCols)[number]["id"];
export type Task = {
id: string;
title: string;
description?: string;
status: Status;
};
export type State = {
tasks: Task[];
columns: Column[];
draggedTask: string | null;
};
export type Actions = {
addTask: (title: string, description?: string) => void;
addCol: (title: string) => void;
dragTask: (id: string | null) => void;
removeTask: (title: string) => void;
removeCol: (id: UniqueIdentifier) => void;
setTasks: (updatedTask: Task[]) => void;
setCols: (cols: Column[]) => void;
updateCol: (id: UniqueIdentifier, newName: string) => void;
};
export const useTaskStore = create<State & Actions>()(
persist(
(set) => ({
tasks: [],
columns: defaultCols,
draggedTask: null,
addTask: (title: string, description?: string) =>
set((state) => ({
tasks: [
...state.tasks,
{ id: uuid(), title, description, status: "TODO" },
],
})),
updateCol: (id: UniqueIdentifier, newName: string) =>
set((state) => ({
columns: state.columns.map((col) =>
col.id === id ? { ...col, title: newName } : col,
),
})),
addCol: (title: string) =>
set((state) => ({
columns: [...state.columns, { id: uuid(), title }],
})),
dragTask: (id: string | null) => set({ draggedTask: id }),
removeTask: (id: string) =>
set((state) => ({
tasks: state.tasks.filter((task) => task.id !== id),
})),
removeCol: (id: UniqueIdentifier) =>
set((state) => ({
columns: state.columns.filter((col) => col.id !== id),
})),
setTasks: (newTasks: Task[]) => set({ tasks: newTasks }),
setCols: (newCols: Column[]) => set({ columns: newCols }),
}),
{ name: "task-store", skipHydration: true },
),
);