@ -7,10 +7,9 @@ import { startRequest, finishRequest } from 'src/actions/requests'
import {
import {
LOCAL_STORAGE_ACCESS_TOKEN_KEY ,
LOCAL_STORAGE_ACCESS_TOKEN_KEY ,
LOCAL_STORAGE_REFRESH_TOKEN_KEY ,
LOCAL_STORAGE_REFRESH_TOKEN_KEY ,
REQUEST_KEYS ,
} from 'src/constants'
} from 'src/constants'
import { AppThunkAction } from 'src/types'
import { AppThunkAction , NotificationType , RequestKey } from 'src/types'
export interface SetStepAction extends Action {
export interface SetStepAction extends Action {
type : 'REGISTRATION_SET_STEP'
type : 'REGISTRATION_SET_STEP'
@ -30,7 +29,7 @@ export const setStep = (step: number): SetStepAction => ({
} )
} )
export const checkGroupAvailability = ( name : string ) : AppThunkAction = > async dispatch = > {
export const checkGroupAvailability = ( name : string ) : AppThunkAction = > async dispatch = > {
dispatch ( startRequest ( REQUEST_KEYS . FETCH_GROUP_AVAILABILITY ) )
dispatch ( startRequest ( RequestKey . FetchGroupAvailability ) )
try {
try {
const { id , available } = await apiFetch < AvailabilityResponse > ( {
const { id , available } = await apiFetch < AvailabilityResponse > ( {
@ -42,20 +41,20 @@ export const checkGroupAvailability = (name: string): AppThunkAction => async di
} )
} )
if ( available ) {
if ( available ) {
dispatch ( setFieldNotification ( 'group-name' , 'success' , ` ${ id } is available ` ) )
dispatch ( setFieldNotification ( 'group-name' , NotificationType . Success , ` ${ id } is available ` ) )
} else {
} else {
dispatch ( setFieldNotification ( 'group-name' , 'error' , ` ${ id } isn't available ` ) )
dispatch ( setFieldNotification ( 'group-name' , NotificationType . Error , ` ${ id } isn't available ` ) )
}
}
dispatch ( finishRequest ( REQUEST_KEYS . FETCH_GROUP_AVAILABILITY , true ) )
dispatch ( finishRequest ( RequestKey . FetchGroupAvailability , true ) )
} catch ( err ) {
} catch ( err ) {
dispatch ( finishRequest ( REQUEST_KEYS . FETCH_GROUP_AVAILABILITY , false ) )
dispatch ( finishRequest ( RequestKey . FetchGroupAvailability , false ) )
throw err
throw err
}
}
}
}
export const checkUserAvailability = ( name : string ) : AppThunkAction = > async dispatch = > {
export const checkUserAvailability = ( name : string ) : AppThunkAction = > async dispatch = > {
dispatch ( startRequest ( REQUEST_KEYS . FETCH_USER_AVAILABILITY ) )
dispatch ( startRequest ( RequestKey . FetchUserAvailability ) )
try {
try {
const { id , available } = await apiFetch < AvailabilityResponse > ( {
const { id , available } = await apiFetch < AvailabilityResponse > ( {
@ -67,14 +66,14 @@ export const checkUserAvailability = (name: string): AppThunkAction => async dis
} )
} )
if ( available ) {
if ( available ) {
dispatch ( setFieldNotification ( 'user-id' , 'success' , ` ${ id } is available ` ) )
dispatch ( setFieldNotification ( 'user-id' , NotificationType . Success , ` ${ id } is available ` ) )
} else {
} else {
dispatch ( setFieldNotification ( 'user-id' , 'error' , ` ${ id } isn't available ` ) )
dispatch ( setFieldNotification ( 'user-id' , NotificationType . Error , ` ${ id } isn't available ` ) )
}
}
dispatch ( finishRequest ( REQUEST_KEYS . FETCH_USER_AVAILABILITY , true ) )
dispatch ( finishRequest ( RequestKey . FetchUserAvailability , true ) )
} catch ( err ) {
} catch ( err ) {
dispatch ( finishRequest ( REQUEST_KEYS . FETCH_USER_AVAILABILITY , false ) )
dispatch ( finishRequest ( RequestKey . FetchUserAvailability , false ) )
throw err
throw err
}
}
}
}
@ -92,7 +91,7 @@ interface CreateGroupResponse {
export const createGroup = ( options : CreateGroupOptions ) : AppThunkAction < string > = > async dispatch = > {
export const createGroup = ( options : CreateGroupOptions ) : AppThunkAction < string > = > async dispatch = > {
const { name , registration , about } = options
const { name , registration , about } = options
dispatch ( startRequest ( REQUEST_KEYS . CREATE_GROUP ) )
dispatch ( startRequest ( RequestKey . CreateGroup ) )
try {
try {
const { id } = await apiFetch < CreateGroupResponse > ( {
const { id } = await apiFetch < CreateGroupResponse > ( {
@ -105,11 +104,11 @@ export const createGroup = (options: CreateGroupOptions): AppThunkAction<string>
} ,
} ,
} )
} )
dispatch ( finishRequest ( REQUEST_KEYS . CREATE_GROUP , true ) )
dispatch ( finishRequest ( RequestKey . CreateGroup , true ) )
return id
return id
} catch ( err ) {
} catch ( err ) {
dispatch ( finishRequest ( REQUEST_KEYS . CREATE_GROUP , false ) )
dispatch ( finishRequest ( RequestKey . CreateGroup , false ) )
throw err
throw err
}
}
}
}
@ -131,7 +130,7 @@ interface RegisterResponse {
export const register = ( options : RegisterOptions ) : AppThunkAction < string > = > async dispatch = > {
export const register = ( options : RegisterOptions ) : AppThunkAction < string > = > async dispatch = > {
const { id , email , password , name , group } = options
const { id , email , password , name , group } = options
dispatch ( startRequest ( REQUEST_KEYS . REGISTER ) )
dispatch ( startRequest ( RequestKey . Register ) )
try {
try {
const response = await apiFetch < RegisterResponse > ( {
const response = await apiFetch < RegisterResponse > ( {
@ -146,14 +145,14 @@ export const register = (options: RegisterOptions): AppThunkAction<string> => as
} ,
} ,
} )
} )
dispatch ( finishRequest ( REQUEST_KEYS . REGISTER , true ) )
dispatch ( finishRequest ( RequestKey . Register , true ) )
localStorage . setItem ( LOCAL_STORAGE_ACCESS_TOKEN_KEY , response . access )
localStorage . setItem ( LOCAL_STORAGE_ACCESS_TOKEN_KEY , response . access )
localStorage . setItem ( LOCAL_STORAGE_REFRESH_TOKEN_KEY , response . refresh )
localStorage . setItem ( LOCAL_STORAGE_REFRESH_TOKEN_KEY , response . refresh )
return response . id
return response . id
} catch ( err ) {
} catch ( err ) {
dispatch ( finishRequest ( REQUEST_KEYS . REGISTER , false ) )
dispatch ( finishRequest ( RequestKey . Register , false ) )
throw err
throw err
}
}
}
}