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.
52 lines
1.7 KiB
52 lines
1.7 KiB
import { Action } from 'redux'
|
|
|
|
import { setEntities } from 'src/actions/entities'
|
|
import { startRequest, finishRequest } from 'src/actions/requests'
|
|
import { apiFetch } from 'src/api'
|
|
import { normalize } from 'src/utils/normalization'
|
|
import { AppThunkAction, Installation, RequestKey, EntityType } from 'src/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 type ComposerActions = SetInstallationsAction | SetSelectedInstallationAction
|
|
|
|
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,
|
|
})
|
|
|
|
interface FetchInstallationsResponse {
|
|
installations: Installation[]
|
|
}
|
|
|
|
export const fetchInstallations = (): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchInstallations))
|
|
|
|
try {
|
|
const response = await apiFetch<FetchInstallationsResponse>({
|
|
path: '/api/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
|
|
}
|
|
}
|