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
106 lines
3.4 KiB
// composer.ts
|
|
// Copyright (C) 2020 Dwayne Harris
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
import { Action } from 'redux'
|
|
|
|
import { setEntities } from '../actions/entities'
|
|
import { startRequest, finishRequest } from '../actions/requests'
|
|
import { apiFetch } from '../api'
|
|
import { normalize } from '../utils/normalization'
|
|
import { AppThunkAction, Installation, RequestKey, EntityType, Settings } from '../types'
|
|
|
|
export interface SetInstallationsAction extends Action {
|
|
type: 'COMPOSER_SET_INSTALLATIONS'
|
|
payload: string[]
|
|
}
|
|
|
|
export interface SetSelectedInstallationAction extends Action {
|
|
type: 'COMPOSER_SET_SELECTED_INSTALLATION'
|
|
payload?: string
|
|
}
|
|
|
|
export interface SetHeightAction extends Action {
|
|
type: 'COMPOSER_SET_HEIGHT',
|
|
payload: number
|
|
}
|
|
|
|
export interface SetErrorAction extends Action {
|
|
type: 'COMPOSER_SET_ERROR',
|
|
payload?: string
|
|
}
|
|
|
|
export type ComposerActions = SetInstallationsAction | SetSelectedInstallationAction | SetHeightAction | SetErrorAction
|
|
|
|
export const setInstallations = (installations: string[]): SetInstallationsAction => ({
|
|
type: 'COMPOSER_SET_INSTALLATIONS',
|
|
payload: installations,
|
|
})
|
|
|
|
export const setSelectedInstallation = (installation?: string): SetSelectedInstallationAction => ({
|
|
type: 'COMPOSER_SET_SELECTED_INSTALLATION',
|
|
payload: installation,
|
|
})
|
|
|
|
export const setHeight = (height: number): SetHeightAction => ({
|
|
type: 'COMPOSER_SET_HEIGHT',
|
|
payload: height,
|
|
})
|
|
|
|
export const setError = (error?: string): SetErrorAction => ({
|
|
type: 'COMPOSER_SET_ERROR',
|
|
payload: error,
|
|
})
|
|
|
|
interface FetchInstallationsResponse {
|
|
installations: Installation[]
|
|
}
|
|
|
|
export const fetchInstallations = (): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchInstallations))
|
|
|
|
try {
|
|
const response = await apiFetch<FetchInstallationsResponse>({
|
|
path: '/v1/installations',
|
|
})
|
|
|
|
const result = normalize(response.installations, EntityType.Installation)
|
|
|
|
dispatch(setEntities(result.entities))
|
|
dispatch(setInstallations(result.keys))
|
|
dispatch(finishRequest(RequestKey.FetchInstallations, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchInstallations, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const saveInstallationSettings = (id: string, settings: Settings): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.UpdateInstallationSettings))
|
|
|
|
try {
|
|
await apiFetch({
|
|
path: `/v1/installation/${id}/settings`,
|
|
method: 'put',
|
|
body: { settings },
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.UpdateInstallationSettings, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.UpdateInstallationSettings, false))
|
|
throw err
|
|
}
|
|
}
|