UNPKG

shift-admin-ui-kit

Version:
176 lines (149 loc) 4.05 kB
import * as types from './constants/actionTypes' import * as localStorageKeys from './constants/localStorageKeys' import composeQueryString from './lib/composeQueryString' export function resetAuthentication() { return (dispatch) => { dispatch(setAuthenticationToken(null)) window.localStorage.removeItem(localStorageKeys.ACCESS_TOKEN_KEY) window.localStorage.removeItem(localStorageKeys.REFRESH_TOKEN_KEY) window.localStorage.removeItem(localStorageKeys.REFRESH_TOKEN_AT_KEY) } } export function setAuthenticationToken(token) { return { type: types.SET_AUTHENTICATION_TOKEN, token: token } } export function getAutoCompleteOptions(endpoint, queryObject, field) { return (dispatch) => { const callback = (json) => { let options = json.data.map((opt) => { return opt.attributes[field] }) dispatch(setAutoCompleteOptions(options)) } dispatch(readFromAPI(endpoint, queryObject)). then(callback). catch((error) => { console.log(error) }) } } export function setAutoCompleteOptions(options) { return { type: types.SET_AUTOCOMPLETE_OPTIONS, options: options } } export function setFlashMessage(options) { return { type: types.SET_FLASH_MESSAGE, flashMessage: options } } export function setPaginationPage(page) { return { type: types.SET_PAGINATION_PAGE, current_page: page } } export function setRecordCount(record_count) { return { type: types.SET_RECORD_COUNT, record_count: record_count } } export function setSearchTerm(search_term, namespace) { return { type: types.SET_SEARCH_TERM, namespace: namespace, term: search_term } } export function setDisplaySearchTerm(search_term, namespace) { return { type: types.SET_DISPLAY_SEARCH_TERM, namespace: namespace, display_search_term: search_term } } export function resetSearch(namespace) { return { type: types.RESET_SEARCH, namespace: namespace } } export function setApplicationConfiguration(config, services = []) { return ((dispatch) => { services.forEach ((service) =>{ service(config, dispatch) }) }) } export function liveMessage(data) { return { type: types.LIVE_UPDATE, payload: data } } export function liveMessagingConfigured() { return { type: types.LIVE_MESSAGING_CONFIGURED } } export function setLoadingState(boolean) { return { type: types.SET_PROCESS_LOADING, loading: boolean } } export function setErroredState(boolean) { return { type: types.SET_PROCESS_ERRORED, errored: boolean } } export function resetProcesses() { return { type: types.RESET_PROCESSES } } export function setApiBaseEndpoint(endpoint) { return { type: types.SET_API_BASE_ENDPOINT, baseEndpoint: endpoint } } export function setApiHeaders(headers) { return { type: types.SET_API_HEADERS, headers: headers } } export function sendApiRequest(endpoint, options = {}) { return (dispatch, getState) => { const method = options.method || "GET" const body = JSON.stringify(options.body) const headers = options.headers || getState().api.headers const host = options.host || getState().api.baseEndpoint const url = host + endpoint return fetch(url, { headers: headers, method: method, body: body }). then( (response) => { if (response.ok || options.skipResponseValidation) { return Promise.resolve(response.json()) } else { return Promise.reject(`Error ${response.status}: ${response.statusText}`) } }) } } export function readFromAPI(endpoint, queryObject = {}, options) { return (dispatch) => { let url = endpoint if (Object.keys(queryObject).length > 0) { url += composeQueryString(queryObject) } return dispatch(sendApiRequest(url, options)) } } export function postToAPI(endpoint, body, options = {}) { return (dispatch) => { options.body = body options.method = "POST" return dispatch(sendApiRequest(endpoint, options)) } }