1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { bindDataExplorerActions } from '~/store/data-explorer/data-explorer-action';
7 import { RootState } from '~/store/store';
8 import { ServiceRepository } from "~/services/services";
9 import { navigateToRepositories } from "~/store/navigation/navigation-action";
10 import { unionize, ofType, UnionOf } from "~/common/unionize";
11 import { dialogActions } from '~/store/dialog/dialog-actions';
12 import { RepositoryResource } from "~/models/repositories";
13 import { startSubmit, reset, stopSubmit, FormErrors } from "redux-form";
14 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
15 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
17 export const repositoriesActions = unionize({
18 SET_REPOSITORIES: ofType<any>(),
21 export type RepositoriesActions = UnionOf<typeof repositoriesActions>;
23 export const REPOSITORIES_PANEL = 'repositoriesPanel';
24 export const REPOSITORIES_SAMPLE_GIT_DIALOG = 'repositoriesSampleGitDialog';
25 export const REPOSITORY_ATTRIBUTES_DIALOG = 'repositoryAttributesDialog';
26 export const REPOSITORY_CREATE_FORM_NAME = 'repositoryCreateFormName';
27 export const REPOSITORY_REMOVE_DIALOG = 'repositoryRemoveDialog';
29 export const openRepositoriesSampleGitDialog = () =>
30 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31 const uuidPrefix = getState().properties.uuidPrefix;
32 dispatch(dialogActions.OPEN_DIALOG({ id: REPOSITORIES_SAMPLE_GIT_DIALOG, data: { uuidPrefix } }));
35 export const openRepositoryAttributes = (uuid: string) =>
36 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
37 const repositoryData = getState().repositories.items.find(it => it.uuid === uuid);
38 dispatch(dialogActions.OPEN_DIALOG({ id: REPOSITORY_ATTRIBUTES_DIALOG, data: { repositoryData } }));
41 export const openRepositoryCreateDialog = () =>
42 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43 const userUuid = await services.authService.getUuid();
44 const user = await services.userService.get(userUuid!);
45 dispatch(reset(REPOSITORY_CREATE_FORM_NAME));
46 dispatch(dialogActions.OPEN_DIALOG({ id: REPOSITORY_CREATE_FORM_NAME, data: { user } }));
49 export const createRepository = (repository: RepositoryResource) =>
50 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
51 const userUuid = await services.authService.getUuid();
52 const user = await services.userService.get(userUuid!);
53 dispatch(startSubmit(REPOSITORY_CREATE_FORM_NAME));
55 const newRepository = await services.repositoriesService.create({ name: `${user.username}/${repository.name}` });
56 dispatch(dialogActions.CLOSE_DIALOG({ id: REPOSITORY_CREATE_FORM_NAME }));
57 dispatch(reset(REPOSITORY_CREATE_FORM_NAME));
58 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Repository has been successfully created.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
59 dispatch<any>(loadRepositoriesData());
62 const error = getCommonResourceServiceError(e);
63 if (error === CommonResourceServiceError.NAME_HAS_ALREADY_BEEN_TAKEN) {
64 dispatch(stopSubmit(REPOSITORY_CREATE_FORM_NAME, { name: 'Repository with the same name already exists.' } as FormErrors));
70 export const openRemoveRepositoryDialog = (uuid: string) =>
71 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72 dispatch(dialogActions.OPEN_DIALOG({
73 id: REPOSITORY_REMOVE_DIALOG,
75 title: 'Remove repository',
76 text: 'Are you sure you want to remove this repository?',
77 confirmButtonLabel: 'Remove',
83 export const removeRepository = (uuid: string) =>
84 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
85 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
86 await services.repositoriesService.delete(uuid);
87 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
88 dispatch<any>(loadRepositoriesData());
91 const repositoriesBindedActions = bindDataExplorerActions(REPOSITORIES_PANEL);
93 export const openRepositoriesPanel = () =>
94 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
95 dispatch<any>(navigateToRepositories);
98 export const loadRepositoriesData = () =>
99 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
100 const repositories = await services.repositoriesService.list();
101 dispatch(repositoriesActions.SET_REPOSITORIES(repositories.items));
104 export const loadRepositoriesPanel = () =>
105 (dispatch: Dispatch) => {
106 dispatch(repositoriesBindedActions.REQUEST_ITEMS());