8070a386d1bf7d1226ee1fb5a2c4687b162284e6
[arvados-workbench2.git] / src / services / project-service / project-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { serverApi } from "../../common/api/server-api";
6 import { Dispatch } from "redux";
7 import actions from "../../store/project/project-action";
8 import { Project } from "../../models/project";
9 import UrlBuilder from "../../common/api/url-builder";
10 import FilterBuilder, { FilterField } from "../../common/api/filter-builder";
11 import { ArvadosResource } from "../response";
12
13 interface GroupResource extends ArvadosResource {
14     name: string;
15     group_class: string;
16     description: string;
17     writable_by: string[];
18     delete_at: string;
19     trash_at: string;
20     is_trashed: boolean;
21 }
22
23 interface GroupsResponse {
24     offset: number;
25     limit: number;
26     items: GroupResource[];
27 }
28
29 export default class ProjectService {
30     public getProjectList = (parentUuid?: string) => (dispatch: Dispatch): Promise<Project[]> => {
31         dispatch(actions.PROJECTS_REQUEST(parentUuid));
32         if (parentUuid) {
33             const fb = new FilterBuilder();
34             fb.addLike(FilterField.OWNER_UUID, parentUuid);
35             return serverApi.get<GroupsResponse>('/groups', { params: {
36                 filters: fb.get()
37             }}).then(resp => {
38                 const projects = resp.data.items.map(g => ({
39                     name: g.name,
40                     createdAt: g.created_at,
41                     modifiedAt: g.modified_at,
42                     href: g.href,
43                     uuid: g.uuid,
44                     ownerUuid: g.owner_uuid,
45                     kind: g.kind
46                 } as Project));
47                 dispatch(actions.PROJECTS_SUCCESS({projects, parentItemId: parentUuid}));
48                 return projects;
49             });
50         } else {
51             dispatch(actions.PROJECTS_SUCCESS({projects: [], parentItemId: parentUuid}));
52             return Promise.resolve([]);
53         }
54     }
55 }