import axios from 'axios'; import { Dispatch } from 'redux'; import { ActionTypes } from './actionTypes'; import { App, ApiResponse, NewApp } from '../../interfaces'; export interface GetAppsAction { type: ActionTypes.getApps | ActionTypes.getAppsSuccess | ActionTypes.getAppsError; payload: T; } export const getApps = () => async (dispatch: Dispatch) => { dispatch>({ type: ActionTypes.getApps, payload: undefined }); try { const res = await axios.get>('/api/apps'); dispatch>({ type: ActionTypes.getAppsSuccess, payload: res.data.data }) } catch (err) { dispatch>({ type: ActionTypes.getAppsError, payload: err.data.data }) } } export interface PinAppAction { type: ActionTypes.pinApp; payload: App; } export const pinApp = (id: number, isPinned: boolean) => async (dispatch: Dispatch) => { try { const res = await axios.put>(`/api/apps/${id}`, { isPinned: !isPinned }); dispatch({ 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 { const res = await axios.post>('/api/apps', formData); dispatch({ 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 { const res = await axios.delete>(`/api/apps/${id}`); dispatch({ type: ActionTypes.deleteApp, payload: id }) } catch (err) { console.log(err); } } export interface UpdateAppAction { type: ActionTypes.updateApp; payload: App; } export const updateApp = (id: number, formData: NewApp) => async (dispatch: Dispatch) => { try { const res = await axios.put>(`/api/apps/${id}`, formData); dispatch({ type: ActionTypes.updateApp, payload: res.data.data }) } catch (err) { console.log(err); } }