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.
171 lines
4.6 KiB
171 lines
4.6 KiB
import { Action } from 'redux'
|
|
|
|
import { apiFetch } from 'src/api'
|
|
import { setFieldNotification } from 'src/actions/forms'
|
|
import { startRequest, finishRequest } from 'src/actions/requests'
|
|
|
|
import {
|
|
LOCAL_STORAGE_ACCESS_TOKEN_KEY,
|
|
LOCAL_STORAGE_REFRESH_TOKEN_KEY,
|
|
} from 'src/constants'
|
|
|
|
import { AppThunkAction, NotificationType, RequestKey, AvailabilityResponse } from 'src/types'
|
|
|
|
export interface SetStepAction extends Action {
|
|
type: 'REGISTRATION_SET_STEP'
|
|
payload: number
|
|
}
|
|
|
|
export type RegistrationActions = SetStepAction
|
|
|
|
export const setStep = (step: number): SetStepAction => ({
|
|
type: 'REGISTRATION_SET_STEP',
|
|
payload: step,
|
|
})
|
|
|
|
export const checkGroupAvailability = (name: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchGroupAvailability))
|
|
|
|
try {
|
|
const { id, available } = await apiFetch<AvailabilityResponse>({
|
|
path: '/api/group/available',
|
|
method: 'post',
|
|
body: {
|
|
name,
|
|
},
|
|
})
|
|
|
|
if (available) {
|
|
dispatch(setFieldNotification('group-name', NotificationType.Success, `${id} is available`))
|
|
} else {
|
|
dispatch(setFieldNotification('group-name', NotificationType.Error, `${id} isn't available`))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchGroupAvailability, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchGroupAvailability, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const checkUserAvailability = (name: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchUserAvailability))
|
|
|
|
try {
|
|
const { id, available } = await apiFetch<AvailabilityResponse>({
|
|
path: '/api/user/available',
|
|
method: 'post',
|
|
body: {
|
|
name,
|
|
},
|
|
})
|
|
|
|
if (available) {
|
|
dispatch(setFieldNotification('user-id', NotificationType.Success, `${id} is available`))
|
|
} else {
|
|
dispatch(setFieldNotification('user-id', NotificationType.Error, `${id} isn't available`))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchUserAvailability, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchUserAvailability, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface CreateGroupOptions {
|
|
name: string
|
|
registration: string
|
|
about?: string
|
|
imageUrl?: string
|
|
coverImageUrl?: string
|
|
iconImageUrl?: string
|
|
theme: string
|
|
}
|
|
|
|
interface CreateGroupResponse {
|
|
id: string
|
|
}
|
|
|
|
export const createGroup = (options: CreateGroupOptions): AppThunkAction<string> => async dispatch => {
|
|
const { name, registration, about, imageUrl, coverImageUrl, iconImageUrl, theme } = options
|
|
|
|
dispatch(startRequest(RequestKey.CreateGroup))
|
|
|
|
try {
|
|
const { id } = await apiFetch<CreateGroupResponse>({
|
|
path: '/api/group',
|
|
method: 'post',
|
|
body: {
|
|
name,
|
|
registration,
|
|
about,
|
|
imageUrl,
|
|
coverImageUrl,
|
|
iconImageUrl,
|
|
theme,
|
|
},
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.CreateGroup, true))
|
|
|
|
return id
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.CreateGroup, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface RegisterOptions {
|
|
id: string
|
|
email: string
|
|
password: string
|
|
name?: string
|
|
imageUrl?: string
|
|
coverImageUrl?: string
|
|
requiresApproval: boolean
|
|
privacy: string
|
|
group?: string
|
|
theme: string
|
|
}
|
|
|
|
interface RegisterResponse {
|
|
id: string
|
|
access: string
|
|
refresh: string
|
|
}
|
|
|
|
export const register = (options: RegisterOptions): AppThunkAction<string> => async dispatch => {
|
|
const { id, email, password, name, imageUrl, coverImageUrl, requiresApproval, privacy, group, theme } = options
|
|
|
|
dispatch(startRequest(RequestKey.Register))
|
|
|
|
try {
|
|
const response = await apiFetch<RegisterResponse>({
|
|
path: '/api/register',
|
|
method: 'post',
|
|
body: {
|
|
id,
|
|
email,
|
|
password,
|
|
name,
|
|
imageUrl,
|
|
coverImageUrl,
|
|
requiresApproval,
|
|
privacy,
|
|
group,
|
|
theme,
|
|
},
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.Register, true))
|
|
|
|
localStorage.setItem(LOCAL_STORAGE_ACCESS_TOKEN_KEY, response.access)
|
|
localStorage.setItem(LOCAL_STORAGE_REFRESH_TOKEN_KEY, response.refresh)
|
|
|
|
return response.id
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.Register, false))
|
|
throw err
|
|
}
|
|
}
|