[ABANDONED] React/Redux front end for the Flexor social network.
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.
 
 

90 lines
2.6 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 { REQUEST_KEYS } from 'src/constants'
import { objectToQuerystring } from 'src/utils'
import { AppThunkAction, Entity } 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(REQUEST_KEYS.FETCH_GROUP))
try {
const group = await apiFetch<Entity>({
path: `/api/group/${id}`
})
dispatch(setEntity('group', group))
dispatch(finishRequest(REQUEST_KEYS.FETCH_GROUP, true))
} catch (err) {
dispatch(finishRequest(REQUEST_KEYS.FETCH_GROUP, false))
throw err
}
}
}
interface GroupsResponse {
groups: Entity[]
continuation?: string
}
export const fetchGroups = (sort?: string, continuation?: string): AppThunkAction => async dispatch => {
dispatch(startRequest(REQUEST_KEYS.FETCH_GROUPS))
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(REQUEST_KEYS.FETCH_GROUPS, true))
} catch (err) {
dispatch(finishRequest(REQUEST_KEYS.FETCH_GROUPS, false))
throw err
}
}