1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-08-02 01:15:17 +02:00
flame/client/src/store/actions/config.ts

113 lines
2.7 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
import { Dispatch } from 'redux';
import { ActionTypes } from './actionTypes';
2021-10-06 14:15:05 +02:00
import { Config, ApiResponse, Query } from '../../interfaces';
import { CreateNotificationAction } from './notification';
import { searchConfig } from '../../utility';
export interface GetConfigAction {
type: ActionTypes.getConfig;
payload: Config[];
}
export const getConfig = () => async (dispatch: Dispatch) => {
try {
const res = await axios.get<ApiResponse<Config[]>>('/api/config');
2021-10-06 14:15:05 +02:00
dispatch<GetConfigAction>({
type: ActionTypes.getConfig,
2021-10-06 14:15:05 +02:00
payload: res.data.data,
});
// Set custom page title if set
document.title = searchConfig('customTitle', 'Flame');
} catch (err) {
2021-10-06 14:15:05 +02:00
console.log(err);
}
2021-10-06 14:15:05 +02:00
};
export interface UpdateConfigAction {
type: ActionTypes.updateConfig;
payload: Config[];
}
export const updateConfig = (formData: any) => async (dispatch: Dispatch) => {
try {
const res = await axios.put<ApiResponse<Config[]>>('/api/config', formData);
2021-10-06 14:15:05 +02:00
dispatch<CreateNotificationAction>({
type: ActionTypes.createNotification,
payload: {
title: 'Success',
2021-10-06 14:15:05 +02:00
message: 'Settings updated',
},
});
dispatch<UpdateConfigAction>({
type: ActionTypes.updateConfig,
2021-10-06 14:15:05 +02:00
payload: res.data.data,
});
} catch (err) {
console.log(err);
}
2021-10-06 14:15:05 +02:00
};
export interface FetchQueriesAction {
type: ActionTypes.fetchQueries;
payload: Query[];
}
export const fetchQueries =
() => async (dispatch: Dispatch<FetchQueriesAction>) => {
try {
const res = await axios.get<ApiResponse<Query[]>>('/api/queries');
2021-10-06 14:15:05 +02:00
dispatch<FetchQueriesAction>({
type: ActionTypes.fetchQueries,
payload: res.data.data,
});
} catch (err) {
console.log(err);
}
};
export interface AddQueryAction {
type: ActionTypes.addQuery;
payload: Query;
}
export const addQuery =
(query: Query) => async (dispatch: Dispatch<AddQueryAction>) => {
try {
const res = await axios.post<ApiResponse<Query>>('/api/queries', query);
dispatch<AddQueryAction>({
type: ActionTypes.addQuery,
payload: res.data.data,
});
} catch (err) {
console.log(err);
}
};
export interface DeleteQueryAction {
type: ActionTypes.deleteQuery;
payload: Query[];
}
export const deleteQuery =
(prefix: string) => async (dispatch: Dispatch<DeleteQueryAction>) => {
try {
const res = await axios.delete<ApiResponse<Query[]>>(
`/api/queries/${prefix}`
);
dispatch<DeleteQueryAction>({
type: ActionTypes.deleteQuery,
payload: res.data.data,
});
} catch (err) {
console.log(err);
}
};