|
|
|
@ -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, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|