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.
89 lines
2.5 KiB
89 lines
2.5 KiB
import { Action } from 'redux'
|
|
import { normalize } from 'normalizr'
|
|
|
|
import { apiFetch } from 'src/api'
|
|
import { setEntity, setEntities } from 'src/actions/entities'
|
|
import { startRequest, finishRequest } from 'src/actions/requests'
|
|
import { groupSchema } from 'src/store/schemas'
|
|
|
|
import { objectToQuerystring } from 'src/utils'
|
|
import { AppThunkAction, Entity, RequestKey, EntityType } from 'src/types'
|
|
|
|
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): AppThunkAction => {
|
|
return async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroup))
|
|
|
|
try {
|
|
const group = await apiFetch<Entity>({
|
|
path: `/api/group/${id}`
|
|
})
|
|
|
|
dispatch(setEntity(EntityType.Group, group))
|
|
dispatch(finishRequest(RequestKey.FetchGroup, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroup, false))
|
|
throw err
|
|
}
|
|
}
|
|
}
|
|
|
|
interface GroupsResponse {
|
|
groups: Entity[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchGroups = (sort?: string, continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroups))
|
|
|
|
try {
|
|
const response = await apiFetch<GroupsResponse>({
|
|
path: `/api/groups?${objectToQuerystring({ 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(RequestKey.FetchGroups, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroups, false))
|
|
throw err
|
|
}
|
|
}
|