accounts-frontend/src/components/dev/apps/reducer.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

// @flow
import type { OauthAppResponse } from 'services/api/oauth';
2018-05-05 12:12:21 +05:30
import type { Action } from './actions';
export type Apps = {|
+available: OauthAppResponse[],
|};
const defaults: Apps = {
available: [],
};
export default function apps(
state: Apps = defaults,
2018-05-05 12:12:21 +05:30
action: Action
): Apps {
2018-05-05 12:12:21 +05:30
switch (action.type) {
case 'apps:setAvailable':
return {
...state,
2018-05-05 12:12:21 +05:30
available: action.payload,
};
2018-05-05 12:12:21 +05:30
case 'apps:addApp': {
const { payload } = action;
const available = [...state.available];
2018-05-05 12:12:21 +05:30
let index = available.findIndex((app) => app.clientId === payload.clientId);
if (index === -1) {
index = available.length;
}
available[index] = action.payload;
return {
...state,
available
};
2018-05-05 12:12:21 +05:30
}
case 'apps:deleteApp':
return {
...state,
available: state.available.filter((app) => app.clientId !== action.payload),
};
default:
(action.type: empty);
return state;
}
}