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

171 lines
4.6 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. import { Action } from 'redux'
  2. import { apiFetch } from '../api'
  3. import { setFieldNotification } from '../actions/forms'
  4. import { startRequest, finishRequest } from '../actions/requests'
  5. import {
  6. LOCAL_STORAGE_ACCESS_TOKEN_KEY,
  7. LOCAL_STORAGE_REFRESH_TOKEN_KEY,
  8. } from '../constants'
  9. import { AppThunkAction, NotificationType, RequestKey, AvailabilityResponse } from '../types'
  10. export interface SetStepAction extends Action {
  11. type: 'REGISTRATION_SET_STEP'
  12. payload: number
  13. }
  14. export type RegistrationActions = SetStepAction
  15. export const setStep = (step: number): SetStepAction => ({
  16. type: 'REGISTRATION_SET_STEP',
  17. payload: step,
  18. })
  19. export const checkGroupAvailability = (name: string): AppThunkAction => async dispatch => {
  20. dispatch(startRequest(RequestKey.FetchGroupAvailability))
  21. try {
  22. const { id, available } = await apiFetch<AvailabilityResponse>({
  23. path: '/v1/group/available',
  24. method: 'post',
  25. body: {
  26. name,
  27. },
  28. })
  29. if (available) {
  30. dispatch(setFieldNotification('group-name', NotificationType.Success, `${id} is available`))
  31. } else {
  32. dispatch(setFieldNotification('group-name', NotificationType.Error, `${id} isn't available`))
  33. }
  34. dispatch(finishRequest(RequestKey.FetchGroupAvailability, true))
  35. } catch (err) {
  36. dispatch(finishRequest(RequestKey.FetchGroupAvailability, false))
  37. throw err
  38. }
  39. }
  40. export const checkUserAvailability = (name: string): AppThunkAction => async dispatch => {
  41. dispatch(startRequest(RequestKey.FetchUserAvailability))
  42. try {
  43. const { id, available } = await apiFetch<AvailabilityResponse>({
  44. path: '/v1/user/available',
  45. method: 'post',
  46. body: {
  47. name,
  48. },
  49. })
  50. if (available) {
  51. dispatch(setFieldNotification('user-id', NotificationType.Success, `${id} is available`))
  52. } else {
  53. dispatch(setFieldNotification('user-id', NotificationType.Error, `${id} isn't available`))
  54. }
  55. dispatch(finishRequest(RequestKey.FetchUserAvailability, true))
  56. } catch (err) {
  57. dispatch(finishRequest(RequestKey.FetchUserAvailability, false))
  58. throw err
  59. }
  60. }
  61. interface CreateGroupOptions {
  62. name: string
  63. registration: string
  64. about?: string
  65. imageUrl?: string
  66. coverImageUrl?: string
  67. iconImageUrl?: string
  68. theme: string
  69. }
  70. interface CreateGroupResponse {
  71. id: string
  72. }
  73. export const createGroup = (options: CreateGroupOptions): AppThunkAction<string> => async dispatch => {
  74. const { name, registration, about, imageUrl, coverImageUrl, iconImageUrl, theme } = options
  75. dispatch(startRequest(RequestKey.CreateGroup))
  76. try {
  77. const { id } = await apiFetch<CreateGroupResponse>({
  78. path: '/v1/group',
  79. method: 'post',
  80. body: {
  81. name,
  82. registration,
  83. about,
  84. imageUrl,
  85. coverImageUrl,
  86. iconImageUrl,
  87. theme,
  88. },
  89. })
  90. dispatch(finishRequest(RequestKey.CreateGroup, true))
  91. return id
  92. } catch (err) {
  93. dispatch(finishRequest(RequestKey.CreateGroup, false))
  94. throw err
  95. }
  96. }
  97. interface RegisterOptions {
  98. id: string
  99. email: string
  100. password: string
  101. name?: string
  102. imageUrl?: string
  103. coverImageUrl?: string
  104. requiresApproval: boolean
  105. privacy: string
  106. group?: string
  107. theme: string
  108. }
  109. interface RegisterResponse {
  110. id: string
  111. access: string
  112. refresh: string
  113. }
  114. export const register = (options: RegisterOptions): AppThunkAction<string> => async dispatch => {
  115. const { id, email, password, name, imageUrl, coverImageUrl, requiresApproval, privacy, group, theme } = options
  116. dispatch(startRequest(RequestKey.Register))
  117. try {
  118. const response = await apiFetch<RegisterResponse>({
  119. path: '/v1/register',
  120. method: 'post',
  121. body: {
  122. id,
  123. email,
  124. password,
  125. name,
  126. imageUrl,
  127. coverImageUrl,
  128. requiresApproval,
  129. privacy,
  130. group,
  131. theme,
  132. },
  133. })
  134. dispatch(finishRequest(RequestKey.Register, true))
  135. localStorage.setItem(LOCAL_STORAGE_ACCESS_TOKEN_KEY, response.access)
  136. localStorage.setItem(LOCAL_STORAGE_REFRESH_TOKEN_KEY, response.refresh)
  137. return response.id
  138. } catch (err) {
  139. dispatch(finishRequest(RequestKey.Register, false))
  140. throw err
  141. }
  142. }