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.
62 lines
2.1 KiB
62 lines
2.1 KiB
import { Action } from 'redux'
|
|
import { setFieldNotification } from 'src/actions/forms'
|
|
import { startRequest, finishRequest } from 'src/actions/requests'
|
|
import { fetchGroupAvailability } from 'src/api/registration'
|
|
import { AppThunkAction } from 'src/types'
|
|
|
|
const FETCH_GROUP_AVAILABILITY_ID = 'FETCH_GROUP_AVAILABILITY'
|
|
const FETCH_USER_AVAILABILITY_ID = 'FETCH_USER_AVAILABILITY'
|
|
|
|
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 => {
|
|
return async dispatch => {
|
|
dispatch(startRequest(FETCH_GROUP_AVAILABILITY_ID))
|
|
|
|
try {
|
|
const response = await fetchGroupAvailability(name)
|
|
|
|
if (response.available) {
|
|
dispatch(setFieldNotification('group-name', 'success', `${response.id} is available`))
|
|
} else {
|
|
dispatch(setFieldNotification('group-name', 'error', `${response.id} isn't available`))
|
|
}
|
|
|
|
dispatch(finishRequest(FETCH_GROUP_AVAILABILITY_ID, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(FETCH_GROUP_AVAILABILITY_ID, false))
|
|
throw err
|
|
}
|
|
}
|
|
}
|
|
|
|
export const checkUserAvailability = (name: string): AppThunkAction => {
|
|
return async dispatch => {
|
|
dispatch(startRequest(FETCH_USER_AVAILABILITY_ID))
|
|
|
|
try {
|
|
const response = await fetchGroupAvailability(name)
|
|
|
|
if (response.available) {
|
|
dispatch(setFieldNotification('user-id', 'success', `${response.id} is available`))
|
|
} else {
|
|
dispatch(setFieldNotification('user-id', 'error', `${response.id} isn't available`))
|
|
}
|
|
|
|
dispatch(finishRequest(FETCH_USER_AVAILABILITY_ID, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(FETCH_USER_AVAILABILITY_ID, false))
|
|
throw err
|
|
}
|
|
}
|
|
}
|