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

89 lines
2.7 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
4 years ago
5 years ago
  1. import { Action } from 'redux'
  2. import { setEntities } from '../actions/entities'
  3. import { startRequest, finishRequest } from '../actions/requests'
  4. import { apiFetch } from '../api'
  5. import { normalize } from '../utils/normalization'
  6. import { AppThunkAction, Installation, RequestKey, EntityType, Settings } from '../types'
  7. export interface SetInstallationsAction extends Action {
  8. type: 'COMPOSER_SET_INSTALLATIONS'
  9. payload: string[]
  10. }
  11. export interface SetSelectedInstallationAction extends Action {
  12. type: 'COMPOSER_SET_SELECTED_INSTALLATION'
  13. payload?: string
  14. }
  15. export interface SetHeightAction extends Action {
  16. type: 'COMPOSER_SET_HEIGHT',
  17. payload: number
  18. }
  19. export interface SetErrorAction extends Action {
  20. type: 'COMPOSER_SET_ERROR',
  21. payload?: string
  22. }
  23. export type ComposerActions = SetInstallationsAction | SetSelectedInstallationAction | SetHeightAction | SetErrorAction
  24. export const setInstallations = (installations: string[]): SetInstallationsAction => ({
  25. type: 'COMPOSER_SET_INSTALLATIONS',
  26. payload: installations,
  27. })
  28. export const setSelectedInstallation = (installation?: string): SetSelectedInstallationAction => ({
  29. type: 'COMPOSER_SET_SELECTED_INSTALLATION',
  30. payload: installation,
  31. })
  32. export const setHeight = (height: number): SetHeightAction => ({
  33. type: 'COMPOSER_SET_HEIGHT',
  34. payload: height,
  35. })
  36. export const setError = (error?: string): SetErrorAction => ({
  37. type: 'COMPOSER_SET_ERROR',
  38. payload: error,
  39. })
  40. interface FetchInstallationsResponse {
  41. installations: Installation[]
  42. }
  43. export const fetchInstallations = (): AppThunkAction => async dispatch => {
  44. dispatch(startRequest(RequestKey.FetchInstallations))
  45. try {
  46. const response = await apiFetch<FetchInstallationsResponse>({
  47. path: '/v1/installations',
  48. })
  49. const result = normalize(response.installations, EntityType.Installation)
  50. dispatch(setEntities(result.entities))
  51. dispatch(setInstallations(result.keys))
  52. dispatch(finishRequest(RequestKey.FetchInstallations, true))
  53. } catch (err) {
  54. dispatch(finishRequest(RequestKey.FetchInstallations, false))
  55. throw err
  56. }
  57. }
  58. export const saveInstallationSettings = (id: string, settings: Settings): AppThunkAction => async dispatch => {
  59. dispatch(startRequest(RequestKey.UpdateInstallationSettings))
  60. try {
  61. await apiFetch({
  62. path: `/v1/installation/${id}/settings`,
  63. method: 'put',
  64. body: { settings },
  65. })
  66. dispatch(finishRequest(RequestKey.UpdateInstallationSettings, true))
  67. } catch (err) {
  68. dispatch(finishRequest(RequestKey.UpdateInstallationSettings, false))
  69. throw err
  70. }
  71. }