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.
82 lines
2.5 KiB
82 lines
2.5 KiB
import { Action, AnyAction } from 'redux'
|
|
import { ThunkAction, ThunkDispatch } from 'redux-thunk'
|
|
import { normalize } from 'normalizr'
|
|
|
|
import { getGroup, getGroups } from '../api/groups'
|
|
import { setEntity, setEntities } from '../actions/entities'
|
|
import { startRequest, finishRequest } from '../actions/requests'
|
|
import { groupSchema } from '../store/schemas'
|
|
import { AppState } from '../types'
|
|
|
|
const FETCH_GROUP_ID = 'FETCH_GROUP'
|
|
const FETCH_GROUPS_ID = 'FETCH_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
|
|
|
|
export const setGroups = (groups: string[]): SetGroupsAction => ({
|
|
type: 'DIRECTORY_SET_GROUPS',
|
|
payload: groups,
|
|
})
|
|
|
|
export const appendGroups = (groups: string[]): AppendGroupsAction => ({
|
|
type: 'DIRECTORY_APPEND_GROUPS',
|
|
payload: groups,
|
|
})
|
|
|
|
export const setContinuation = (continuation: string): SetContinuationAction => ({
|
|
type: 'DIRECTORY_SET_CONTINUATION',
|
|
payload: continuation,
|
|
})
|
|
|
|
export const fetchGroup = (id: string) => {
|
|
return async (dispatch: ThunkDispatch<AppState, void, AnyAction>) => {
|
|
dispatch(startRequest(FETCH_GROUP_ID))
|
|
|
|
try {
|
|
const group = await getGroup(id)
|
|
dispatch(setEntity('group', group))
|
|
dispatch(finishRequest(FETCH_GROUP_ID, true))
|
|
} catch (err) {
|
|
console.error(err)
|
|
dispatch(finishRequest(FETCH_GROUP_ID, false))
|
|
}
|
|
}
|
|
}
|
|
|
|
export const fetchGroups = (sort?: string, continuation?: string): ThunkAction<Promise<void>, AppState, void, AnyAction> => {
|
|
return async (dispatch: ThunkDispatch<AppState, void, AnyAction>) => {
|
|
dispatch(startRequest(FETCH_GROUPS_ID))
|
|
|
|
try {
|
|
const response = await getGroups(sort, continuation)
|
|
const groups = normalize(response.groups, [groupSchema])
|
|
|
|
dispatch(setEntities(groups.entities))
|
|
dispatch(setGroups(groups.result))
|
|
|
|
if (response.continuation) {
|
|
dispatch(setContinuation(response.continuation))
|
|
}
|
|
|
|
dispatch(finishRequest(FETCH_GROUPS_ID, true))
|
|
} catch (err) {
|
|
console.error(err)
|
|
dispatch(finishRequest(FETCH_GROUPS_ID, false))
|
|
}
|
|
}
|
|
}
|