1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-31 00:19:36 +02:00
flame/client/src/store/actions/app.ts

102 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-05-10 19:02:16 +02:00
import axios from 'axios';
import { Dispatch } from 'redux';
import { ActionTypes } from './actionTypes';
2021-05-19 18:26:57 +02:00
import { App, ApiResponse, NewApp } from '../../interfaces';
export interface GetAppsAction<T> {
2021-05-12 17:31:25 +02:00
type: ActionTypes.getApps | ActionTypes.getAppsSuccess | ActionTypes.getAppsError;
payload: T;
}
2021-05-10 19:02:16 +02:00
export const getApps = () => async (dispatch: Dispatch) => {
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
dispatch<GetAppsAction<App[]>>({
type: ActionTypes.getAppsSuccess,
2021-05-10 19:02:16 +02:00
payload: res.data.data
})
} catch (err) {
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);
}
}
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}`);
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
}