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-05-19 18:26:57 +02:00
|
|
|
import { App, ApiResponse, NewApp } from '../../interfaces';
|
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-05-11 16:44:05 +02:00
|
|
|
dispatch<GetAppsAction<string>>({
|
|
|
|
type: ActionTypes.getAppsError,
|
2021-05-10 19:02:16 +02:00
|
|
|
payload: err.data.data
|
|
|
|
})
|
|
|
|
}
|
2021-05-12 17:31:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface PinAppAction {
|
|
|
|
type: ActionTypes.pinApp;
|
|
|
|
payload: App;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const pinApp = (id: number, isPinned: boolean) => async (dispatch: Dispatch) => {
|
|
|
|
try {
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
dispatch<AddAppAction>({
|
|
|
|
type: ActionTypes.addAppSuccess,
|
|
|
|
payload: res.data.data
|
|
|
|
})
|
|
|
|
} 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-05-19 18:26:57 +02:00
|
|
|
const res = await axios.delete<ApiResponse<{}>>(`/api/apps/${id}`);
|
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);
|
|
|
|
|
|
|
|
dispatch<UpdateAppAction>({
|
|
|
|
type: ActionTypes.updateApp,
|
|
|
|
payload: res.data.data
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
2021-05-10 19:02:16 +02:00
|
|
|
}
|