Create actions and store field for project creator
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 9 Jul 2018 14:47:34 +0000 (16:47 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 9 Jul 2018 14:47:34 +0000 (16:47 +0200)
Feature #13694

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/store/project/project-action.ts
src/store/project/project-reducer.test.ts
src/store/project/project-reducer.ts

index 516934969310fa7c5331d49719b190c5fc8d990b..a02c9e75461565687884b9ff960f7e5b76ca1b6c 100644 (file)
@@ -8,9 +8,13 @@ import { projectService } from "../../services/services";
 import { Dispatch } from "redux";
 import { getResourceKind } from "../../models/resource";
 import FilterBuilder from "../../common/api/filter-builder";
+import { ThunkAction } from "../../../node_modules/redux-thunk";
 
 const actions = unionize({
-    CREATE_PROJECT: ofType<Project>(),
+    OPEN_PROJECT_CREATOR: ofType<{}>(),
+    CREATE_PROJECT: ofType<Partial<ProjectResource>>(),
+    CREATE_PROJECT_SUCCESS: ofType<ProjectResource>(),
+    CREATE_PROJECT_ERROR: ofType<string>(),
     REMOVE_PROJECT: ofType<string>(),
     PROJECTS_REQUEST: ofType<string>(),
     PROJECTS_SUCCESS: ofType<{ projects: Project[], parentItemId?: string }>(),
@@ -38,5 +42,14 @@ export const getProjectList = (parentUuid: string = '') => (dispatch: Dispatch)
     });
 };
 
+export const createProject = (project: Partial<ProjectResource>) =>
+    (dispatch: Dispatch) => {
+        dispatch(actions.CREATE_PROJECT(project));
+        return projectService
+            .create(project)
+            .then(project => dispatch(actions.CREATE_PROJECT_SUCCESS(project)))
+            .catch(() => dispatch(actions.CREATE_PROJECT_ERROR("Could not create a project")));
+    };
+
 export type ProjectAction = UnionOf<typeof actions>;
 export default actions;
index c80f18c82f1ef92a6032cbd60ce46687d0b7cd8f..724623e4c9f362c9dd60163446915fe6fb46ecf7 100644 (file)
@@ -76,7 +76,8 @@ describe('project-reducer', () => {
                 active: true,
                 status: 1
             }],
-            currentItemId: "1"
+            currentItemId: "1",
+            creator: { opened: false, pending: false },
         };
         const project = {
             items: [{
@@ -118,7 +119,8 @@ describe('project-reducer', () => {
                 active: false,
                 status: 1
             }],
-            currentItemId: "1"
+            currentItemId: "1",
+            creator: { opened: false, pending: false }
         };
         const project = {
             items: [{
@@ -137,7 +139,8 @@ describe('project-reducer', () => {
                 status: 1,
                 toggled: true
             }],
-            currentItemId: "1"
+            currentItemId: "1",
+            creator: { opened: false, pending: false },
         };
 
         const state = projectsReducer(initialState, actions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(initialState.items[0].id));
@@ -163,7 +166,8 @@ describe('project-reducer', () => {
                 status: 1,
                 toggled: false,
             }],
-            currentItemId: "1"
+            currentItemId: "1",
+            creator: { opened: false, pending: false }
         };
         const project = {
             items: [{
@@ -182,7 +186,8 @@ describe('project-reducer', () => {
                 status: 1,
                 toggled: true
             }],
-            currentItemId: "1"
+            currentItemId: "1",
+            creator: { opened: false, pending: false },
         };
 
         const state = projectsReducer(initialState, actions.TOGGLE_PROJECT_TREE_ITEM_OPEN(initialState.items[0].id));
index efef80992189c39a2d748a477cc972f3b1bf696b..bb7d3d07e3720cfba20aa939122582d54be5860f 100644 (file)
@@ -10,7 +10,11 @@ import { TreeItem, TreeItemStatus } from "../../components/tree/tree";
 
 export type ProjectState = {
     items: Array<TreeItem<Project>>,
-    currentItemId: string
+    currentItemId: string,
+    creator: {
+        opened: boolean,
+        pending: boolean
+    }
 };
 
 export function findTreeItem<T>(tree: Array<TreeItem<T>>, itemId: string): TreeItem<T> | undefined {
@@ -40,12 +44,12 @@ export function getActiveTreeItem<T>(tree: Array<TreeItem<T>>): TreeItem<T> | un
 }
 
 export function getTreePath<T>(tree: Array<TreeItem<T>>, itemId: string): Array<TreeItem<T>> {
-    for (const item of tree){
-        if(item.id === itemId){
+    for (const item of tree) {
+        if (item.id === itemId) {
             return [item];
         } else {
             const branch = getTreePath(item.items || [], itemId);
-            if(branch.length > 0){
+            if (branch.length > 0) {
                 return [item, ...branch];
             }
         }
@@ -85,20 +89,22 @@ function updateProjectTree(tree: Array<TreeItem<Project>>, projects: Project[],
     return items;
 }
 
-const projectsReducer = (state: ProjectState = { items: [], currentItemId: "" }, action: ProjectAction) => {
+const initialState: ProjectState = {
+    items: [],
+    currentItemId: "",
+    creator: {
+        opened: false,
+        pending: false
+    }
+};
+
+
+const projectsReducer = (state: ProjectState = initialState, action: ProjectAction) => {
     return actions.match(action, {
-        CREATE_PROJECT: project => ({
-            ...state,
-            items: state.items.concat({
-                id: project.uuid,
-                open: false,
-                active: false,
-                status: TreeItemStatus.Loaded,
-                toggled: false,
-                items: [],
-                data: project
-            })
-        }),
+        OPEN_PROJECT_CREATOR: () => ({ ...state, creator: { opened: true, pending: false } }),
+        CREATE_PROJECT: () => ({ ...state, creator: { opened: false, pending: true } }),
+        CREATE_PROJECT_SUCCESS: () => ({ ...state, creator: { opened: false, pending: false } }),
+        CREATE_PROJECT_ERROR: () => ({ ...state, creator: { opened: false, pending: false } }),
         REMOVE_PROJECT: () => state,
         PROJECTS_REQUEST: itemId => {
             const items = _.cloneDeep(state.items);