2021-05-10 19:02:16 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
import { Dispatch } from 'redux';
|
2021-05-11 16:44:05 +02:00
|
|
|
import { ActionTypes } from './actionTypes';
|
2021-06-18 10:38:05 +02:00
|
|
|
import { App, ApiResponse, NewApp, Config } from '../../interfaces';
|
2021-05-24 14:54:46 +02:00
|
|
|
import { CreateNotificationAction } from './notification';
|
2021-05-11 16:44:05 +02:00
|
|
|
|
|
|
|
export interface GetAppsAction<T> {
|
2021-05-12 17:31:25 +02:00
|
|
|
type: ActionTypes.getApps | ActionTypes.getAppsSuccess | ActionTypes.getAppsError;
|
|
|
|
payload: T;
|
2021-05-11 16:44:05 +02:00
|
|
|
}
|
2021-05-10 19:02:16 +02:00
|
|
|
|
|
|
|
export const getApps = () => async (dispatch: Dispatch) => {
|
2021-05-11 16:44:05 +02:00
|
|
|
dispatch<GetAppsAction<undefined>>({
|
|
|
|
type: ActionTypes.getApps,
|
|
|
|
payload: undefined
|
|
|
|
});
|
2021-05-10 19:02:16 +02:00
|
|
|
|
|
|
|
try {
|
2021-05-19 18:26:57 +02:00
|
|
|
const res = await axios.get<ApiResponse<App[]>>('/api/apps');
|
2021-05-10 19:02:16 +02:00
|
|
|
|
2021-05-11 16:44:05 +02:00
|
|
|
dispatch<GetAppsAction<App[]>>({
|
|
|
|
type: ActionTypes.getAppsSuccess,
|
2021-05-10 19:02:16 +02:00
|
|
|
payload: res.data.data
|
|
|
|
})
|
|
|
|
} catch (err) {
|
2021-06-10 13:05:55 +02:00
|
|
|
console.log(err);
|
2021-05-10 19:02:16 +02:00
|
|
|
}
|
2021-05-12 17:31:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface PinAppAction {
|
|
|
|
type: ActionTypes.pinApp;
|
|
|
|
payload: App;
|
|
|
|
}
|
|
|
|
|
2021-05-22 18:10:12 +02:00
|
|
|
export const pinApp = (app: App) => async (dispatch: Dispatch) => {
|
2021-05-12 17:31:25 +02:00
|
|
|
try {
|
2021-05-24 14:54:46 +02:00
|
|
|
const { id, isPinned, name } = app;
|
2021-05-19 18:26:57 +02:00
|
|
|
const res = await axios.put<ApiResponse<App>>(`/api/apps/${id}`, { isPinned: !isPinned });
|
2021-05-12 17:31:25 +02:00
|
|
|
|
2021-05-24 14:54:46 +02:00
|
|
|
const status = isPinned ? 'unpinned from Homescreen' : 'pinned to Homescreen';
|
|
|
|
|
|
|
|
dispatch<CreateNotificationAction>({
|
|
|
|
type: ActionTypes.createNotification,
|
|
|
|
payload: {
|
|
|
|
title: 'Success',
|
|
|
|
message: `App ${name} ${status}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-05-12 17:31:25 +02:00
|
|
|
dispatch<PinAppAction>({
|
|
|
|
type: ActionTypes.pinApp,
|
|
|
|
payload: res.data.data
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AddAppAction {
|
|
|
|
type: ActionTypes.addAppSuccess;
|
|
|
|
payload: App;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const addApp = (formData: NewApp) => async (dispatch: Dispatch) => {
|
|
|
|
try {
|
2021-05-19 18:26:57 +02:00
|
|
|
const res = await axios.post<ApiResponse<App>>('/api/apps', formData);
|
2021-05-12 17:31:25 +02:00
|
|
|
|
2021-05-24 14:54:46 +02:00
|
|
|
dispatch<CreateNotificationAction>({
|
|
|
|
type: ActionTypes.createNotification,
|
|
|
|
payload: {
|
|
|
|
title: 'Success',
|
|
|
|
message: `App ${formData.name} added`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-18 10:38:05 +02:00
|
|
|
await dispatch<AddAppAction>({
|
2021-05-12 17:31:25 +02:00
|
|
|
type: ActionTypes.addAppSuccess,
|
|
|
|
payload: res.data.data
|
|
|
|
})
|
2021-06-18 10:38:05 +02:00
|
|
|
|
|
|
|
// Sort apps
|
|
|
|
dispatch<any>(sortApps())
|
2021-05-12 17:31:25 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2021-05-13 18:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface DeleteAppAction {
|
|
|
|
type: ActionTypes.deleteApp,
|
|
|
|
payload: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export const deleteApp = (id: number) => async (dispatch: Dispatch) => {
|
|
|
|
try {
|
2021-06-13 01:06:42 +02:00
|
|
|
await axios.delete<ApiResponse<{}>>(`/api/apps/${id}`);
|
2021-05-13 18:23:12 +02:00
|
|
|
|
2021-05-24 14:54:46 +02:00
|
|
|
dispatch<CreateNotificationAction>({
|
|
|
|
type: ActionTypes.createNotification,
|
|
|
|
payload: {
|
|
|
|
title: 'Success',
|
|
|
|
message: 'App deleted'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-05-13 18:23:12 +02:00
|
|
|
dispatch<DeleteAppAction>({
|
|
|
|
type: ActionTypes.deleteApp,
|
|
|
|
payload: id
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2021-05-22 17:03:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface UpdateAppAction {
|
|
|
|
type: ActionTypes.updateApp;
|
|
|
|
payload: App;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const updateApp = (id: number, formData: NewApp) => async (dispatch: Dispatch) => {
|
|
|
|
try {
|
|
|
|
const res = await axios.put<ApiResponse<App>>(`/api/apps/${id}`, formData);
|
|
|
|
|
2021-05-24 14:54:46 +02:00
|
|
|
dispatch<CreateNotificationAction>({
|
|
|
|
type: ActionTypes.createNotification,
|
|
|
|
payload: {
|
|
|
|
title: 'Success',
|
|
|
|
message: `App ${formData.name} updated`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-18 10:38:05 +02:00
|
|
|
await dispatch<UpdateAppAction>({
|
2021-05-22 17:03:32 +02:00
|
|
|
type: ActionTypes.updateApp,
|
|
|
|
payload: res.data.data
|
|
|
|
})
|
2021-06-18 10:38:05 +02:00
|
|
|
|
|
|
|
// Sort apps
|
|
|
|
dispatch<any>(sortApps())
|
2021-05-22 17:03:32 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2021-06-15 16:02:57 +02:00
|
|
|
}
|
|
|
|
|
2021-06-18 10:38:05 +02:00
|
|
|
export interface ReorderAppsAction {
|
|
|
|
type: ActionTypes.reorderApps;
|
2021-06-15 16:02:57 +02:00
|
|
|
payload: App[]
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ReorderQuery {
|
|
|
|
apps: {
|
|
|
|
id: number;
|
|
|
|
orderId: number;
|
|
|
|
}[]
|
|
|
|
}
|
|
|
|
|
2021-06-18 10:38:05 +02:00
|
|
|
export const reorderApps = (apps: App[]) => async (dispatch: Dispatch) => {
|
2021-06-15 16:02:57 +02:00
|
|
|
try {
|
|
|
|
const updateQuery: ReorderQuery = { apps: [] }
|
|
|
|
|
|
|
|
apps.forEach((app, index) => updateQuery.apps.push({
|
|
|
|
id: app.id,
|
|
|
|
orderId: index + 1
|
|
|
|
}))
|
|
|
|
|
2021-06-18 12:09:59 +02:00
|
|
|
await axios.put<ApiResponse<{}>>('/api/apps/0/reorder', updateQuery);
|
2021-06-18 10:38:05 +02:00
|
|
|
|
|
|
|
dispatch<ReorderAppsAction>({
|
|
|
|
type: ActionTypes.reorderApps,
|
2021-06-15 16:02:57 +02:00
|
|
|
payload: apps
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2021-06-18 10:38:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface SortAppsAction {
|
|
|
|
type: ActionTypes.sortApps;
|
|
|
|
payload: {};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const sortApps = () => async (dispatch: Dispatch) => {
|
|
|
|
try {
|
|
|
|
const res = await axios.get<ApiResponse<Config>>('/api/config/useOrdering');
|
|
|
|
|
|
|
|
dispatch<SortAppsAction>({
|
|
|
|
type: ActionTypes.sortApps,
|
|
|
|
payload: res.data.data.value
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2021-05-10 19:02:16 +02:00
|
|
|
}
|