Update ownership error check
[arvados-workbench2.git] / src / views-components / update-project-dialog / update-project-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from "react-redux";
6 import { Dispatch } from "redux";
7 import { SubmissionError } from "redux-form";
8 import { RootState } from "~/store/store";
9 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
10 import { DialogProjectUpdate } from "../dialog-update/dialog-project-update";
11 import { projectActions, updateProject } from "~/store/project/project-action";
12
13 const mapStateToProps = (state: RootState) => ({
14     open: state.projects.updater.opened
15 });
16
17 const mapDispatchToProps = (dispatch: Dispatch) => ({
18     handleClose: () => {
19         dispatch(projectActions.CLOSE_PROJECT_UPDATER());
20     },
21     onSubmit: (data: { name: string, description: string }) => {
22         return dispatch<any>(editProject(data))
23             .catch((e: any) => {
24                 if (e.errors) {
25                     throw new SubmissionError({ name: e.errors.join("").includes("UniqueViolation") ? "Project with this name already exists." : "" });
26                 }
27             });
28     }
29 });
30
31 const editProject = (data: { name: string, description: string }) =>
32     (dispatch: Dispatch, getState: () => RootState) => {
33         const { uuid } = getState().projects.updater;
34         return dispatch<any>(updateProject(data)).then(() => {
35             dispatch(snackbarActions.OPEN_SNACKBAR({
36                 message: "Project has been successfully updated.",
37                 hideDuration: 2000
38             }));
39         });
40     };
41
42 export const UpdateProjectDialog = connect(mapStateToProps, mapDispatchToProps)(DialogProjectUpdate);