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

188 lines
5.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. // registration.ts
  2. // Copyright (C) 2020 Dwayne Harris
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. import { Action } from 'redux'
  14. import { apiFetch } from '../api'
  15. import { setFieldNotification } from '../actions/forms'
  16. import { startRequest, finishRequest } from '../actions/requests'
  17. import {
  18. LOCAL_STORAGE_ACCESS_TOKEN_KEY,
  19. LOCAL_STORAGE_REFRESH_TOKEN_KEY,
  20. } from '../constants'
  21. import { AppThunkAction, NotificationType, RequestKey, AvailabilityResponse } from '../types'
  22. export interface SetStepAction extends Action {
  23. type: 'REGISTRATION_SET_STEP'
  24. payload: number
  25. }
  26. export type RegistrationActions = SetStepAction
  27. export const setStep = (step: number): SetStepAction => ({
  28. type: 'REGISTRATION_SET_STEP',
  29. payload: step,
  30. })
  31. export const checkGroupAvailability = (name: string): AppThunkAction => async dispatch => {
  32. dispatch(startRequest(RequestKey.FetchGroupAvailability))
  33. try {
  34. const { id, available } = await apiFetch<AvailabilityResponse>({
  35. path: '/v1/group/available',
  36. method: 'post',
  37. body: {
  38. name,
  39. },
  40. })
  41. if (available) {
  42. dispatch(setFieldNotification('group-name', NotificationType.Success, `${id} is available`))
  43. } else {
  44. dispatch(setFieldNotification('group-name', NotificationType.Error, `${id} isn't available`))
  45. }
  46. dispatch(finishRequest(RequestKey.FetchGroupAvailability, true))
  47. } catch (err) {
  48. dispatch(finishRequest(RequestKey.FetchGroupAvailability, false))
  49. throw err
  50. }
  51. }
  52. export const checkUserAvailability = (name: string): AppThunkAction => async dispatch => {
  53. dispatch(startRequest(RequestKey.FetchUserAvailability))
  54. try {
  55. const { id, available } = await apiFetch<AvailabilityResponse>({
  56. path: '/v1/user/available',
  57. method: 'post',
  58. body: {
  59. name,
  60. },
  61. })
  62. if (available) {
  63. dispatch(setFieldNotification('user-id', NotificationType.Success, `${id} is available`))
  64. } else {
  65. dispatch(setFieldNotification('user-id', NotificationType.Error, `${id} isn't available`))
  66. }
  67. dispatch(finishRequest(RequestKey.FetchUserAvailability, true))
  68. } catch (err) {
  69. dispatch(finishRequest(RequestKey.FetchUserAvailability, false))
  70. throw err
  71. }
  72. }
  73. interface CreateGroupOptions {
  74. name: string
  75. registration: string
  76. about?: string
  77. imageUrl?: string
  78. coverImageUrl?: string
  79. iconImageUrl?: string
  80. theme: string
  81. }
  82. interface CreateGroupResponse {
  83. id: string
  84. }
  85. export const createGroup = (options: CreateGroupOptions): AppThunkAction<string> => async dispatch => {
  86. const { name, registration, about, imageUrl, coverImageUrl, iconImageUrl, theme } = options
  87. dispatch(startRequest(RequestKey.CreateGroup))
  88. try {
  89. const { id } = await apiFetch<CreateGroupResponse>({
  90. path: '/v1/group',
  91. method: 'post',
  92. body: {
  93. name,
  94. registration,
  95. about,
  96. imageUrl,
  97. coverImageUrl,
  98. iconImageUrl,
  99. theme,
  100. },
  101. })
  102. dispatch(finishRequest(RequestKey.CreateGroup, true))
  103. return id
  104. } catch (err) {
  105. dispatch(finishRequest(RequestKey.CreateGroup, false))
  106. throw err
  107. }
  108. }
  109. interface RegisterOptions {
  110. id: string
  111. email: string
  112. password: string
  113. name?: string
  114. imageUrl?: string
  115. coverImageUrl?: string
  116. requiresApproval: boolean
  117. privacy: string
  118. group?: string
  119. theme: string
  120. }
  121. interface RegisterResponse {
  122. id: string
  123. access: string
  124. refresh: string
  125. }
  126. export const register = (options: RegisterOptions): AppThunkAction<string> => async dispatch => {
  127. const { id, email, password, name, imageUrl, coverImageUrl, requiresApproval, privacy, group, theme } = options
  128. dispatch(startRequest(RequestKey.Register))
  129. try {
  130. const response = await apiFetch<RegisterResponse>({
  131. path: '/v1/register',
  132. method: 'post',
  133. body: {
  134. id,
  135. email,
  136. password,
  137. name,
  138. imageUrl,
  139. coverImageUrl,
  140. requiresApproval,
  141. privacy,
  142. group,
  143. theme,
  144. },
  145. })
  146. dispatch(finishRequest(RequestKey.Register, true))
  147. localStorage.setItem(LOCAL_STORAGE_ACCESS_TOKEN_KEY, response.access)
  148. localStorage.setItem(LOCAL_STORAGE_REFRESH_TOKEN_KEY, response.refresh)
  149. return response.id
  150. } catch (err) {
  151. dispatch(finishRequest(RequestKey.Register, false))
  152. throw err
  153. }
  154. }