91 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-12-26 19:32:41 -08:00
import React, { useEffect } from "react";
2020-12-27 16:37:10 -08:00
import { useIntl } from "react-intl";
2020-12-26 19:32:41 -08:00
import { useMutation, useQueryClient } from "react-query";
import { useSelector } from "react-redux";
2021-01-27 17:27:19 -08:00
import { FormControl } from "@material-ui/core";
2021-02-01 22:15:32 -08:00
import Client, { BasicMapInfo, ErrorInfo } from "../../../../client";
2021-01-13 19:46:45 -08:00
import { activeInstance } from '../../../../reducers/serviceSlice';
import Input from "../../../form/input";
2021-01-14 01:14:07 -08:00
import { DialogProps, fetchMapById, handleOnMutationSuccess } from "..";
2021-01-13 19:46:45 -08:00
import BaseDialog from "../action-dialog";
2020-12-26 19:32:41 -08:00
2021-01-14 01:14:07 -08:00
export type DuplicateModel = {
2020-12-26 19:32:41 -08:00
id: number;
2021-02-04 10:01:35 -08:00
title: string;
2020-12-26 19:32:41 -08:00
description?: string;
}
2021-02-04 10:01:35 -08:00
const defaultModel: DuplicateModel = { title: '', description: '', id: -1 };
2021-01-14 01:14:07 -08:00
const DuplicateDialog = (props: DialogProps) => {
2021-02-01 22:15:32 -08:00
const service: Client = useSelector(activeInstance);
2021-01-14 01:14:07 -08:00
const [model, setModel] = React.useState<DuplicateModel>(defaultModel);
2020-12-26 19:32:41 -08:00
const [error, setError] = React.useState<ErrorInfo>();
const { mapId, open } = props;
const intl = useIntl();
const queryClient = useQueryClient();
2021-02-04 10:01:35 -08:00
const mutation = useMutation<number, ErrorInfo, DuplicateModel>((model: DuplicateModel) => {
2020-12-26 19:32:41 -08:00
const { id, ...rest } = model;
2021-02-04 10:01:35 -08:00
return service.duplicateMap(id, rest);
2020-12-26 19:32:41 -08:00
},
{
onSuccess: () => {
handleOnMutationSuccess(props.onClose, queryClient);
},
onError: (error) => {
setError(error);
}
}
);
const handleOnClose = (): void => {
2020-12-27 16:37:10 -08:00
props.onClose();
2020-12-26 19:32:41 -08:00
setModel(defaultModel);
setError(undefined);
};
const handleOnSubmit = (event: React.FormEvent<HTMLFormElement>): void => {
event.preventDefault();
mutation.mutate(model);
};
const handleOnChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
event.preventDefault();
const name = event.target.name;
const value = event.target.value;
setModel({ ...model, [name as keyof BasicMapInfo]: value });
}
const { map } = fetchMapById(mapId);
useEffect(() => {
if (open && map) {
setModel(map);
} else {
setModel(defaultModel);
setError(undefined);
}
}, [mapId])
return (
<div>
2021-01-13 19:46:45 -08:00
<BaseDialog open={open} onClose={handleOnClose} onSubmit={handleOnSubmit} error={error}
2021-01-27 17:27:19 -08:00
title={intl.formatMessage({ id: 'duplicate.title', defaultMessage: 'Duplicate' })}
description={intl.formatMessage({ id: 'rename.description', defaultMessage: 'Please, fill the new map name and description.' })}
submitButton={intl.formatMessage({ id: 'duplicate.title', defaultMessage: 'Duplicate' })}>
2021-01-31 18:04:50 -08:00
2021-01-13 19:46:45 -08:00
<FormControl fullWidth={true}>
2021-02-04 10:01:35 -08:00
<Input name="title" type="text" label={{ id: "action.rename-name-placeholder", defaultMessage: "Name" }}
value={model.title} onChange={handleOnChange} error={error} fullWidth={true} />
2021-01-13 19:46:45 -08:00
<Input name="description" type="text" label={{ id: "action.rename-description-placeholder", defaultMessage: "Description" }}
value={model.description} onChange={handleOnChange} required={false} fullWidth={true} />
</FormControl>
</BaseDialog>
2021-01-31 18:04:50 -08:00
</div>
2020-12-26 19:32:41 -08:00
);
}
2021-01-14 01:14:07 -08:00
export default DuplicateDialog;