Skip to content

Commit

Permalink
feat: task CRUD 관련 Actions 정의(#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoonncho committed Mar 2, 2023
1 parent ad40f97 commit 9944547
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/store/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const INITIAL_STATE: State['blockList'] = {

type Actions = {
setBlockList: (data: State['blockList']) => void
addNewTask: (blockId: number) => void
updateTask: (blockId: number, taskId: number, content: string) => void
updateTaskStatus: (blockId: number, taskId: number, isDone: boolean) => void
deleteTask: (blockId: number, taskId: number) => void
}

const useBlockListStore = create(
Expand All @@ -25,6 +29,54 @@ const useBlockListStore = create(
set((state) => {
state.blockList = data
}),
addNewTask: (blockId) =>
set((state) => {
const blockIndex = state.blockList.blocks.findIndex(
(block) => block.blockId === blockId,
)
state.blockList.blocks[blockIndex].tasks.push({
taskId: state.blockList.blocks[blockIndex].tasks.length + 1,
task: '',
isDone: false,
})
state.blockList.blocks[blockIndex].sumOfTask += 1
state.blockList.totalTask += 1
}),
updateTask: (blockId, taskId, content) =>
set((state) => {
const blockIndex = state.blockList.blocks.findIndex(
(block) => block.blockId === blockId,
)
const taskIndex = state.blockList.blocks[blockIndex].tasks.findIndex(
(task) => task.taskId === taskId,
)
state.blockList.blocks[blockIndex].tasks[taskIndex].task = content
}),
updateTaskStatus: (blockId, taskId, isDone) =>
set((state) => {
const blockIndex = state.blockList.blocks.findIndex(
(block) => block.blockId === blockId,
)
const taskIndex = state.blockList.blocks[blockIndex].tasks.findIndex(
(task) => task.taskId === taskId,
)
state.blockList.blocks[blockIndex].tasks[taskIndex].isDone = isDone
if (isDone) state.blockList.blocks[blockIndex].sumOfDoneTask += 1
else state.blockList.blocks[blockIndex].sumOfDoneTask -= 1
}),
deleteTask: (blockId, taskId) => {
set((state) => {
const blockIndex = state.blockList.blocks.findIndex(
(block) => block.blockId === blockId,
)
const taskIndex = state.blockList.blocks[blockIndex].tasks.findIndex(
(task) => task.taskId === taskId,
)
state.blockList.blocks[blockIndex].tasks.splice(taskIndex, 1)
state.blockList.blocks[blockIndex].sumOfTask -= 1
state.blockList.totalTask -= 1
})
},
})),
)

Expand Down

0 comments on commit 9944547

Please sign in to comment.