// registration.ts // Copyright (C) 2020 Dwayne Harris // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . import { Action } from 'redux' import { apiFetch } from '../api' import { setFieldNotification } from '../actions/forms' import { startRequest, finishRequest } from '../actions/requests' import { LOCAL_STORAGE_ACCESS_TOKEN_KEY, LOCAL_STORAGE_REFRESH_TOKEN_KEY, } from '../constants' import { AppThunkAction, NotificationType, RequestKey, AvailabilityResponse } from '../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({ path: '/v1/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({ path: '/v1/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 => async dispatch => { const { name, registration, about, imageUrl, coverImageUrl, iconImageUrl, theme } = options dispatch(startRequest(RequestKey.CreateGroup)) try { const { id } = await apiFetch({ path: '/v1/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 => async dispatch => { const { id, email, password, name, imageUrl, coverImageUrl, requiresApproval, privacy, group, theme } = options dispatch(startRequest(RequestKey.Register)) try { const response = await apiFetch({ path: '/v1/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 } }