2021-06-13 00:16:57 +02:00
|
|
|
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';
|
2021-06-13 00:16:57 +02:00
|
|
|
import { CreateNotificationAction } from './notification';
|
2021-06-13 23:21:35 +02:00
|
|
|
import { searchConfig } from '../../utility';
|
2021-06-13 00:16:57 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-06-13 00:16:57 +02:00
|
|
|
dispatch<GetConfigAction>({
|
|
|
|
type: ActionTypes.getConfig,
|
2021-10-06 14:15:05 +02:00
|
|
|
payload: res.data.data,
|
|
|
|
});
|
2021-06-13 23:21:35 +02:00
|
|
|
|
|
|
|
// Set custom page title if set
|
|
|
|
document.title = searchConfig('customTitle', 'Flame');
|
2021-06-13 00:16:57 +02:00
|
|
|
} catch (err) {
|
2021-10-06 14:15:05 +02:00
|
|
|
console.log(err);
|
2021-06-13 00:16:57 +02:00
|
|
|
}
|
2021-10-06 14:15:05 +02:00
|
|
|
};
|
2021-06-13 00:16:57 +02:00
|
|
|
|
|
|
|
export interface UpdateConfigAction {
|
|
|
|
type: ActionTypes.updateConfig;
|
|
|
|
payload: Config[];
|
|
|
|
}
|
|
|
|
|
2021-06-13 23:21:35 +02:00
|
|
|
export const updateConfig = (formData: any) => async (dispatch: Dispatch) => {
|
2021-06-13 00:16:57 +02:00
|
|
|
try {
|
|
|
|
const res = await axios.put<ApiResponse<Config[]>>('/api/config', formData);
|
2021-10-06 14:15:05 +02:00
|
|
|
|
2021-06-13 00:16:57 +02:00
|
|
|
dispatch<CreateNotificationAction>({
|
|
|
|
type: ActionTypes.createNotification,
|
|
|
|
payload: {
|
|
|
|
title: 'Success',
|
2021-10-06 14:15:05 +02:00
|
|
|
message: 'Settings updated',
|
|
|
|
},
|
|
|
|
});
|
2021-06-13 00:16:57 +02:00
|
|
|
|
|
|
|
dispatch<UpdateConfigAction>({
|
|
|
|
type: ActionTypes.updateConfig,
|
2021-10-06 14:15:05 +02:00
|
|
|
payload: res.data.data,
|
|
|
|
});
|
2021-06-13 00:16:57 +02:00
|
|
|
} 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/config/0/queries'
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch<FetchQueriesAction>({
|
|
|
|
type: ActionTypes.fetchQueries,
|
|
|
|
payload: res.data.data,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
};
|