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

240 lines
7.5 KiB

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
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
4 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
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
  1. // groups.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 { apiFetch } from '../api'
  14. import { setEntities } from '../actions/entities'
  15. import { listSet, listAppend } from '../actions/lists'
  16. import { startRequest, finishRequest } from '../actions/requests'
  17. import { objectToQuerystring } from '../utils'
  18. import { normalize } from '../utils/normalization'
  19. import { AppThunkAction, Entity, RequestKey, EntityType, User, EntityListKey } from '../types'
  20. export const fetchGroup = (id: string): AppThunkAction => {
  21. return async dispatch => {
  22. dispatch(startRequest(RequestKey.FetchGroup))
  23. try {
  24. const group = await apiFetch<Entity>({
  25. path: `/v1/group/${id}`
  26. })
  27. const groups = normalize([group], EntityType.Group)
  28. dispatch(setEntities(groups.entities))
  29. dispatch(finishRequest(RequestKey.FetchGroup, true))
  30. } catch (err) {
  31. dispatch(finishRequest(RequestKey.FetchGroup, false))
  32. throw err
  33. }
  34. }
  35. }
  36. interface GroupsResponse {
  37. groups: Entity[]
  38. continuation?: string
  39. }
  40. export const fetchGroups = (sort?: string, continuation?: string): AppThunkAction => async dispatch => {
  41. dispatch(startRequest(RequestKey.FetchGroups))
  42. try {
  43. const response = await apiFetch<GroupsResponse>({
  44. path: `/v1/groups?${objectToQuerystring({ sort, continuation })}`,
  45. })
  46. const groups = normalize(response.groups, EntityType.Group)
  47. dispatch(setEntities(groups.entities))
  48. if (continuation) {
  49. dispatch(listAppend(EntityListKey.Groups, groups.keys, response.continuation))
  50. } else {
  51. dispatch(listSet(EntityListKey.Groups, groups.keys, response.continuation))
  52. }
  53. dispatch(finishRequest(RequestKey.FetchGroups, true))
  54. } catch (err) {
  55. dispatch(finishRequest(RequestKey.FetchGroups, false))
  56. throw err
  57. }
  58. }
  59. interface GroupMembersResponse {
  60. members: User[]
  61. continuation?: string
  62. }
  63. export const fetchGroupMembers = (id: string, type?: string, continuation?: string): AppThunkAction => async dispatch => {
  64. dispatch(startRequest(RequestKey.FetchGroupMembers))
  65. try {
  66. const response = await apiFetch<GroupMembersResponse>({
  67. path: `/v1/group/${id}/members?${objectToQuerystring({ type, continuation })}`,
  68. })
  69. const users = normalize(response.members, EntityType.User)
  70. dispatch(setEntities(users.entities))
  71. if (continuation) {
  72. dispatch(listAppend(EntityListKey.GroupMembers, users.keys, response.continuation))
  73. } else {
  74. dispatch(listSet(EntityListKey.GroupMembers, users.keys, response.continuation))
  75. }
  76. dispatch(finishRequest(RequestKey.FetchGroupMembers, true))
  77. } catch (err) {
  78. dispatch(finishRequest(RequestKey.FetchGroupMembers, false))
  79. throw err
  80. }
  81. }
  82. interface GroupLogsResponse {
  83. logs: Entity[]
  84. continuation?: string
  85. }
  86. export const fetchLogs = (continuation?: string): AppThunkAction => async dispatch => {
  87. dispatch(startRequest(RequestKey.FetchGroupLogs))
  88. try {
  89. const response = await apiFetch<GroupLogsResponse>({
  90. path: `/v1/group/logs?${objectToQuerystring({ continuation })}`,
  91. })
  92. const logs = normalize(response.logs, EntityType.Log)
  93. dispatch(setEntities(logs.entities))
  94. if (continuation) {
  95. dispatch(listAppend(EntityListKey.Logs, logs.keys, response.continuation))
  96. } else {
  97. dispatch(listSet(EntityListKey.Logs, logs.keys, response.continuation))
  98. }
  99. dispatch(finishRequest(RequestKey.FetchGroupLogs, true))
  100. } catch (err) {
  101. dispatch(finishRequest(RequestKey.FetchGroupLogs, false))
  102. throw err
  103. }
  104. }
  105. interface CreateInvitationResponse {
  106. code: string
  107. }
  108. export const createInvitation = (expiration?: number, limit?: number): AppThunkAction<string> => async dispatch => {
  109. dispatch(startRequest(RequestKey.CreateInvitation))
  110. try {
  111. const response = await apiFetch<CreateInvitationResponse>({
  112. path: `/v1/group/invitation`,
  113. method: 'post',
  114. body: {
  115. expiration,
  116. limit,
  117. }
  118. })
  119. dispatch(finishRequest(RequestKey.CreateInvitation, true))
  120. return response.code
  121. } catch (err) {
  122. dispatch(finishRequest(RequestKey.CreateInvitation, false))
  123. throw err
  124. }
  125. }
  126. interface InvitationsResponse {
  127. invitations: Entity[]
  128. continuation?: string
  129. }
  130. export const fetchInvitations = (): AppThunkAction => async dispatch => {
  131. dispatch(startRequest(RequestKey.FetchInvitations))
  132. try {
  133. const response = await apiFetch<InvitationsResponse>({
  134. path: `/v1/group/invitations`,
  135. })
  136. const invitations = normalize(response.invitations, EntityType.Invitation)
  137. dispatch(setEntities(invitations.entities))
  138. dispatch(listSet(EntityListKey.Invitations, invitations.keys, response.continuation))
  139. dispatch(finishRequest(RequestKey.FetchInvitations, true))
  140. } catch (err) {
  141. dispatch(finishRequest(RequestKey.FetchInvitations, false))
  142. throw err
  143. }
  144. }
  145. export const updateGroup = (id: string, updates: object): AppThunkAction => async dispatch => {
  146. dispatch(startRequest(RequestKey.UpdateGroup))
  147. try {
  148. await apiFetch({
  149. path: `/v1/group/${id}`,
  150. method: 'put',
  151. body: updates,
  152. })
  153. dispatch(finishRequest(RequestKey.UpdateGroup, true))
  154. } catch (err) {
  155. dispatch(finishRequest(RequestKey.UpdateGroup, false))
  156. throw err
  157. }
  158. }
  159. interface FetchPendingGroupsResponse {
  160. groups: Entity[]
  161. continuation?: string
  162. }
  163. export const fetchPendingGroups = (continuation?: string): AppThunkAction => async dispatch => {
  164. dispatch(startRequest(RequestKey.FetchPendingGroups))
  165. try {
  166. const response = await apiFetch<FetchPendingGroupsResponse>({
  167. path: `/v1/groups/pending?${objectToQuerystring({ continuation })}`,
  168. method: 'get',
  169. })
  170. const groups = normalize(response.groups, EntityType.Group)
  171. dispatch(setEntities(groups.entities))
  172. dispatch(listSet(EntityListKey.PendingGroups, groups.keys, response.continuation))
  173. dispatch(finishRequest(RequestKey.FetchPendingGroups, true))
  174. } catch (err) {
  175. dispatch(finishRequest(RequestKey.FetchPendingGroups, false))
  176. throw err
  177. }
  178. }
  179. export const activateGroup = (id: string): AppThunkAction => async dispatch => {
  180. dispatch(startRequest(RequestKey.ActivateGroup))
  181. try {
  182. await apiFetch({
  183. path: `/v1/group/${id}/activate`,
  184. method: 'post',
  185. })
  186. dispatch(finishRequest(RequestKey.ActivateGroup, true))
  187. } catch (err) {
  188. dispatch(finishRequest(RequestKey.ActivateGroup, false))
  189. throw err
  190. }
  191. }