From 55dc510728e33bce50fa5576b3896434b7d58521 Mon Sep 17 00:00:00 2001 From: Zabilsya Date: Tue, 10 Dec 2024 13:30:40 +0600 Subject: [PATCH] [DOP-22068] create run --- src/entities/run/api/hooks/index.ts | 1 + .../run/api/hooks/useCreateRun/index.ts | 24 +++++++++++++++ src/entities/run/api/runService.ts | 6 +++- src/entities/run/api/types.ts | 4 +++ src/features/run/CreateRun/index.tsx | 30 +++++++++++++++++++ src/features/run/CreateRun/styles.module.less | 20 +++++++++++++ src/features/run/CreateRun/types.ts | 6 ++++ src/features/run/index.ts | 1 + .../components/CreateRunModal/index.tsx | 7 ++--- .../components/CreateRunButton/index.tsx | 14 ++++----- 10 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 src/entities/run/api/hooks/useCreateRun/index.ts create mode 100644 src/features/run/CreateRun/index.tsx create mode 100644 src/features/run/CreateRun/styles.module.less create mode 100644 src/features/run/CreateRun/types.ts diff --git a/src/entities/run/api/hooks/index.ts b/src/entities/run/api/hooks/index.ts index 5f209060..582dd1de 100644 --- a/src/entities/run/api/hooks/index.ts +++ b/src/entities/run/api/hooks/index.ts @@ -1 +1,2 @@ export * from './useGetRun'; +export * from './useCreateRun'; diff --git a/src/entities/run/api/hooks/useCreateRun/index.ts b/src/entities/run/api/hooks/useCreateRun/index.ts new file mode 100644 index 00000000..78ecd255 --- /dev/null +++ b/src/entities/run/api/hooks/useCreateRun/index.ts @@ -0,0 +1,24 @@ +import { useMutation, UseMutationResult, useQueryClient } from '@tanstack/react-query'; +import { notification } from 'antd'; +import { getErrorMessage } from '@shared/config'; + +import { runService } from '../../runService'; +import { CreateRunRequest } from '../../types'; +import { RunQueryKey } from '../../keys'; + +/** Hook for creating run (run transfer) */ +export const useCreateRun = (data: CreateRunRequest): UseMutationResult => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: () => runService.createRun(data), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: [RunQueryKey.GET_RUNS, data.transfer_id] }); + }, + onError: (error) => { + notification.error({ + message: getErrorMessage(error), + }); + }, + }); +}; diff --git a/src/entities/run/api/runService.ts b/src/entities/run/api/runService.ts index f7f5220a..39985a3e 100644 --- a/src/entities/run/api/runService.ts +++ b/src/entities/run/api/runService.ts @@ -1,7 +1,7 @@ import { axiosInstance } from '@shared/config'; import { PaginationResponse } from '@shared/types'; -import { GetRunRequest, Run, GetRunsRequest } from './types'; +import { GetRunRequest, Run, GetRunsRequest, CreateRunRequest } from './types'; export const runService = { getRuns: (params: GetRunsRequest): Promise> => { @@ -11,4 +11,8 @@ export const runService = { getRun: ({ id }: GetRunRequest): Promise => { return axiosInstance.get(`runs/${id}`); }, + + createRun: (data: CreateRunRequest): Promise => { + return axiosInstance.post('runs', data); + }, }; diff --git a/src/entities/run/api/types.ts b/src/entities/run/api/types.ts index a138ad9d..bbb68bed 100644 --- a/src/entities/run/api/types.ts +++ b/src/entities/run/api/types.ts @@ -18,3 +18,7 @@ export interface GetRunsRequest extends PaginationRequest { export interface GetRunRequest { id: number; } + +export interface CreateRunRequest { + transfer_id: number; +} diff --git a/src/features/run/CreateRun/index.tsx b/src/features/run/CreateRun/index.tsx new file mode 100644 index 00000000..96471dc5 --- /dev/null +++ b/src/features/run/CreateRun/index.tsx @@ -0,0 +1,30 @@ +import React from 'react'; +import { ControlButtons } from '@shared/ui'; +import { Typography } from 'antd'; +import { WarningOutlined } from '@ant-design/icons'; +import { useCreateRun } from '@entities/run'; + +import classes from './styles.module.less'; +import { CreateRunProps } from './types'; + +const { Text } = Typography; + +export const CreateRun = ({ transferId, transferName, onSuccess, onCancel }: CreateRunProps) => { + const { mutate: createRun, isPending } = useCreateRun({ transfer_id: transferId }); + + const handleSubmit = () => { + createRun(null, { onSuccess }); + }; + + return ( +
+
+ + + Do you really want to run transfer «{transferName}»? + +
+ +
+ ); +}; diff --git a/src/features/run/CreateRun/styles.module.less b/src/features/run/CreateRun/styles.module.less new file mode 100644 index 00000000..4341ad51 --- /dev/null +++ b/src/features/run/CreateRun/styles.module.less @@ -0,0 +1,20 @@ +.root { + display: flex; + flex-direction: column; + gap: 24px; + + .main { + display: flex; + align-items: flex-start; + gap: 24px; + + .icon { + color: @red-6; + + svg { + width: 24px; + height: 24px; + } + } + } +} diff --git a/src/features/run/CreateRun/types.ts b/src/features/run/CreateRun/types.ts new file mode 100644 index 00000000..d96a8489 --- /dev/null +++ b/src/features/run/CreateRun/types.ts @@ -0,0 +1,6 @@ +export interface CreateRunProps { + transferId: number; + transferName: string; + onSuccess: () => void; + onCancel: () => void; +} diff --git a/src/features/run/index.ts b/src/features/run/index.ts index f211f80d..e3c247e5 100644 --- a/src/features/run/index.ts +++ b/src/features/run/index.ts @@ -1,2 +1,3 @@ export * from './RunList'; export * from './RunDetailInfo'; +export * from './CreateRun'; diff --git a/src/widgets/run/TransferRuns/components/CreateRunButton/components/CreateRunModal/index.tsx b/src/widgets/run/TransferRuns/components/CreateRunButton/components/CreateRunModal/index.tsx index 45be59d8..cb1f2c6a 100644 --- a/src/widgets/run/TransferRuns/components/CreateRunButton/components/CreateRunModal/index.tsx +++ b/src/widgets/run/TransferRuns/components/CreateRunButton/components/CreateRunModal/index.tsx @@ -1,14 +1,13 @@ import React from 'react'; -import { DEFAULT_MODAL_WIDTH } from '@shared/constants'; import { ModalWrapper } from '@shared/ui'; +import { CreateRun } from '@features/run'; import { CreateRunModalProps } from './types'; export const CreateRunModal = ({ transferId, transferName, onClose, ...props }: CreateRunModalProps) => { return ( - - {/* //TODO: [DOP-20067] add create run modal */} - {/* */} + + ); }; diff --git a/src/widgets/run/TransferRuns/components/CreateRunButton/index.tsx b/src/widgets/run/TransferRuns/components/CreateRunButton/index.tsx index f815eb25..0a33d597 100644 --- a/src/widgets/run/TransferRuns/components/CreateRunButton/index.tsx +++ b/src/widgets/run/TransferRuns/components/CreateRunButton/index.tsx @@ -7,22 +7,18 @@ import classes from './styles.module.less'; import { CreateRunButtonProps } from './types'; export const CreateRunButton = memo(({ transferId, transferName }: CreateRunButtonProps) => { - const { - isOpened: isOpenedCreateRunModal, - handleOpen: handleOpenCreateRunModal, - handleClose: handleCloseCreateRunModal, - } = useModalState(); + const { isOpened: isOpenedModal, handleOpen: handleOpenModal, handleClose: handleCloseModal } = useModalState(); return ( <> - );