Skip to content

Commit 967fa7b

Browse files
ZabilsyaZabilsya
andauthored
[DOP-22068] create run (#49)
Co-authored-by: Zabilsya <[email protected]>
1 parent cd52572 commit 967fa7b

File tree

10 files changed

+99
-14
lines changed

10 files changed

+99
-14
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './useGetRun';
2+
export * from './useCreateRun';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useMutation, UseMutationResult, useQueryClient } from '@tanstack/react-query';
2+
import { notification } from 'antd';
3+
import { getErrorMessage } from '@shared/config';
4+
5+
import { runService } from '../../runService';
6+
import { CreateRunRequest } from '../../types';
7+
import { RunQueryKey } from '../../keys';
8+
9+
/** Hook for creating run (run transfer) */
10+
export const useCreateRun = (data: CreateRunRequest): UseMutationResult => {
11+
const queryClient = useQueryClient();
12+
13+
return useMutation({
14+
mutationFn: () => runService.createRun(data),
15+
onSuccess: () => {
16+
queryClient.invalidateQueries({ queryKey: [RunQueryKey.GET_RUNS, data.transfer_id] });
17+
},
18+
onError: (error) => {
19+
notification.error({
20+
message: getErrorMessage(error),
21+
});
22+
},
23+
});
24+
};

src/entities/run/api/runService.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { axiosInstance } from '@shared/config';
22
import { PaginationResponse } from '@shared/types';
33

4-
import { GetRunRequest, Run, GetRunsRequest } from './types';
4+
import { GetRunRequest, Run, GetRunsRequest, CreateRunRequest } from './types';
55

66
export const runService = {
77
getRuns: (params: GetRunsRequest): Promise<PaginationResponse<Run>> => {
@@ -11,4 +11,8 @@ export const runService = {
1111
getRun: ({ id }: GetRunRequest): Promise<Run> => {
1212
return axiosInstance.get(`runs/${id}`);
1313
},
14+
15+
createRun: (data: CreateRunRequest): Promise<Run> => {
16+
return axiosInstance.post('runs', data);
17+
},
1418
};

src/entities/run/api/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export interface GetRunsRequest extends PaginationRequest {
1818
export interface GetRunRequest {
1919
id: number;
2020
}
21+
22+
export interface CreateRunRequest {
23+
transfer_id: number;
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import { ControlButtons } from '@shared/ui';
3+
import { Typography } from 'antd';
4+
import { WarningOutlined } from '@ant-design/icons';
5+
import { useCreateRun } from '@entities/run';
6+
7+
import classes from './styles.module.less';
8+
import { CreateRunProps } from './types';
9+
10+
const { Text } = Typography;
11+
12+
export const CreateRun = ({ transferId, transferName, onSuccess, onCancel }: CreateRunProps) => {
13+
const { mutate: createRun, isPending } = useCreateRun({ transfer_id: transferId });
14+
15+
const handleSubmit = () => {
16+
createRun(null, { onSuccess });
17+
};
18+
19+
return (
20+
<div className={classes.root}>
21+
<div className={classes.main}>
22+
<WarningOutlined className={classes.icon} />
23+
<Text>
24+
Do you really want to run transfer <b>«{transferName}»</b>?
25+
</Text>
26+
</div>
27+
<ControlButtons isLoading={isPending} submitButtonText="Confirm" onSubmit={handleSubmit} onCancel={onCancel} />
28+
</div>
29+
);
30+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.root {
2+
display: flex;
3+
flex-direction: column;
4+
gap: 24px;
5+
6+
.main {
7+
display: flex;
8+
align-items: flex-start;
9+
gap: 24px;
10+
11+
.icon {
12+
color: @red-6;
13+
14+
svg {
15+
width: 24px;
16+
height: 24px;
17+
}
18+
}
19+
}
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface CreateRunProps {
2+
transferId: number;
3+
transferName: string;
4+
onSuccess: () => void;
5+
onCancel: () => void;
6+
}

src/features/run/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './RunList';
22
export * from './RunDetailInfo';
3+
export * from './CreateRun';
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import React from 'react';
2-
import { DEFAULT_MODAL_WIDTH } from '@shared/constants';
32
import { ModalWrapper } from '@shared/ui';
3+
import { CreateRun } from '@features/run';
44

55
import { CreateRunModalProps } from './types';
66

77
export const CreateRunModal = ({ transferId, transferName, onClose, ...props }: CreateRunModalProps) => {
88
return (
9-
<ModalWrapper title="Run transfer" width={DEFAULT_MODAL_WIDTH} onCancel={onClose} {...props}>
10-
{/* //TODO: [DOP-20067] add create run modal */}
11-
{/* <CreateRun transferId={transferId} transferName={transferName} onSuccess={onClose} onCancel={onClose} /> */}
9+
<ModalWrapper title="Run Transfer" width={400} onCancel={onClose} {...props}>
10+
<CreateRun transferId={transferId} transferName={transferName} onSuccess={onClose} onCancel={onClose} />
1211
</ModalWrapper>
1312
);
1413
};

src/widgets/run/TransferRuns/components/CreateRunButton/index.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@ import classes from './styles.module.less';
77
import { CreateRunButtonProps } from './types';
88

99
export const CreateRunButton = memo(({ transferId, transferName }: CreateRunButtonProps) => {
10-
const {
11-
isOpened: isOpenedCreateRunModal,
12-
handleOpen: handleOpenCreateRunModal,
13-
handleClose: handleCloseCreateRunModal,
14-
} = useModalState();
10+
const { isOpened: isOpenedModal, handleOpen: handleOpenModal, handleClose: handleCloseModal } = useModalState();
1511

1612
return (
1713
<>
18-
<Button className={classes.button} type="primary" size="large" onClick={handleOpenCreateRunModal}>
19-
Run transfer
14+
<Button className={classes.button} type="primary" size="large" onClick={handleOpenModal}>
15+
Run Transfer
2016
</Button>
2117
<CreateRunModal
2218
transferId={transferId}
2319
transferName={transferName}
24-
open={isOpenedCreateRunModal}
25-
onClose={handleCloseCreateRunModal}
20+
open={isOpenedModal}
21+
onClose={handleCloseModal}
2622
/>
2723
</>
2824
);

0 commit comments

Comments
 (0)