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.
165 lines
4.8 KiB
165 lines
4.8 KiB
import { apiFetch } from 'src/api'
|
|
import { setEntities } from 'src/actions/entities'
|
|
import { startRequest, finishRequest } from 'src/actions/requests'
|
|
import { setFieldNotification } from 'src/actions/forms'
|
|
import { objectToQuerystring } from 'src/utils'
|
|
import { normalize } from 'src/utils/normalization'
|
|
|
|
import { AppThunkAction, RequestKey, EntityType, App, AvailabilityResponse, NotificationType, AppThunkDispatch }from 'src/types'
|
|
|
|
interface AppsResponse {
|
|
apps: App[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchApps = (sort?: string, continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchApps))
|
|
|
|
try {
|
|
const response = await apiFetch<AppsResponse>({
|
|
path: `/api/apps?${objectToQuerystring({ sort, continuation })}`,
|
|
})
|
|
|
|
const apps = normalize(response.apps, EntityType.App)
|
|
|
|
dispatch(setEntities(apps.entities))
|
|
dispatch(finishRequest(RequestKey.FetchApps, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchApps, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const fetchSelfApps = (sort?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchSelfApps))
|
|
|
|
try {
|
|
const response = await apiFetch<AppsResponse>({
|
|
path: `/api/self/apps?${objectToQuerystring({ sort })}`,
|
|
})
|
|
|
|
const apps = normalize(response.apps, EntityType.App)
|
|
|
|
dispatch(setEntities(apps.entities))
|
|
dispatch(finishRequest(RequestKey.FetchSelfApps, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchSelfApps, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const checkAppAvailability = (name: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchAppAvailability))
|
|
|
|
try {
|
|
const { id, available } = await apiFetch<AvailabilityResponse>({
|
|
path: '/api/app/available',
|
|
method: 'post',
|
|
body: {
|
|
name,
|
|
},
|
|
})
|
|
|
|
if (available) {
|
|
dispatch(setFieldNotification('name', NotificationType.Success, `${id} is available`))
|
|
} else {
|
|
dispatch(setFieldNotification('name', NotificationType.Error, `${id} isn't available`))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchAppAvailability, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchAppAvailability, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface CreateAppOptions {
|
|
name: string
|
|
about?: string
|
|
websiteUrl?: string
|
|
companyName?: string
|
|
version: string
|
|
composerUrl?: string
|
|
rendererUrl?: string
|
|
}
|
|
|
|
interface CreateAppResponse {
|
|
id: string
|
|
}
|
|
|
|
export const createApp = (options: CreateAppOptions): AppThunkAction<string> => async dispatch => {
|
|
const { name, about, websiteUrl, companyName, version, composerUrl, rendererUrl } = options
|
|
dispatch(startRequest(RequestKey.CreateApp))
|
|
|
|
try {
|
|
const { id } = await apiFetch<CreateAppResponse>({
|
|
path: '/api/app',
|
|
method: 'post',
|
|
body: {
|
|
name,
|
|
about,
|
|
websiteUrl,
|
|
companyName,
|
|
version,
|
|
composerUrl,
|
|
rendererUrl,
|
|
},
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.CreateApp, true))
|
|
return id
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.CreateApp, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const fetchApp = (id: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchApp))
|
|
|
|
try {
|
|
const app = await apiFetch<App>({
|
|
path: `/api/app/${id}`,
|
|
method: 'get',
|
|
})
|
|
|
|
const apps = normalize([app], EntityType.App)
|
|
dispatch(setEntities(apps.entities))
|
|
dispatch(finishRequest(RequestKey.FetchApp, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchApp, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const installApp = (id: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.InstallApp))
|
|
|
|
try {
|
|
await apiFetch({
|
|
path: `/api/app/${id}/install`,
|
|
method: 'post'
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.InstallApp, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.InstallApp, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export const uninstallApp = (id: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.UninstallApp))
|
|
|
|
try {
|
|
await apiFetch({
|
|
path: `/api/app/${id}/uninstall`,
|
|
method: 'post'
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.UninstallApp, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.UninstallApp, false))
|
|
throw err
|
|
}
|
|
}
|