You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.0 KiB
73 lines
2.0 KiB
import { Action, AnyAction } from 'redux'
|
|
import { ThunkAction, ThunkDispatch } from 'redux-thunk'
|
|
import { normalize } from 'normalizr'
|
|
|
|
import { getGroups } from '../api/groups'
|
|
import { setEntities } from '../actions/entities'
|
|
import { startRequest, finishRequest } from '../actions/requests'
|
|
import { group } from '../store/schemas'
|
|
import { AppState } from '../types'
|
|
|
|
const FETCH_ID = 'groups'
|
|
|
|
export interface SetGroupsAction extends Action {
|
|
type: 'DIRECTORY_SET_GROUPS'
|
|
payload: string[]
|
|
}
|
|
|
|
export interface AppendGroupsAction extends Action {
|
|
type: 'DIRECTORY_APPEND_GROUPS',
|
|
payload: string[]
|
|
}
|
|
|
|
export interface SetContinuationAction extends Action {
|
|
type: 'DIRECTORY_SET_CONTINUATION'
|
|
payload: string
|
|
}
|
|
|
|
export type DirectoryActions = SetGroupsAction | AppendGroupsAction | SetContinuationAction
|
|
|
|
const setGroups = (groups: string[]): SetGroupsAction => ({
|
|
type: 'DIRECTORY_SET_GROUPS',
|
|
payload: groups,
|
|
})
|
|
|
|
const appendGroups = (groups: string[]): AppendGroupsAction => ({
|
|
type: 'DIRECTORY_APPEND_GROUPS',
|
|
payload: groups,
|
|
})
|
|
|
|
const setContinuation = (continuation: string): SetContinuationAction => ({
|
|
type: 'DIRECTORY_SET_CONTINUATION',
|
|
payload: continuation,
|
|
})
|
|
|
|
const fetchGroups = (sort?: string, continuation?: string): ThunkAction<Promise<void>, AppState, void, AnyAction> => {
|
|
return async (dispatch: ThunkDispatch<AppState, void, AnyAction>) => {
|
|
dispatch(startRequest(FETCH_ID))
|
|
|
|
try {
|
|
const response = await getGroups(sort, continuation)
|
|
const groups = normalize(response.groups, group)
|
|
|
|
dispatch(setEntities(groups.entities))
|
|
dispatch(setGroups(groups.result))
|
|
|
|
if (response.continuation) {
|
|
dispatch(setContinuation(response.continuation))
|
|
}
|
|
|
|
dispatch(finishRequest(FETCH_ID, true))
|
|
} catch (err) {
|
|
console.error(err)
|
|
dispatch(finishRequest(FETCH_ID, false))
|
|
}
|
|
}
|
|
}
|
|
|
|
export {
|
|
setGroups,
|
|
appendGroups,
|
|
setContinuation,
|
|
fetchGroups,
|
|
}
|