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

75 lines
1.8 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/config/0/queries'
);
dispatch<FetchQueriesAction>({
type: ActionTypes.fetchQueries,
payload: res.data.data,
});
} catch (err) {
console.log(err);
}
};