1374ac41bad7feb8f827b1055d63e5c56b50ff80
[arvados-workbench2.git] / src / views-components / update-collection-dialog / update-collection-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 { collectionUpdaterActions, updateCollection } from "../../store/collections/updater/collection-updater-action";
11 import { dataExplorerActions } from "../../store/data-explorer/data-explorer-action";
12 import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
13 import { DialogCollectionUpdate } from "../dialog-update/dialog-collection-update";
14
15 const mapStateToProps = (state: RootState) => ({
16     open: state.collections.updater.opened
17 });
18
19 const mapDispatchToProps = (dispatch: Dispatch) => ({
20     handleClose: () => {
21         dispatch(collectionUpdaterActions.CLOSE_COLLECTION_UPDATER());
22     },
23     onSubmit: (data: { name: string, description: string }) => {
24         return dispatch<any>(editCollection(data))
25             .catch((e: any) => {
26                 if(e.errors) {
27                     throw new SubmissionError({ name: e.errors.join("").includes("UniqueViolation") ? "Collection with this name already exists." : "" });
28                 }
29             });
30     }
31 });
32
33 const editCollection = (data: { name: string, description: string }) =>
34     (dispatch: Dispatch) => {
35         return dispatch<any>(updateCollection(data)).then(() => {
36             dispatch(snackbarActions.OPEN_SNACKBAR({
37                 message: "Collection has been successfully updated.",
38                 hideDuration: 2000
39             }));
40             dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
41         });
42     };
43
44 export const UpdateCollectionDialog = connect(mapStateToProps, mapDispatchToProps)(DialogCollectionUpdate);