Skip to content

Commit

Permalink
refactor/use array of tasks instead of key value
Browse files Browse the repository at this point in the history
  • Loading branch information
udovichenk0 committed Mar 4, 2024
1 parent 725b52c commit 974762b
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 346 deletions.
4 changes: 2 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"dependencies": {
"@farfetched/core": "^0.11.1",
"@farfetched/zod": "^0.11.1",
"atomic-router": "^0.9.0",
"atomic-router-react": "^0.9.0",
"atomic-router": "^0.10.1",
"atomic-router-react": "^0.10.0",
"class-variance-authority": "^0.6.0",
"clsx": "^1.2.1",
"dayjs": "^1.11.7",
Expand Down
2 changes: 1 addition & 1 deletion client/src/entities/task/task-item/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export { Sort } from "./ui/task-sorting"
export { TaskSortingPopup as TaskSorting } from "./ui/task-sorting"
export * from "./model/task.model"
export * from "./model/sorting.model"
export type { Task, SortType, SortConfig, TaskKv } from "./type"
export type { Task, SortType, SortConfig } from "./type"
export * from "./config"
27 changes: 7 additions & 20 deletions client/src/entities/task/task-item/lib.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import { TaskId, TaskType } from "@/shared/api/task"

import { Task, TaskKv } from "./type"
import { Task } from "./type"
import { TaskTypes } from "./config"

export const removeTaskFromKv = (kv: TaskKv, taskId: TaskId) => {
if (!kv) return null
const updatedTasks = Object.entries(kv).filter(([key]) => key !== taskId)
return Object.fromEntries(updatedTasks)
}
export const removeTasksFromKvByFilter = (func: (task: Task) => boolean) => {
return (kv: TaskKv) => {
const updated = Object.entries(kv).filter(([_, value]) => func(value))
return Object.fromEntries(updated)
}
}
export const tasksNotNull = (tasks: Nullable<Task[]>): tasks is Task[] => tasks != null

export const transformTasksToKv = (tasks: Task[]) => {
return tasks.reduce((kv, task) => ({ ...kv, [task.id]: task }), {})
}

export const addTaskToKv = (kv: TaskKv, task: Task) => {
if (!kv) return null
return { ...kv, [task.id]: task }
}
export const findTaskById = (tasks: Task[], id: TaskId) => tasks.find((task) => task.id === id)!

export const switchTaskType = (type: TaskType, date?: Date): TaskType => {
switch (true) {
Expand All @@ -34,3 +17,7 @@ export const switchTaskType = (type: TaskType, date?: Date): TaskType => {
return TaskTypes.INBOX
}
}

export function deleteById(tasks: Task[], deletedTaskId: TaskId){
return tasks.filter((task) => task.id != deletedTaskId)
}
66 changes: 32 additions & 34 deletions client/src/entities/task/task-item/model/task.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,72 @@ import { TaskId, taskApi } from "@/shared/api/task"
import { singleton } from "@/shared/lib/effector/singleton"
import { createIdModal, createModal } from "@/shared/lib/modal"

import { Task, TaskKv } from "../type"
import { addTaskToKv, removeTaskFromKv, removeTasksFromKvByFilter, transformTasksToKv } from "../lib"
import { Task } from "../type"
import { deleteById, tasksNotNull } from "../lib"

export const $$dateModal = createModal({})
export const $$modal = createIdModal()

export const $$task = singleton(() => {
const $taskKv = createStore<Nullable<TaskKv>>(null)
const setTaskKvTriggered = createEvent<Task[]>()
const $tasks = createStore<Nullable<Task[]>>(null)
const setTaskTriggered = createEvent<Task>()
const setTasksTriggered = createEvent<Task[]>()
const getTasksTriggered = createEvent()
const taskDeleted = createEvent<TaskId>()
const trashTasksDeleted = createEvent()
const reset = createEvent()
const init = createEvent()

const listenFx = createEffect(() => {
const setTaskKvTriggeredScoped = scopeBind(setTaskKvTriggered)
const setTaskTriggeredScoped = scopeBind(setTasksTriggered)
addEventListener('storage', (event) => {
if(event.newValue && event.key == 'tasks'){
setTaskKvTriggeredScoped(JSON.parse(event.newValue))
setTaskTriggeredScoped(JSON.parse(event.newValue))
}
})
})

//tasks
sample({
clock: taskApi.getTasksQuery.finished.success,
fn: ({ result }) => transformTasksToKv(result),
target: $taskKv,
fn: ({ result }) => result,
target: $tasks,
})
sample({
clock: setTaskTriggered,
source: $taskKv,
filter: (kv: Nullable<TaskKv>): kv is TaskKv => !!kv,
fn: addTaskToKv,
target: $taskKv,
source: $tasks,
filter: tasksNotNull,
fn: (oldTasks, newTask) => [...oldTasks, newTask],
target: $tasks,
})
sample({
clock: taskDeleted,
source: $taskKv,
filter: (kv: Nullable<TaskKv>): kv is TaskKv => !!kv,
fn: removeTaskFromKv,
target: $taskKv,
clock: setTasksTriggered,
target: $tasks
})
sample({
clock: trashTasksDeleted,
source: $taskKv,
filter: (kv: Nullable<TaskKv>): kv is TaskKv => !!kv,
fn: removeTasksFromKvByFilter((task) => !task.is_deleted),
target: $taskKv
clock: taskDeleted,
source: $tasks,
filter: tasksNotNull,
fn: deleteById,
target: $tasks,
})
sample({
clock: setTaskKvTriggered,
fn: transformTasksToKv,
target: $taskKv,
clock: trashTasksDeleted,
source: $tasks,
filter: tasksNotNull,
fn: (tasks) => tasks.filter(task => !task.is_deleted),
target: $tasks
})
sample({

sample({ //!move to create task model
clock: taskApi.createTasksQuery.finished.success,
fn: ({result}) => result,
target: [setTaskKvTriggered, taskApi.deleteTasksFromLocalStorageFx],
target: [$tasks, taskApi.deleteTasksFromLocalStorageFx],
})

sample({
clock: taskApi.getTasksFromLocalStorageFx.doneData,
filter: not($$session.$isAuthenticated),
target: setTaskKvTriggered,
target: $tasks,
})

sample({
Expand All @@ -81,23 +82,20 @@ export const $$task = singleton(() => {

sample({
clock: reset,
target: $taskKv.reinit!,
target: $tasks.reinit,
})

sample({
clock: init,
target: listenFx
})
return {
$taskKv: $taskKv.map((kv) => kv),
$tasks,
setTaskTriggered,
reset,
getTasksTriggered,
trashTasksDeleted,
taskDeleted,
init,
_: {
$taskKv,
},
}
})
Loading

0 comments on commit 974762b

Please sign in to comment.