-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3351 from cloudfoundry-incubator/blank-org-details
Fix null exception after creating a space in an new org
- Loading branch information
Showing
4 changed files
with
81 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 74 additions & 41 deletions
115
src/frontend/app/store/reducers/organization-space.reducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,89 @@ | ||
import { IOrganization, ISpace } from '../../core/cf-api.types'; | ||
import { BaseSpaceAction, CREATE_SPACE_SUCCESS, DELETE_SPACE_SUCCESS } from '../actions/space.actions'; | ||
import { APIResource } from '../types/api.types'; | ||
import { | ||
BaseSpaceAction, | ||
CREATE_SPACE_SUCCESS, | ||
CreateSpace, | ||
DELETE_SPACE_SUCCESS, | ||
DeleteSpace, | ||
} from '../actions/space.actions'; | ||
import { spaceSchemaKey } from '../helpers/entity-factory'; | ||
import { APIResource, NormalizedResponse } from '../types/api.types'; | ||
import { APISuccessOrFailedAction } from '../types/request.types'; | ||
|
||
// Note - This reducer will be updated when we address general entity relation deletion | ||
import { IRequestEntityTypeState } from '../app-state'; | ||
type entityOrgType = APIResource<IOrganization<string[]>>; | ||
// Note - This reducer will be updated when we address general deletion of entities within inline lists (not paginated lists) | ||
export function updateOrganizationSpaceReducer() { | ||
return function (state: APIResource, action: APISuccessOrFailedAction) { | ||
return function (state: IRequestEntityTypeState<entityOrgType>, action: APISuccessOrFailedAction<NormalizedResponse>) { | ||
switch (action.type) { | ||
case DELETE_SPACE_SUCCESS: | ||
const deleteSpaceAction: DeleteSpace = action.apiAction as DeleteSpace; | ||
return removeSpaceFromOrg(state, deleteSpaceAction.orgGuid, deleteSpaceAction.guid); | ||
case CREATE_SPACE_SUCCESS: | ||
const spaceAction: BaseSpaceAction = action.apiAction as BaseSpaceAction; | ||
return deleteOrgSpaces(state, spaceAction.orgGuid, spaceAction); | ||
const createSpaceAction = action.apiAction as CreateSpace; | ||
const response = action.response; | ||
const space = response.entities[spaceSchemaKey][response.result[0]]; | ||
return addSpaceToOrg(state, createSpaceAction.orgGuid, space); | ||
} | ||
return state; | ||
}; | ||
} | ||
|
||
function deleteOrgSpaces(state: APIResource, orgGuid: string, spaceAction: BaseSpaceAction) { | ||
if (!orgGuid) { | ||
return state; | ||
} | ||
function addSpaceToOrg( | ||
state: IRequestEntityTypeState<entityOrgType>, | ||
orgGuid: string, | ||
newSpace: APIResource<ISpace> | ||
) { | ||
const orgToModify = getOrg(state, orgGuid); | ||
const newSpaces = [ | ||
...orgToModify.entity.spaces, | ||
newSpace.metadata.guid | ||
]; | ||
const mergedOrg = applySpacesToOrg(orgToModify, newSpaces); | ||
return { | ||
...state, | ||
[orgGuid]: mergedOrg | ||
}; | ||
} | ||
|
||
const orgGuids = Object.keys(state); | ||
if (orgGuids.indexOf(orgGuid) === -1) { | ||
return state; | ||
} | ||
function removeSpaceFromOrg( | ||
state: IRequestEntityTypeState<entityOrgType>, | ||
orgGuid: string, | ||
spaceGuid: string | ||
) { | ||
const orgToModify = getOrg(state, orgGuid); | ||
const newSpaces = orgToModify.entity.spaces.reduce((spaceIds, spaceId) => { | ||
if (spaceId !== spaceGuid) { | ||
spaceIds.push(spaceId); | ||
} | ||
return spaceIds; | ||
}, []); | ||
const mergedOrg = applySpacesToOrg(orgToModify, newSpaces); | ||
return applyModifyOrgToState(state, mergedOrg); | ||
} | ||
|
||
const newState = {}; | ||
orgGuids.forEach(currentGuid => { | ||
const org: APIResource<IOrganization> = state[currentGuid]; | ||
if (currentGuid === orgGuid) { | ||
let newSpaces: APIResource<ISpace>[] = null; | ||
if (spaceAction.removeEntityOnDelete) { | ||
const spaceIndex = org.entity.spaces.findIndex(space => { | ||
return typeof (space) === 'string' ? space === spaceAction.guid : space.metadata.guid === spaceAction.guid; | ||
}); | ||
if (spaceIndex >= 0) { | ||
newSpaces = [...org.entity.spaces]; | ||
newSpaces.splice(spaceIndex, 1); | ||
} | ||
} | ||
const newOrg = { | ||
...org, | ||
entity: { | ||
...org.entity, | ||
spaces: newSpaces | ||
} | ||
}; | ||
newState[currentGuid] = newOrg; | ||
} else { | ||
newState[currentGuid] = org; | ||
function applySpacesToOrg(org: entityOrgType, spaces: string[]): entityOrgType { | ||
return { | ||
...org, | ||
entity: { | ||
...org.entity, | ||
spaces | ||
} | ||
}); | ||
return newState; | ||
}; | ||
} | ||
|
||
function applyModifyOrgToState(state: IRequestEntityTypeState<entityOrgType>, org: entityOrgType) { | ||
return { | ||
...state, | ||
[org.metadata.guid]: org | ||
}; | ||
} | ||
|
||
function getOrg( | ||
state: IRequestEntityTypeState<entityOrgType>, | ||
orgGuid: string, | ||
) { | ||
const { | ||
[orgGuid]: newOrg | ||
} = state; | ||
return newOrg; | ||
} |