WIP item categories.

This commit is contained in:
elforjani3
2020-04-04 19:32:05 +02:00
parent cf5f56ae32
commit cff8945889
15 changed files with 697 additions and 111 deletions

View File

@@ -1,42 +1,59 @@
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 = () => {
export const fetchItemCategories = () => {
return (dispatch, getState) =>
new Promise((resolve, reject) => {
ApiService.get('item_categories')
.then(response => {
dispatch({
type: t.ITEMS_CATEGORY_DATA_TABLE,
data: response.data
type: t.ITEMS_CATEGORY_LIST_SET,
categories: response.data.categories
});
resolve(response);
})
.catch(error => {
reject(error);
});
});
};
export const editItemCategory = (id, form) => {
return dispatch =>
new Promise((resolve, reject) => {
ApiService.post(`item_categories/${id}`, form)
.then(response => {
dispatch({ type: t.CLEAR_CATEGORY_FORM_ERRORS });
resolve(response);
})
.catch(error => {
const { response } = error;
const { data } = response;
const { errors } = data;
dispatch({ type: t.CLEAR_CATEGORY_FORM_ERRORS });
if (errors) {
dispatch({ type: t.CATEGORY_FORM_ERRORS, errors });
}
reject(error);
});
});
};
export const deleteItemCategory = id => {
return dispatch =>
new Promise((resolve, reject) => {
ApiService.delete(`item_categories/${id}`)
.then(response => {
dispatch({
type: t.CATEGORY_DELETE,
id
});
resolve(response);
})

View File

@@ -0,0 +1,30 @@
import t from 'store/types';
import { createReducer } from '@reduxjs/toolkit';
const initialState = {
categories: {}
};
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_DELETE]: (state, action) => {
if (typeof state.categories[action.id] !== 'undefined') {
delete state.categories[action.id];
}
}
});
export const getCategoryId = (state, id) => {
return state.itemCategories.categories[id] || {};
};

View File

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

View File

@@ -2,8 +2,7 @@ import t from 'store/types';
import { createReducer } from '@reduxjs/toolkit';
const initialState = {
categories: {},
categoriesById: {}
categories: {}
};
export default createReducer(initialState, {
@@ -18,10 +17,14 @@ export default createReducer(initialState, {
..._categories
};
},
[t.CATEGORY_SET]: (state, action) => {
state.categoriesById[action.category.id] = action.category;
[t.CATEGORY_DELETE]: (state, action) => {
if (typeof state.categories[action.id] !== 'undefined') {
delete state.categories[action.id];
}
}
});
export const getCategoryId = (state, id) => {
return state.categories.categoriesById[id];
return state.itemCategories.categories[id] || {};
};

View File

@@ -11,7 +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';
import itemCategories from './itemCategories/itemsCategory.reducer';
export default combineReducers({
authentication,