1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-25 13:59:36 +02:00

Rewritten some parts of redux store to use typescript interfaces

This commit is contained in:
unknown 2021-05-11 16:44:05 +02:00
parent 78acede8ab
commit d3c5e2a5ec
10 changed files with 89 additions and 45 deletions

View file

@ -6,7 +6,7 @@ import { connect } from 'react-redux';
import { getApps } from '../../store/actions'; import { getApps } from '../../store/actions';
// Typescript // Typescript
import { App } from '../../interfaces'; import { App, GlobalState } from '../../interfaces';
// CSS // CSS
import classes from './Apps.module.css'; import classes from './Apps.module.css';
@ -49,7 +49,7 @@ const Apps = (props: ComponentProps): JSX.Element => {
) )
} }
const mapStateToProps = (state: any) => { const mapStateToProps = (state: GlobalState) => {
return { return {
apps: state.app.apps, apps: state.app.apps,
loading: state.app.loading loading: state.app.loading

View file

@ -5,4 +5,9 @@ export interface App {
icon: string; icon: string;
createdAt: Date; createdAt: Date;
updatedAt: Date; updatedAt: Date;
}
export interface AppResponse {
success: boolean;
data: App[]
} }

View file

@ -0,0 +1,7 @@
import { State as AppState } from '../store/reducers/app';
import { State as ThemeState } from '../store/reducers/theme';
export interface GlobalState {
theme: ThemeState;
app: AppState;
}

View file

@ -1,2 +1,3 @@
export * from './App'; export * from './App';
export * from './Theme'; export * from './Theme';
export * from './GlobalState';

View file

@ -1,5 +1,13 @@
export const SET_THEME = 'SET_THEME'; import {
GetAppsAction,
SetTheme
} from './';
export const GET_APPS = 'GET_APPS'; export enum ActionTypes {
export const GET_APPS_SUCCESS = 'GET_APPS_SUCCESS'; setTheme,
export const GET_APPS_ERROR = 'GET_APPS_ERROR'; getApps,
getAppsSuccess,
getAppsError
}
export type Action = GetAppsAction<any> | SetTheme;

View file

@ -1,24 +1,29 @@
import axios from 'axios'; import axios from 'axios';
import { Dispatch } from 'redux'; import { Dispatch } from 'redux';
import { import { ActionTypes } from './actionTypes';
GET_APPS, import { App, AppResponse } from '../../interfaces/App';
GET_APPS_SUCCESS,
GET_APPS_ERROR export interface GetAppsAction<T> {
} from './actionTypes'; type: ActionTypes.getApps | ActionTypes.getAppsSuccess | ActionTypes.getAppsError,
payload: T
}
export const getApps = () => async (dispatch: Dispatch) => { export const getApps = () => async (dispatch: Dispatch) => {
dispatch({ type: GET_APPS }); dispatch<GetAppsAction<undefined>>({
type: ActionTypes.getApps,
payload: undefined
});
try { try {
const res = await axios.get('/api/apps'); const res = await axios.get<AppResponse>('/api/apps');
dispatch({ dispatch<GetAppsAction<App[]>>({
type: GET_APPS_SUCCESS, type: ActionTypes.getAppsSuccess,
payload: res.data.data payload: res.data.data
}) })
} catch (err) { } catch (err) {
dispatch({ dispatch<GetAppsAction<string>>({
type: GET_APPS_ERROR, type: ActionTypes.getAppsError,
payload: err.data.data payload: err.data.data
}) })
} }

View file

@ -1,7 +1,12 @@
import { Dispatch } from 'redux'; import { Dispatch } from 'redux';
import { themes } from '../../components/Themer/themes.json'; import { themes } from '../../components/Themer/themes.json';
import { Theme } from '../../interfaces/Theme'; import { Theme } from '../../interfaces/Theme';
import { SET_THEME } from './actionTypes'; import { ActionTypes } from './actionTypes';
export interface SetTheme {
type: ActionTypes.setTheme,
payload: string
}
export const setTheme = (themeName: string) => (dispatch: Dispatch) => { export const setTheme = (themeName: string) => (dispatch: Dispatch) => {
const theme = themes.find((theme: Theme) => theme.name === themeName); const theme = themes.find((theme: Theme) => theme.name === themeName);
@ -10,8 +15,8 @@ export const setTheme = (themeName: string) => (dispatch: Dispatch) => {
localStorage.setItem('theme', themeName); localStorage.setItem('theme', themeName);
loadTheme(theme); loadTheme(theme);
dispatch({ dispatch<SetTheme>({
type: SET_THEME, type: ActionTypes.setTheme,
payload: themeName payload: themeName
}) })
} }

View file

@ -1,29 +1,27 @@
import { import { ActionTypes, Action } from '../actions';
GET_APPS,
GET_APPS_SUCCESS,
GET_APPS_ERROR
} from '../actions/actionTypes';
import { App } from '../../interfaces/App'; import { App } from '../../interfaces/App';
interface State { export interface State {
loading: boolean; loading: boolean;
apps: App[]; apps: App[];
errors: '' | undefined;
} }
const initialState: State = { const initialState: State = {
loading: true, loading: true,
apps: [] apps: [],
errors: undefined
} }
const getApps = (state: State, action: any): State => { const getApps = (state: State, action: Action): State => {
return { return {
...state, ...state,
loading: true loading: true,
errors: undefined
} }
} }
const getAppsSuccess = (state: State, action: any): State => { const getAppsSuccess = (state: State, action: Action): State => {
return { return {
...state, ...state,
loading: false, loading: false,
@ -31,10 +29,19 @@ const getAppsSuccess = (state: State, action: any): State => {
} }
} }
const appReducer = (state = initialState, action: any) => { const getAppsError = (state: State, action: Action): State => {
return {
...state,
loading: false,
errors: action.payload
}
}
const appReducer = (state = initialState, action: Action) => {
switch (action.type) { switch (action.type) {
case GET_APPS: return getApps(state, action); case ActionTypes.getApps: return getApps(state, action);
case GET_APPS_SUCCESS: return getAppsSuccess(state, action); case ActionTypes.getAppsSuccess: return getAppsSuccess(state, action);
case ActionTypes.getAppsError: return getAppsError(state, action);
default: return state; default: return state;
} }
} }

View file

@ -1,9 +1,11 @@
import { combineReducers } from 'redux'; import { combineReducers } from 'redux';
import { GlobalState } from '../../interfaces/GlobalState';
import themeReducer from './theme'; import themeReducer from './theme';
import appReducer from './app'; import appReducer from './app';
const rootReducer = combineReducers({ const rootReducer = combineReducers<GlobalState>({
theme: themeReducer, theme: themeReducer,
app: appReducer app: appReducer
}) })

View file

@ -1,17 +1,21 @@
import { SET_THEME } from '../actions/actionTypes'; import { ActionTypes, Action } from '../actions';
const initialState = { export interface State {
theme: string;
}
const initialState: State = {
theme: 'blues' theme: 'blues'
} }
const themeReducer = (state = initialState, action: any) => { const setTheme = (state: State, action: Action): State => {
return { theme: action.payload };
}
const themeReducer = (state = initialState, action: Action) => {
switch (action.type) { switch (action.type) {
case SET_THEME: case ActionTypes.setTheme: return setTheme(state, action);
return { default: return state;
theme: action.payload
};
default:
return state;
} }
} }