mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-09 04:25:18 +02:00
Apps actions and reducer
This commit is contained in:
parent
2acc3b72ec
commit
78acede8ab
15 changed files with 230 additions and 27 deletions
|
@ -1 +1,5 @@
|
|||
export const SET_THEME = 'SET_THEME';
|
||||
|
||||
export const GET_APPS = 'GET_APPS';
|
||||
export const GET_APPS_SUCCESS = 'GET_APPS_SUCCESS';
|
||||
export const GET_APPS_ERROR = 'GET_APPS_ERROR';
|
25
client/src/store/actions/app.ts
Normal file
25
client/src/store/actions/app.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import axios from 'axios';
|
||||
import { Dispatch } from 'redux';
|
||||
import {
|
||||
GET_APPS,
|
||||
GET_APPS_SUCCESS,
|
||||
GET_APPS_ERROR
|
||||
} from './actionTypes';
|
||||
|
||||
export const getApps = () => async (dispatch: Dispatch) => {
|
||||
dispatch({ type: GET_APPS });
|
||||
|
||||
try {
|
||||
const res = await axios.get('/api/apps');
|
||||
|
||||
dispatch({
|
||||
type: GET_APPS_SUCCESS,
|
||||
payload: res.data.data
|
||||
})
|
||||
} catch (err) {
|
||||
dispatch({
|
||||
type: GET_APPS_ERROR,
|
||||
payload: err.data.data
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
export * from './theme';
|
||||
export * from './app';
|
||||
export * from './actionTypes';
|
42
client/src/store/reducers/app.ts
Normal file
42
client/src/store/reducers/app.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
import {
|
||||
GET_APPS,
|
||||
GET_APPS_SUCCESS,
|
||||
GET_APPS_ERROR
|
||||
} from '../actions/actionTypes';
|
||||
|
||||
import { App } from '../../interfaces/App';
|
||||
|
||||
interface State {
|
||||
loading: boolean;
|
||||
apps: App[];
|
||||
}
|
||||
|
||||
const initialState: State = {
|
||||
loading: true,
|
||||
apps: []
|
||||
}
|
||||
|
||||
const getApps = (state: State, action: any): State => {
|
||||
return {
|
||||
...state,
|
||||
loading: true
|
||||
}
|
||||
}
|
||||
|
||||
const getAppsSuccess = (state: State, action: any): State => {
|
||||
return {
|
||||
...state,
|
||||
loading: false,
|
||||
apps: action.payload
|
||||
}
|
||||
}
|
||||
|
||||
const appReducer = (state = initialState, action: any) => {
|
||||
switch (action.type) {
|
||||
case GET_APPS: return getApps(state, action);
|
||||
case GET_APPS_SUCCESS: return getAppsSuccess(state, action);
|
||||
default: return state;
|
||||
}
|
||||
}
|
||||
|
||||
export default appReducer;
|
|
@ -1,9 +1,11 @@
|
|||
import { combineReducers } from 'redux';
|
||||
|
||||
import themeReducer from './theme';
|
||||
import appReducer from './app';
|
||||
|
||||
const rootReducer = combineReducers({
|
||||
theme: themeReducer
|
||||
theme: themeReducer,
|
||||
app: appReducer
|
||||
})
|
||||
|
||||
export default rootReducer;
|
Loading…
Add table
Add a link
Reference in a new issue