2021-06-13 00:16:57 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
import { Dispatch } from 'redux';
|
|
|
|
import { ActionTypes } from './actionTypes';
|
2021-06-13 23:21:35 +02:00
|
|
|
import { Config, ApiResponse } 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-06-13 23:21:35 +02:00
|
|
|
|
2021-06-13 00:16:57 +02:00
|
|
|
dispatch<GetConfigAction>({
|
|
|
|
type: ActionTypes.getConfig,
|
|
|
|
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) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
dispatch<CreateNotificationAction>({
|
|
|
|
type: ActionTypes.createNotification,
|
|
|
|
payload: {
|
|
|
|
title: 'Success',
|
|
|
|
message: 'Settings updated'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
dispatch<UpdateConfigAction>({
|
|
|
|
type: ActionTypes.updateConfig,
|
|
|
|
payload: res.data.data
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|