import { serverApi } from "../../common/api/server-api";
import { Dispatch } from "redux";
-import actions from "../../store/project/project-action";
import { Project } from "../../models/project";
-import UrlBuilder from "../../common/api/url-builder";
import FilterBuilder, { FilterField } from "../../common/api/filter-builder";
+import { ArvadosResource } from "../response";
+
+interface GroupResource extends ArvadosResource {
+ name: string;
+ group_class: string;
+ description: string;
+ writable_by: string[];
+ delete_at: string;
+ trash_at: string;
+ is_trashed: boolean;
+}
interface GroupsResponse {
offset: number;
limit: number;
- items: Array<{
- href: string;
- kind: string;
- etag: string;
- uuid: string;
- owner_uuid: string;
- created_at: string;
- modified_by_client_uuid: string;
- modified_by_user_uuid: string;
- modified_at: string;
- name: string;
- group_class: string;
- description: string;
- writable_by: string[];
- delete_at: string;
- trash_at: string;
- is_trashed: boolean;
- }>;
+ items: GroupResource[];
}
export default class ProjectService {
- public getProjectList = (parentUuid?: string) => (dispatch: Dispatch): Promise<Project[]> => {
- dispatch(actions.PROJECTS_REQUEST());
-
- const ub = new UrlBuilder('/groups');
- const fb = new FilterBuilder();
- fb.addEqual(FilterField.OWNER_UUID, parentUuid);
- const url = ub.addParam('filters', fb.get()).get();
-
- return serverApi.get<GroupsResponse>(url).then(groups => {
- const projects = groups.data.items.map(g => ({
- name: g.name,
- createdAt: g.created_at,
- modifiedAt: g.modified_at,
- href: g.href,
- uuid: g.uuid,
- ownerUuid: g.owner_uuid,
- kind: g.kind
- } as Project));
- dispatch(actions.PROJECTS_SUCCESS({projects, parentItemId: parentUuid}));
- return projects;
- });
- };
+ public getProjectList = (parentUuid?: string): Promise<Project[]> => {
+ if (parentUuid) {
+ const fb = new FilterBuilder();
+ fb.addLike(FilterField.OWNER_UUID, parentUuid);
+ return serverApi.get<GroupsResponse>('/groups', { params: {
+ filters: fb.get()
+ }}).then(resp => {
+ const projects = resp.data.items.map(g => ({
+ name: g.name,
+ createdAt: g.created_at,
+ modifiedAt: g.modified_at,
+ href: g.href,
+ uuid: g.uuid,
+ ownerUuid: g.owner_uuid,
+ kind: g.kind
+ } as Project));
+ return projects;
+ });
+ } else {
+ return Promise.resolve([]);
+ }
+ }
}