From: Michal Klobukowski Date: Mon, 9 Jul 2018 14:47:34 +0000 (+0200) Subject: Create actions and store field for project creator X-Git-Tag: 1.2.0~52^2~5^2~1 X-Git-Url: https://git.arvados.org/arvados-workbench2.git/commitdiff_plain/312f0cb3a36f9bce0b16b53765701578191daab8 Create actions and store field for project creator Feature #13694 Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski --- diff --git a/src/store/project/project-action.ts b/src/store/project/project-action.ts index 51693496..a02c9e75 100644 --- a/src/store/project/project-action.ts +++ b/src/store/project/project-action.ts @@ -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(), + OPEN_PROJECT_CREATOR: ofType<{}>(), + CREATE_PROJECT: ofType>(), + CREATE_PROJECT_SUCCESS: ofType(), + CREATE_PROJECT_ERROR: ofType(), REMOVE_PROJECT: ofType(), PROJECTS_REQUEST: ofType(), PROJECTS_SUCCESS: ofType<{ projects: Project[], parentItemId?: string }>(), @@ -38,5 +42,14 @@ export const getProjectList = (parentUuid: string = '') => (dispatch: Dispatch) }); }; +export const createProject = (project: Partial) => + (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; export default actions; diff --git a/src/store/project/project-reducer.test.ts b/src/store/project/project-reducer.test.ts index c80f18c8..724623e4 100644 --- a/src/store/project/project-reducer.test.ts +++ b/src/store/project/project-reducer.test.ts @@ -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)); diff --git a/src/store/project/project-reducer.ts b/src/store/project/project-reducer.ts index efef8099..bb7d3d07 100644 --- a/src/store/project/project-reducer.ts +++ b/src/store/project/project-reducer.ts @@ -10,7 +10,11 @@ import { TreeItem, TreeItemStatus } from "../../components/tree/tree"; export type ProjectState = { items: Array>, - currentItemId: string + currentItemId: string, + creator: { + opened: boolean, + pending: boolean + } }; export function findTreeItem(tree: Array>, itemId: string): TreeItem | undefined { @@ -40,12 +44,12 @@ export function getActiveTreeItem(tree: Array>): TreeItem | un } export function getTreePath(tree: Array>, itemId: string): Array> { - 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>, 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);