Merge branch 'feature/itemCategore'

This commit is contained in:
Ahmed Bouhuolia
2020-03-31 16:58:56 +02:00
12 changed files with 356 additions and 27 deletions

View File

@@ -0,0 +1,47 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitCategory = ({ form }) => {
return dispatch =>
new Promise((resolve, reject) => {
ApiService.post('item_categories', form)
.then(response => {
dispatch({ type: t.ITEMS_CATEGORY_LIST_SET });
resolve(response);
})
.catch(error => {
const { response } = error;
const { data } = response;
const { errors } = data;
dispatch({ type: t.ITEMS_CATEGORY_LIST_SET });
if (errors) {
dispatch({ type: t.ITEMS_CATEGORY_LIST_SET, errors });
}
reject(error);
});
});
};
export const submitItemCategory = ({ form }) => {
return dispatch => {
return ApiService.post('item_categories', { ...form });
};
};
export const fetchCategory = () => {
return (dispatch, getState) =>
new Promise((resolve, reject) => {
ApiService.get('item_categories')
.then(response => {
dispatch({
type: t.ITEMS_CATEGORY_DATA_TABLE,
data: response.data
});
resolve(response);
})
.catch(error => {
reject(error);
});
});
};

View File

@@ -0,0 +1,6 @@
export default {
ITEMS_CATEGORY_LIST_SET: 'ITEMS_CATEGORY_LIST_SET',
ITEMS_CATEGORY_DATA_TABLE: 'ITEMS_CATEGORY_DATA_TABLE',
};

View File

@@ -0,0 +1,27 @@
import t from 'store/types';
import { createReducer } from '@reduxjs/toolkit';
const initialState = {
categories: {},
categoriesById: {}
};
export default createReducer(initialState, {
[t.ITEMS_CATEGORY_LIST_SET]: (state, action) => {
const _categories = {};
action.categories.forEach(category => {
_categories[category.id] = category;
});
state.categories = {
...state.categories,
..._categories
};
},
[t.CATEGORY_SET]: (state, action) => {
state.categoriesById[action.category.id] = action.category;
}
});
export const getCategoryId = (state, id) => {
return state.categories.categoriesById[id];
};

View File

@@ -11,6 +11,7 @@ import expenses from './expenses/expenses.reducer';
import currencies from './currencies/currencies.reducer';
import resources from './resources/resources.reducer';
import financialStatements from './financialStatement/financialStatements.reducer';
import itemCategories from './itemCategories/itemsCateory.reducer';
export default combineReducers({
authentication,
@@ -24,4 +25,5 @@ export default combineReducers({
resources,
financialStatements,
items,
});
itemCategories
});

View File

@@ -10,7 +10,7 @@ import preferences from './preferences/preferences.types';
import resources from './resources/resource.types';
import users from './users/users.types';
import financialStatements from './financialStatement/financialStatements.types';
import itemCategories from './itemCategories/itemsCategory.type';
export default {
...authentication,
...accounts,
@@ -24,4 +24,5 @@ export default {
...resources,
...users,
...financialStatements,
}
...itemCategories
};