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

53 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: Array<OauthAppResponse>,
};
const defaults: Apps = {
available: [],
};
export default function apps(
state: Apps = defaults,
2018-05-05 12:12:21 +05:30
action: Action
) {
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: Array<OauthAppResponse> = [...state.available];
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;
}
}