Browse Source

FIX | Clear executors list after task created

fix/selected-executors
Oksana Stepanenko 2 years ago
parent
commit
e9a34a1cfa
  1. 51
      src/modules/tasks/hooks/use-create-update-task.hook.ts
  2. 1
      src/modules/tasks/screens/add-update-task.screen.tsx

51
src/modules/tasks/hooks/use-create-update-task.hook.ts

@ -1,7 +1,14 @@ @@ -1,7 +1,14 @@
import { StackNavigationProp } from '@react-navigation/stack'
import { useNavigation } from '@react-navigation/native'
import { taskDocumentsService, tasksService } from '@/services/domain'
import { appEvents, getMessageByExceptionKey, IFile, IShortUser, RouteKey, useForm } from '@/shared'
import {
appEvents,
getMessageByExceptionKey,
IFile,
IShortUser,
RouteKey,
useForm,
} from '@/shared'
import { simpleDispatch } from '@/store/store-helpers'
import { UnselectAllUsers } from '@/store/users'
import { useEffect, useMemo, useState } from 'react'
@ -33,6 +40,11 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => { @@ -33,6 +40,11 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => {
const [attachmentsIdsToRemove, setAttachmentsIdsToRemove] = useState<
number[]
>([])
const [selectedExecutors, setSelectedExecutors] = useState<IShortUser[]>([])
const selectedExecutorIds = useMemo(
() => selectedExecutors.map(({ id }) => id),
[selectedExecutors],
)
const nav = useNavigation<TNav>()
@ -41,15 +53,16 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => { @@ -41,15 +53,16 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => {
[TaskEditMode.Update]: (data: any) =>
tasksService.updateTask(taskId, data),
[TaskEditMode.Copy]: (data: any) =>
tasksService.create({ ...data, executorsId: [data.executorId], copyOfTaskId: taskId, copyDocumentsIds: task.documents?.map(it => it.id).filter(it => !_.includes(attachmentsIdsToRemove, it)) }),
tasksService.create({
...data,
executorsId: [data.executorId],
copyOfTaskId: taskId,
copyDocumentsIds: task.documents
?.map(it => it.id)
.filter(it => !_.includes(attachmentsIdsToRemove, it)),
}),
}
const [selectedExecutors, setSelectedExecutors] = useState<IShortUser[]>([])
const selectedExecutorIds = useMemo(
() => selectedExecutors.map(({ id }) => id),
[selectedExecutors],
)
const { taskDetails: task } = useTaskDetails(taskId)
useEffect(() => {
@ -158,16 +171,19 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => { @@ -158,16 +171,19 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => {
setLoading(true)
try {
const newTaskIds = await taskRequests[mode](values)
const taskIdsToAddDocuments = mode === TaskEditMode.Update ? [task.id] : newTaskIds
const taskIdsToAddDocuments =
mode === TaskEditMode.Update ? [task.id] : newTaskIds
setTimeout(async () => {
if (attachmentsToAdd.length)
await Promise.all(
taskIdsToAddDocuments?.forEach(async (taskId: number) => {
await taskDocumentsService.add({
taskId,
files: attachmentsToAdd,
})
})
taskIdsToAddDocuments?.forEach(
async (taskId: number) => {
await taskDocumentsService.add({
taskId,
files: attachmentsToAdd,
})
},
),
)
if (attachmentsIdsToRemove.length && mode !== TaskEditMode.Copy)
await Promise.all(
@ -178,7 +194,7 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => { @@ -178,7 +194,7 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => {
}, 200)
appEvents.emit('onTaskCreated', { executorsId: values.executorsId })
simpleDispatch(new UnselectAllUsers())
setSelectedExecutors([])
onSuccess()
} catch (e: any) {
console.log(e)
@ -195,7 +211,6 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => { @@ -195,7 +211,6 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => {
setFormField('executorsId', selectedExecutorIds)
}, [selectedExecutorIds])
return {
values,
errors,
@ -205,6 +220,6 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => { @@ -205,6 +220,6 @@ export const useCreateUpdateTask = (mode: TaskEditMode, taskId?: number) => {
onSubmit: () => onSubmit(handleSubmit),
task,
selectedExecutors,
setSelectedExecutors
setSelectedExecutors,
}
}

1
src/modules/tasks/screens/add-update-task.screen.tsx

@ -166,6 +166,7 @@ export const AddUpdateTaskScreen: FC<IProps> = ({ route, navigation }) => { @@ -166,6 +166,7 @@ export const AddUpdateTaskScreen: FC<IProps> = ({ route, navigation }) => {
})
simpleDispatch(new UnselectAllUsers())
simpleDispatch(new UnselectExecutor())
setSelectedExecutors([])
}
const submit = async () => {

Loading…
Cancel
Save