-
Notifications
You must be signed in to change notification settings - Fork 138
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 #2511 from cloudfoundry-incubator/fix-app-count-stuck
Fix for app count being stuck after add in org card
- Loading branch information
Showing
3 changed files
with
79 additions
and
17 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
71 changes: 71 additions & 0 deletions
71
src/frontend/app/store/reducers/application-add-remove-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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { APIResource } from '../types/api.types'; | ||
import { APISuccessOrFailedAction } from '../types/request.types'; | ||
import { CREATE_SUCCESS, DELETE_SUCCESS } from '../actions/application.actions'; | ||
import { IApp, ISpace } from '../../core/cf-api.types'; | ||
import { deepMergeState } from '../helpers/reducer.helper'; | ||
|
||
|
||
export function applicationAddRemoveReducer(name) { | ||
return function (state: APIResource, action: APISuccessOrFailedAction) { | ||
switch (action.type) { | ||
case CREATE_SUCCESS: | ||
return addApplicationToSpace(state, action); | ||
case DELETE_SUCCESS: | ||
return deleteApplicationFromSpace(state, action); | ||
} | ||
return state; | ||
}; | ||
} | ||
|
||
function addApplicationToSpace(state: APIResource, action: APISuccessOrFailedAction) { | ||
if (action.response && action.response.entities && action.response.entities.application) { | ||
const apps = action.response.entities.application; | ||
const updatedSpaces = {}; | ||
Object.keys(apps).forEach(appGuid => { | ||
const app = apps[appGuid] as APIResource<IApp>; | ||
const spaceGuid = app.entity.space_guid; | ||
const space = state[spaceGuid] as APIResource<ISpace>; | ||
if (space.entity.apps) { | ||
const newSpaceEntity = { | ||
entity: { | ||
apps: [ ...space.entity.apps, app.metadata.guid ] | ||
} | ||
}; | ||
updatedSpaces[spaceGuid] = newSpaceEntity; | ||
} | ||
}); | ||
if (Object.keys(updatedSpaces).length) { | ||
return deepMergeState(state, updatedSpaces); | ||
} | ||
} | ||
|
||
return state; | ||
} | ||
|
||
interface SpaceAsAppRefs { | ||
apps: string[]; | ||
} | ||
|
||
function deleteApplicationFromSpace(state: APIResource, action: APISuccessOrFailedAction) { | ||
// GUID of the application that was deleted | ||
const appGuid = action.apiAction.guid; | ||
// We don't know ths space GUID, but app guids are unique, so look across all spaces | ||
const updatedSpaces = {}; | ||
Object.keys(state).forEach(spaceGuid => { | ||
const space = state[spaceGuid] as APIResource<SpaceAsAppRefs>; | ||
const apps = <string[]> space.entity.apps; | ||
if (apps && apps.findIndex((value) => value === appGuid) >= 0) { | ||
const newSpaceEntity = { | ||
entity: { | ||
apps: apps.filter((guid) => guid !== appGuid) | ||
} | ||
}; | ||
updatedSpaces[spaceGuid] = newSpaceEntity; | ||
} | ||
}); | ||
if (Object.keys(updatedSpaces).length) { | ||
return deepMergeState(state, updatedSpaces); | ||
} | ||
|
||
return state; | ||
} |