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

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