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

267 lines
7.8 KiB

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
4 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
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
5 years ago
5 years ago
5 years ago
5 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
4 years ago
4 years ago
4 years ago
  1. import { apiFetch } from '../api'
  2. import { setEntities } from '../actions/entities'
  3. import { startRequest, finishRequest } from '../actions/requests'
  4. import { setFieldNotification } from '../actions/forms'
  5. import { listSet, listAppend } from '../actions/lists'
  6. import { objectToQuerystring } from '../utils'
  7. import { normalize } from '../utils/normalization'
  8. import { EntityListKey, Entity } from '../types'
  9. import { AppThunkAction, RequestKey, EntityType, App, AvailabilityResponse, NotificationType }from '../types'
  10. interface AppsResponse {
  11. apps: App[]
  12. continuation?: string
  13. }
  14. export const fetchApps = (sort?: string, continuation?: string): AppThunkAction => async dispatch => {
  15. dispatch(startRequest(RequestKey.FetchApps))
  16. try {
  17. const response = await apiFetch<AppsResponse>({
  18. path: `/v1/apps?${objectToQuerystring({ sort, continuation })}`,
  19. })
  20. const apps = normalize(response.apps, EntityType.App)
  21. dispatch(setEntities(apps.entities))
  22. if (continuation) {
  23. dispatch(listAppend(EntityListKey.Apps, apps.keys, response.continuation))
  24. } else {
  25. dispatch(listSet(EntityListKey.Apps, apps.keys, response.continuation))
  26. }
  27. dispatch(finishRequest(RequestKey.FetchApps, true))
  28. } catch (err) {
  29. dispatch(finishRequest(RequestKey.FetchApps, false))
  30. throw err
  31. }
  32. }
  33. export const fetchCreatedApps = (sort?: string): AppThunkAction => async dispatch => {
  34. dispatch(startRequest(RequestKey.FetchSelfApps))
  35. try {
  36. const response = await apiFetch<AppsResponse>({
  37. path: `/v1/self/apps?${objectToQuerystring({ sort })}`,
  38. })
  39. const apps = normalize(response.apps, EntityType.App)
  40. dispatch(setEntities(apps.entities))
  41. dispatch(listSet(EntityListKey.CreatedApps, apps.keys))
  42. dispatch(finishRequest(RequestKey.FetchSelfApps, true))
  43. } catch (err) {
  44. dispatch(finishRequest(RequestKey.FetchSelfApps, false))
  45. throw err
  46. }
  47. }
  48. export const checkAppAvailability = (name: string): AppThunkAction => async dispatch => {
  49. dispatch(startRequest(RequestKey.FetchAppAvailability))
  50. try {
  51. const { id, available } = await apiFetch<AvailabilityResponse>({
  52. path: '/v1/app/available',
  53. method: 'post',
  54. body: {
  55. name,
  56. },
  57. })
  58. if (available) {
  59. dispatch(setFieldNotification('name', NotificationType.Success, `${id} is available`))
  60. } else {
  61. dispatch(setFieldNotification('name', NotificationType.Error, `${id} isn't available`))
  62. }
  63. dispatch(finishRequest(RequestKey.FetchAppAvailability, true))
  64. } catch (err) {
  65. dispatch(finishRequest(RequestKey.FetchAppAvailability, false))
  66. throw err
  67. }
  68. }
  69. interface AppOptions {
  70. name: string
  71. about?: string
  72. websiteUrl?: string
  73. companyName?: string
  74. version: string
  75. composerUrl?: string
  76. rendererUrl?: string
  77. imageUrl?: string
  78. coverImageUrl?: string
  79. iconImageUrl?: string
  80. }
  81. interface CreateAppResponse {
  82. id: string
  83. }
  84. export const createApp = (options: AppOptions): AppThunkAction<string> => async dispatch => {
  85. const { name, about, websiteUrl, companyName, version, composerUrl, rendererUrl, imageUrl, coverImageUrl, iconImageUrl } = options
  86. dispatch(startRequest(RequestKey.CreateApp))
  87. try {
  88. const { id } = await apiFetch<CreateAppResponse>({
  89. path: '/v1/app',
  90. method: 'post',
  91. body: {
  92. name,
  93. about,
  94. websiteUrl,
  95. companyName,
  96. version,
  97. composerUrl,
  98. rendererUrl,
  99. imageUrl,
  100. coverImageUrl,
  101. iconImageUrl,
  102. },
  103. })
  104. dispatch(finishRequest(RequestKey.CreateApp, true))
  105. return id
  106. } catch (err) {
  107. dispatch(finishRequest(RequestKey.CreateApp, false))
  108. throw err
  109. }
  110. }
  111. export const updateApp = (id: string, options: AppOptions): AppThunkAction => async dispatch => {
  112. const { name, about, websiteUrl, companyName, version, composerUrl, rendererUrl, imageUrl, coverImageUrl, iconImageUrl } = options
  113. dispatch(startRequest(RequestKey.UpdateApp))
  114. try {
  115. await apiFetch({
  116. path: `/v1/app/${id}`,
  117. method: 'put',
  118. body: {
  119. name,
  120. about,
  121. websiteUrl,
  122. companyName,
  123. version,
  124. composerUrl,
  125. rendererUrl,
  126. imageUrl,
  127. coverImageUrl,
  128. iconImageUrl,
  129. },
  130. })
  131. dispatch(finishRequest(RequestKey.UpdateApp, true))
  132. } catch (err) {
  133. dispatch(finishRequest(RequestKey.UpdateApp, false))
  134. throw err
  135. }
  136. }
  137. export const fetchApp = (id: string): AppThunkAction => async dispatch => {
  138. dispatch(startRequest(RequestKey.FetchApp))
  139. try {
  140. const app = await apiFetch<App>({
  141. path: `/v1/app/${id}`,
  142. method: 'get',
  143. })
  144. const apps = normalize([app], EntityType.App)
  145. dispatch(setEntities(apps.entities))
  146. dispatch(finishRequest(RequestKey.FetchApp, true))
  147. } catch (err) {
  148. dispatch(finishRequest(RequestKey.FetchApp, false))
  149. throw err
  150. }
  151. }
  152. export const installApp = (id: string): AppThunkAction => async dispatch => {
  153. dispatch(startRequest(RequestKey.InstallApp))
  154. try {
  155. await apiFetch({
  156. path: `/v1/app/${id}/install`,
  157. method: 'post'
  158. })
  159. dispatch(finishRequest(RequestKey.InstallApp, true))
  160. } catch (err) {
  161. dispatch(finishRequest(RequestKey.InstallApp, false))
  162. throw err
  163. }
  164. }
  165. export const uninstallApp = (id: string): AppThunkAction => async dispatch => {
  166. dispatch(startRequest(RequestKey.UninstallApp))
  167. try {
  168. await apiFetch({
  169. path: `/v1/app/${id}/uninstall`,
  170. method: 'post'
  171. })
  172. dispatch(finishRequest(RequestKey.UninstallApp, true))
  173. } catch (err) {
  174. dispatch(finishRequest(RequestKey.UninstallApp, false))
  175. throw err
  176. }
  177. }
  178. interface FetchPendingAppsResponse {
  179. apps: Entity[]
  180. continuation?: string
  181. }
  182. export const fetchPendingApps = (continuation?: string): AppThunkAction => async dispatch => {
  183. dispatch(startRequest(RequestKey.FetchPendingApps))
  184. try {
  185. const response = await apiFetch<FetchPendingAppsResponse>({
  186. path: `/v1/apps/pending?${objectToQuerystring({ continuation })}`,
  187. method: 'get',
  188. })
  189. const apps = normalize(response.apps, EntityType.App)
  190. dispatch(setEntities(apps.entities))
  191. dispatch(listSet(EntityListKey.PendingApps, apps.keys, response.continuation))
  192. dispatch(finishRequest(RequestKey.FetchPendingApps, true))
  193. } catch (err) {
  194. dispatch(finishRequest(RequestKey.FetchPendingApps, false))
  195. throw err
  196. }
  197. }
  198. export const activateApp = (id: string): AppThunkAction => async dispatch => {
  199. dispatch(startRequest(RequestKey.ActivateApp))
  200. try {
  201. await apiFetch({
  202. path: `/v1/app/${id}/activate`,
  203. method: 'post',
  204. })
  205. dispatch(finishRequest(RequestKey.ActivateApp, true))
  206. } catch (err) {
  207. dispatch(finishRequest(RequestKey.ActivateApp, false))
  208. throw err
  209. }
  210. }
  211. export const setPreinstall = (id: string): AppThunkAction => async dispatch => {
  212. dispatch(startRequest(RequestKey.SetPreinstall))
  213. try {
  214. await apiFetch({
  215. path: `/v1/app/${id}/preinstall`,
  216. method: 'post',
  217. })
  218. dispatch(finishRequest(RequestKey.SetPreinstall, true))
  219. } catch (err) {
  220. dispatch(finishRequest(RequestKey.SetPreinstall, false))
  221. throw err
  222. }
  223. }