1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-08-07 11:55:17 +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

@ -19,7 +19,10 @@ import {
UpdateBookmarkAction,
// Notifications
CreateNotificationAction,
ClearNotificationAction
ClearNotificationAction,
// Config
GetConfigAction,
UpdateConfigAction
} from './';
export enum ActionTypes {
@ -48,7 +51,10 @@ export enum ActionTypes {
updateBookmark = 'UPDATE_BOOKMARK',
// Notifications
createNotification = 'CREATE_NOTIFICATION',
clearNotification = 'CLEAR_NOTIFICATION'
clearNotification = 'CLEAR_NOTIFICATION',
// Config
getConfig = 'GET_CONFIG',
updateConfig = 'UPDATE_CONFIG'
}
export type Action =
@ -72,4 +78,7 @@ export type Action =
UpdateBookmarkAction |
// Notifications
CreateNotificationAction |
ClearNotificationAction;
ClearNotificationAction |
// Config
GetConfigAction |
UpdateConfigAction;

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);
}
}

View file

@ -2,4 +2,5 @@ export * from './theme';
export * from './app';
export * from './actionTypes';
export * from './bookmark';
export * from './notification';
export * from './notification';
export * from './config';

View file

@ -0,0 +1,36 @@
import { ActionTypes, Action } from '../actions';
import { Config } from '../../interfaces';
export interface State {
loading: boolean;
config: Config[];
}
const initialState: State = {
loading: true,
config: []
}
const getConfig = (state: State, action: Action): State => {
return {
loading: false,
config: action.payload
}
}
const updateConfig = (state: State, action: Action): State => {
return {
...state,
config: action.payload
}
}
const configReducer = (state: State = initialState, action: Action) => {
switch(action.type) {
case ActionTypes.getConfig: return getConfig(state, action);
case ActionTypes.updateConfig: return updateConfig(state, action);
default: return state;
}
}
export default configReducer;

View file

@ -6,12 +6,14 @@ import themeReducer from './theme';
import appReducer from './app';
import bookmarkReducer from './bookmark';
import notificationReducer from './notification';
import configReducer from './config';
const rootReducer = combineReducers<GlobalState>({
theme: themeReducer,
app: appReducer,
bookmark: bookmarkReducer,
notification: notificationReducer
notification: notificationReducer,
config: configReducer
})
export default rootReducer;

View file

@ -4,6 +4,4 @@ import thunk from 'redux-thunk';
import rootReducer from './reducers';
const initialState = {};
const store = createStore(rootReducer, initialState, composeWithDevTools(applyMiddleware(thunk)));
export default store;
export const store = createStore(rootReducer, initialState, composeWithDevTools(applyMiddleware(thunk)));