[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.
 
 

53 lines
1.5 KiB

import { Action } from 'redux'
import { normalize } from 'normalizr'
import { apiFetch } from 'src/api'
import { setEntities } from 'src/actions/entities'
import { startRequest, finishRequest } from 'src/actions/requests'
import { userSchema } from 'src/store/schemas'
import { REQUEST_KEYS } from 'src/constants'
import { AppThunkAction, Entity } from 'src/types'
export interface SetAuthenticatedAction extends Action {
type: 'AUTHENTICATION_SET_AUTHENTICATED'
payload: boolean
}
export interface SetUserAction extends Action {
type: 'AUTHENTICATION_SET_USER'
payload: string
}
export type AuthenticationActions = SetAuthenticatedAction | SetUserAction
export const setAuthenticated = (authenticated: boolean): SetAuthenticatedAction => ({
type: 'AUTHENTICATION_SET_AUTHENTICATED',
payload: authenticated,
})
export const setUser = (userId: string): SetUserAction => ({
type: 'AUTHENTICATION_SET_USER',
payload: userId,
})
export const fetchSelf = (): AppThunkAction => async dispatch => {
dispatch(startRequest(REQUEST_KEYS.FETCH_GROUP_AVAILABILITY))
try {
const self = await apiFetch<Entity>({
path: '/api/self',
})
const result = normalize(self, userSchema)
dispatch(setEntities(result.entities))
dispatch(setUser(self.id))
dispatch(setAuthenticated(true))
dispatch(finishRequest(REQUEST_KEYS.FETCH_GROUP_AVAILABILITY, true))
} catch (err) {
dispatch(finishRequest(REQUEST_KEYS.FETCH_GROUP_AVAILABILITY, false))
throw err
}
}