1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-08-06 11:25:16 +02:00

Created config global state. Reworked WeatherSettings and WeatherWidget to use new config state.

This commit is contained in:
unknown 2021-06-13 00:16:57 +02:00
parent a5504e6e80
commit d257fbf9a3
15 changed files with 214 additions and 88 deletions

View file

@ -0,0 +1,48 @@
import axios from 'axios';
import { Dispatch } from 'redux';
import { ActionTypes } from './actionTypes';
import { Config, ApiResponse, WeatherForm } from '../../interfaces';
import { CreateNotificationAction } from './notification';
export interface GetConfigAction {
type: ActionTypes.getConfig;
payload: Config[];
}
export const getConfig = () => async (dispatch: Dispatch) => {
try {
const res = await axios.get<ApiResponse<Config[]>>('/api/config');
dispatch<GetConfigAction>({
type: ActionTypes.getConfig,
payload: res.data.data
})
} catch (err) {
console.log(err)
}
}
export interface UpdateConfigAction {
type: ActionTypes.updateConfig;
payload: Config[];
}
export const updateConfig = (formData: WeatherForm) => async (dispatch: Dispatch) => {
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);
}
}