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.
198 lines
5.7 KiB
198 lines
5.7 KiB
import { Action } from 'redux'
|
|
|
|
import { apiFetch } from 'src/api'
|
|
import { setEntity, setEntities } from 'src/actions/entities'
|
|
import { startRequest, finishRequest } from 'src/actions/requests'
|
|
|
|
import { objectToQuerystring } from 'src/utils'
|
|
import { normalize } from 'src/utils/normalization'
|
|
import { AppThunkAction, Entity, RequestKey, EntityType, User } 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, EntityType.Group)
|
|
|
|
dispatch(setEntities(groups.entities))
|
|
dispatch(setGroups(groups.keys))
|
|
|
|
if (response.continuation) {
|
|
dispatch(setContinuation(response.continuation))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchGroups, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroups, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface GroupMembersResponse {
|
|
members: User[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchGroupMembers = (id: string, type?: string, continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroupMembers))
|
|
|
|
try {
|
|
const response = await apiFetch<GroupMembersResponse>({
|
|
path: `/api/group/${id}/members?${objectToQuerystring({ type, continuation })}`,
|
|
})
|
|
|
|
const users = normalize(response.members, EntityType.User)
|
|
|
|
dispatch(setEntities(users.entities))
|
|
dispatch(finishRequest(RequestKey.FetchGroupMembers, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroupMembers, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface GroupLogsResponse {
|
|
logs: Entity[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchLogs = (id: string, continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroupLogs))
|
|
|
|
try {
|
|
const response = await apiFetch<GroupLogsResponse>({
|
|
path: `/api/group/${id}/logs?${objectToQuerystring({ continuation })}`,
|
|
})
|
|
|
|
const users = normalize(response.logs, EntityType.Log)
|
|
|
|
dispatch(setEntities(users.entities))
|
|
dispatch(finishRequest(RequestKey.FetchGroupLogs, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroupLogs, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface CreateInvitationResponse {
|
|
code: string
|
|
}
|
|
|
|
export const createInvitation = (id: string, expiration?: number, limit?: number): AppThunkAction<string> => async dispatch => {
|
|
dispatch(startRequest(RequestKey.CreateInvitation))
|
|
|
|
try {
|
|
const response = await apiFetch<CreateInvitationResponse>({
|
|
path: `/api/group/${id}/invitation`,
|
|
method: 'post',
|
|
body: {
|
|
expiration,
|
|
limit,
|
|
}
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.CreateInvitation, true))
|
|
return response.code
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.CreateInvitation, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface InvitationsResponse {
|
|
invitations: Entity[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchInvitations = (id: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchInvitations))
|
|
|
|
try {
|
|
const response = await apiFetch<InvitationsResponse>({
|
|
path: `/api/group/${id}/invitations`,
|
|
})
|
|
|
|
const invitations = normalize(response.invitations, EntityType.Invitation)
|
|
dispatch(setEntities(invitations.entities))
|
|
dispatch(finishRequest(RequestKey.FetchInvitations, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchInvitations, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const updateGroup = (id: string, updates: object): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.UpdateGroup))
|
|
|
|
try {
|
|
await apiFetch({
|
|
path: `/api/group/${id}`,
|
|
method: 'put',
|
|
body: updates,
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.UpdateGroup, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.UpdateGroup, false))
|
|
throw err
|
|
}
|
|
}
|