Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
udovichenk0 committed May 2, 2024
1 parent 6a4eeb5 commit 62ad1bf
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 84 deletions.
4 changes: 4 additions & 0 deletions client/src/entities/task/task-item/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ export const switchTaskType = (type: TaskType, date?: Date): TaskType => {
export function deleteById(tasks: Task[], deletedTaskId: TaskId) {
return tasks.filter((task) => task.id != deletedTaskId)
}

export const isUnplaced = (task: Task) => task.type == "unplaced"

export const isInbox = (task: Task) => task.type == "inbox"
61 changes: 33 additions & 28 deletions client/src/entities/task/task-item/model/task.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ export const taskFactory = ({
const setTasksTriggered = createEvent<Task[]>()
const taskDeleted = createEvent<TaskId>()
const reset = createEvent()
sample({
clock: [api.taskQuery.finished.success, api.taskStorage.doneData],
fn: ({ result }) => result,
target: [$tasks, prepend($isInited, true)],
})

sample({
clock: addTaskTriggered,
Expand All @@ -52,6 +47,14 @@ export const taskFactory = ({
fn: (oldTasks, newTask) => [...oldTasks, newTask],
target: $tasks,
})

sample({
clock: taskDeleted,
source: $tasks,
filter: tasksNotNull,
fn: deleteById,
target: $tasks,
})
sample({
clock: [
taskApi.createTaskMutation.finished.success,
Expand All @@ -62,6 +65,11 @@ export const taskFactory = ({
target: addTaskTriggered,
})

sample({
clock: [api.taskQuery.finished.success, api.taskStorage.doneData],
fn: ({ result }) => result,
target: [$tasks, prepend($isInited, true)],
})
sample({
clock: [
taskApi.updateStatusMutation.finished.success,
Expand Down Expand Up @@ -95,15 +103,29 @@ export const taskFactory = ({
fn: ({ result }) => result.id,
target: taskDeleted,
})

sample({
clock: [
taskApi.trashTaskMutation.finished.success,
taskApi.trashTaskLs.doneData,
],
filter: ({ result }) => filter(result),
fn: ({ result }) => result.id,
target: taskDeleted,
})

sample({
clock: taskApi.createTasksMutation.finished.success,
filter: route.$isOpened,
target: api.taskQuery.start,
})
sample({
clock: setTasksTriggered,
target: $tasks,
})
sample({
clock: taskDeleted,
source: $tasks,
filter: tasksNotNull,
fn: deleteById,
clock: authApi.logoutQuery.finished.success,
fn: () => [],
target: $tasks,
})

Expand All @@ -119,20 +141,13 @@ export const taskFactory = ({
target: $tasks,
})
}
sample({
clock: [
taskApi.trashTaskMutation.finished.success,
taskApi.trashTaskLs.doneData,
],
filter: ({ result }) => filter(result),
fn: ({ result }) => result.id,
target: taskDeleted,
})

sample({
clock: [route.opened, $$session.$isAuthenticated],
filter: and(not($isInited), $$session.$isAuthenticated, route.$isOpened),
target: api.taskQuery.start,
})

sample({
clock: [route.opened, tokenApi.refreshQuery.finished.failure],
filter: and(
Expand All @@ -143,16 +158,6 @@ export const taskFactory = ({
),
target: api.taskStorage,
})
sample({
clock: taskApi.createTasksMutation.finished.success,
filter: route.$isOpened,
target: api.taskQuery.start,
})
sample({
clock: authApi.logoutQuery.finished.success,
fn: () => [],
target: $tasks,
})

return {
$tasks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createRoute } from "atomic-router"

import { $$session } from "@/entities/session"
import { TaskFactory, taskFactory } from "@/entities/task/task-item"
import { isInbox } from "@/entities/task/task-item/lib"

import { taskApi } from "@/shared/api/task"

Expand Down Expand Up @@ -62,7 +63,7 @@ describe("create task", () => {
let $$createTask: CreateTaskFactory
beforeEach(() => {
$$mockTasks = taskFactory({
filter: ({ is_deleted, type }) => !is_deleted && type == "inbox",
filter: isInbox,
api: {
taskQuery: taskApi.inboxTasksQuery,
taskStorage: taskApi.inboxTasksLs,
Expand Down
3 changes: 3 additions & 0 deletions client/src/features/manage-task/model/delete/task.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const removeTaskFactory = (taskModel: TaskFactory) => {
taskApi.deleteTaskLs.doneData,
taskApi.deleteTaskMutation.finished.success,
])

//if logged in
sample({
clock: taskDeletedById,
filter: $$session.$isAuthenticated,
Expand All @@ -24,6 +26,7 @@ export const removeTaskFactory = (taskModel: TaskFactory) => {
target: taskApi.deleteAllTasksMutation.start,
})

//if not logged in
sample({
clock: taskDeletedById,
filter: not($$session.$isAuthenticated),
Expand Down
3 changes: 2 additions & 1 deletion client/src/pages/Calendar/calendar.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { updateTaskFactory } from "@/features/manage-task/model/update"
import { trashTaskFactory } from "@/features/manage-task/model/trash"

import { taskFactory, Task } from "@/entities/task/task-item"
import { isUnplaced } from "@/entities/task/task-item/lib"

import { createModal } from "@/shared/lib/modal"
import { bridge } from "@/shared/lib/effector/bridge"
Expand All @@ -20,7 +21,7 @@ export const $$dateModal = createModal({})
export const $$trashTask = trashTaskFactory()

const $unplacedTasks = taskFactory({
filter: ({ type }) => type == "unplaced",
filter: isUnplaced,
route: calendarRoute,
api: {
taskQuery: taskApi.unplacedTasksQuery,
Expand Down
21 changes: 7 additions & 14 deletions client/src/pages/Inbox/inbox.model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { sample } from "effector"

import { disclosureTask } from "@/widgets/expanded-task/model"

import { updateTaskFactory } from "@/features/manage-task/model/update"
import { trashTaskFactory } from "@/features/manage-task/model/trash"
import { createTaskFactory } from "@/features/manage-task/model/create"

import { taskFactory, createSorting, Task } from "@/entities/task/task-item"
import { taskFactory, createSorting } from "@/entities/task/task-item"
import { isInbox } from "@/entities/task/task-item/lib"

import { selectTaskFactory } from "@/shared/lib/effector"
import { routes } from "@/shared/routing"
Expand All @@ -21,7 +20,7 @@ export const $$sort = createSorting()

export const $inboxTasks = taskFactory({
sortModel: $$sort,
filter: inboxFilter,
filter: isInbox,
route: inboxRoute,
api: {
taskQuery: taskApi.inboxTasksQuery,
Expand All @@ -41,13 +40,7 @@ export const $$taskDisclosure = disclosureTask({
createTaskModel: $$createTask,
})

export const $$selectTask = selectTaskFactory($inboxTasks.$tasks)

sample({
clock: $$trashTask.taskTrashedById,
target: $$selectTask.selectNextId,
})

function inboxFilter(task: Task) {
return task.type == "inbox"
}
export const $$selectTask = selectTaskFactory(
$inboxTasks.$tasks,
$$trashTask.taskTrashedById,
)
19 changes: 10 additions & 9 deletions client/src/pages/Today/today.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { updateTaskFactory } from "@/features/manage-task/model/update"
import { trashTaskFactory } from "@/features/manage-task/model/trash"

import { taskFactory, createSorting } from "@/entities/task/task-item"
import { isUnplaced } from "@/entities/task/task-item/lib"

import { cookiePersist } from "@/shared/lib/storage/cookie-persist"
import { selectTaskFactory } from "@/shared/lib/effector"
import { taskApi } from "@/shared/api/task"
import { routes } from "@/shared/routing"
import { createIdModal, createModal } from "@/shared/lib/modal"
import { isToday } from "@/shared/lib/date"

export const homeRoute = routes.home
export const $$dateModal = createModal({})
Expand All @@ -33,18 +35,18 @@ export const $$sort = createSorting()

export const $todayTasks = taskFactory({
sortModel: $$sort,
filter: (task) =>
task.type == "unplaced" && dayjs(new Date(task.start_date!)).isToday(),
filter: (task) => isUnplaced(task) && isToday(task.start_date),
route: homeRoute,
api: {
taskQuery: taskApi.todayTasksQuery,
taskStorage: taskApi.todayTasksLs,
},
})

export const $overdueTasks = taskFactory({
sortModel: $$sort,
filter: (task) =>
task.type == "unplaced" &&
isUnplaced(task) &&
dayjs(new Date(task.start_date!)).isBefore(dayjs().startOf("date")),
route: homeRoute,
api: {
Expand All @@ -60,7 +62,10 @@ const $tasks = combine(
return [todayTasks || [], overdueTasks || []]
},
)
export const $$selectTask = selectTaskFactory($tasks)
export const $$selectTask = selectTaskFactory(
$tasks,
$$trashTask.taskTrashedById,
)

export const $isOverdueTasksOpened = createStore(false)
export const toggleOverdueTasksOpened = createEvent()
Expand All @@ -71,12 +76,8 @@ sample({
fn: (isOpened) => !isOpened,
target: $isOverdueTasksOpened,
})

cookiePersist({
source: $isOverdueTasksOpened,
name: "overdueTasksOpened",
})

sample({
clock: $$trashTask.taskTrashedById,
target: $$selectTask.selectNextId,
})
12 changes: 4 additions & 8 deletions client/src/pages/Trash/trash.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { sample } from "effector"

import { removeTaskFactory } from "@/features/manage-task"

import { taskFactory } from "@/entities/task/task-item"
Expand All @@ -22,9 +20,7 @@ export const $trashTasks = taskFactory({
})
export const $$deleteTask = removeTaskFactory($trashTasks)

export const $$selectTask = selectTaskFactory($trashTasks.$tasks)

sample({
clock: $$deleteTask.taskDeletedById,
target: $$selectTask.selectNextId,
})
export const $$selectTask = selectTaskFactory(
$trashTasks.$tasks,
$$deleteTask.taskDeletedById,
)
16 changes: 7 additions & 9 deletions client/src/pages/Unplaced/unplaced.model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { sample } from "effector"

import { disclosureTask } from "@/widgets/expanded-task/model"

import { createTaskFactory } from "@/features/manage-task/model/create"
import { updateTaskFactory } from "@/features/manage-task/model/update"
import { trashTaskFactory } from "@/features/manage-task/model/trash"

import { taskFactory, createSorting } from "@/entities/task/task-item"
import { isUnplaced } from "@/entities/task/task-item/lib"

import { selectTaskFactory } from "@/shared/lib/effector"
import { taskApi } from "@/shared/api/task"
Expand All @@ -19,10 +18,11 @@ export const $$dateModal = createModal({})
export const $$idModal = createIdModal()

export const $$sort = createSorting()

export const $unplacedTasks = taskFactory({
sortModel: $$sort,
route: unplacedRoute,
filter: ({ type }) => type == "unplaced",
filter: isUnplaced,
api: {
taskQuery: taskApi.unplacedTasksQuery,
taskStorage: taskApi.unplacedTasksLs,
Expand All @@ -42,9 +42,7 @@ export const $$taskDisclosure = disclosureTask({
updateTaskModel: $$updateTask,
createTaskModel: $$createTask,
})
export const $$selectTask = selectTaskFactory($unplacedTasks.$tasks)

sample({
clock: $$trashTask.taskTrashedById,
target: $$selectTask.selectNextId,
})
export const $$selectTask = selectTaskFactory(
$unplacedTasks.$tasks,
$$trashTask.taskTrashedById,
)
15 changes: 7 additions & 8 deletions client/src/pages/Upcoming/upcoming.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { updateTaskFactory } from "@/features/manage-task/model/update"
import { trashTaskFactory } from "@/features/manage-task/model/trash"

import { taskFactory, Task, createSorting } from "@/entities/task/task-item"
import { isUnplaced } from "@/entities/task/task-item/lib"

import { TaskId, taskApi } from "@/shared/api/task"
import { selectTaskFactory } from "@/shared/lib/effector"
Expand All @@ -31,14 +32,13 @@ export const upcomingRoute = routes.upcoming
//factories

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

export const $$sort = createSorting()
export const $$idModal = createIdModal()

export const $upcomingTasks = taskFactory({
sortModel: $$sort,
route: upcomingRoute,
filter: ({ type }) => type == "unplaced",
filter: isUnplaced,
api: {
taskQuery: taskApi.upcomingTasksQuery,
taskStorage: taskApi.upcomingTasksLs,
Expand Down Expand Up @@ -127,7 +127,10 @@ const $sectionTasks = combine(
}
},
)
export const $$selectTask = selectTaskFactory($sectionTasks)
export const $$selectTask = selectTaskFactory(
$sectionTasks,
$$trashTask.taskTrashedById,
)

sample({
clock: selectTaskIdWithSectionTitle,
Expand Down Expand Up @@ -176,7 +179,3 @@ sample({
},
target: $selectedDate,
})
sample({
clock: $$trashTask.taskTrashedById,
target: $$selectTask.selectNextId,
})
Loading

0 comments on commit 62ad1bf

Please sign in to comment.