UNPKG

stitch-ui

Version:

108 lines (101 loc) 3.73 kB
import { OrderedMap } from "immutable"; import { createAction, createReducer } from "redux-act"; import { makeAsyncActions, asyncActionExecutor } from "../util"; import APIKeyModel from "../models/APIKey"; /* These functions take a client and set of args and return a root * object from the admin API that can be used to manipulate keys, since the * methods for app-specific API keys and user-specific profile keys are homomorphic. * This allows re-using the same actions/reducers for both parts of the site, * by supplying the appropriate adapter function. */ const apiKeysRoot = (client, groupId, appId) => client.apps(groupId).app(appId).apiKeys(); const apiKeyRoot = (client, groupId, appId, keyId) => client.apps(groupId).app(appId).apiKeys().apiKey(keyId); export const makeKeyActions = ( NAME, keysRoot = apiKeysRoot, keyRoot = apiKeyRoot ) => { let actions = { loadAPIKeysActions: makeAsyncActions(`${NAME}load api keys`), deleteAPIKeyActions: makeAsyncActions(`${NAME}delete api key`), disableAPIKeyActions: makeAsyncActions(`${NAME}disable api key`), enableAPIKeyActions: makeAsyncActions(`${NAME}enable api key`), createAPIKeyActions: makeAsyncActions(`${NAME}create api key`), getAPIKeyActions: makeAsyncActions(`${NAME}get api key`), setAPIKeyHidden: createAction(`${NAME}set api key hidden`), openNewKeyForm: createAction(`${NAME}open api key form`), closeNewKeyForm: createAction(`${NAME}close api key form`), setNewKeyName: createAction(`${NAME}set new key name`) }; actions = { ...actions, loadAPIKeys: asyncActionExecutor( actions.loadAPIKeysActions, (client, ...args) => keysRoot(client, ...args).list ), deleteAPIKey: asyncActionExecutor( actions.deleteAPIKeyActions, (client, ...args) => keyRoot(client, ...args).remove ), enableAPIKey: asyncActionExecutor( actions.enableAPIKeyActions, (client, ...args) => keyRoot(client, ...args).enable ), disableAPIKey: asyncActionExecutor( actions.disableAPIKeyActions, (client, ...args) => keyRoot(client, ...args).disable ), createAPIKey: asyncActionExecutor( actions.createAPIKeyActions, (client, ...args) => () => keysRoot(client, ...args.slice(0, args.length - 1)).create( args[args.length - 1] ) ), getAPIKey: asyncActionExecutor( actions.getAPIKeyActions, (client, ...args) => keyRoot(client, ...args).get ) }; return actions; }; export const makeAPIKeyReducer = actions => createReducer( { [actions.createAPIKeyActions.fail]: (state, payload) => ({ ...state, apiKeyError: payload.error }), [actions.createAPIKeyActions.rcv]: state => ({ ...state, apiKeyError: null, newKeyName: "" }), [actions.loadAPIKeysActions.rcv]: (state, { payload }) => ({ ...state, apiKeys: OrderedMap(payload.map(x => [x._id, new APIKeyModel(x)])) }), [actions.setAPIKeyHidden]: (state, { key, hidden }) => ({ ...state, apiKeys: state.apiKeys.setIn([key, "hidden"], hidden) }), [actions.getAPIKeyActions.rcv]: (state, { payload }) => ({ ...state, apiKeys: state.apiKeys.set(payload._id, new APIKeyModel(payload)) }), [actions.setNewKeyName]: (state, { name }) => ({ ...state, newKeyName: name }), [actions.openNewKeyForm]: state => ({ ...state, showNewKeyForm: true }), [actions.closeNewKeyForm]: state => ({ ...state, showNewKeyForm: false }) }, { newKeyName: "", apiKeyError: null, showNewKeyForm: false, apiKeys: OrderedMap() } );