Skip to content

Commit b981a97

Browse files
committed
Add switch to exclude new category weight
The weight exclusion of custom categories is not grouped with "Worn" or "Consumable" in the view's "Weight Breakdown" but added to a "Other excluded" count which is only shown when pack has items in custom excluded category.
1 parent 36ce740 commit b981a97

File tree

6 files changed

+44
-16
lines changed

6 files changed

+44
-16
lines changed

api/controllers/item.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ router.get('', async (req, res) => {
8989
});
9090

9191
// create or retrieve category
92-
async function fetchCategory(categoryId) {
92+
async function fetchCategory(categoryId, excludeWeight) {
9393
let newCat = null;
94-
const category = { name: categoryId, level: 'USER' };
94+
const category = { name: categoryId, level: 'USER', exclude_weight: excludeWeight };
9595
try {
9696
newCat = await models.Category.create(category);
9797
} catch (err) {
@@ -106,12 +106,12 @@ async function fetchCategory(categoryId) {
106106

107107
// Create
108108
router.post('', authenticate, async (req, res) => {
109-
const { newCategory } = req.body;
109+
const { newCategory, excludeWeight } = req.body;
110110
const { payload } = itemPayload(req.body);
111111

112112
let newCat = null;
113113
if (newCategory) {
114-
const { category, err } = await fetchCategory(payload.categoryId);
114+
const { category, err } = await fetchCategory(payload.categoryId, excludeWeight);
115115
if (err) {
116116
return res.status(400).json(err);
117117
}
@@ -132,7 +132,8 @@ router.put('', authenticate, async (req, res) => {
132132

133133
let newCat = null;
134134
if (newCategory) {
135-
const { category, err } = await fetchCategory(payload.categoryId);
135+
// hardcoding false here while I figure out how to do update
136+
const { category, err } = await fetchCategory(payload.categoryId, false);
136137
if (err) {
137138
return res.status(400).json(err);
138139
}
@@ -162,4 +163,4 @@ router.post('/delete', authenticate, (req, res) => {
162163
});
163164

164165

165-
module.exports = router;
166+
module.exports = router;

frontend/src/app/components/CategoryTable/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ const CategoryChart: React.FC<CategoryTableProps> = ({ data, unit }) => {
2525
const totalWeight = data.reduce((acc: number, cur: CategoryItemSpecs) => acc + cur.total.value, 0);
2626
const excludedWeight = data.reduce((acc: number, cur: CategoryItemSpecs) => acc + cur.excluded.value, 0);
2727
const consumables = data.find(c => c.name === 'Consumables');
28+
const customExcludedCategories = data.find(c => c.level === 'USER' && c.exclude_weight === true);
29+
const customExcludedWeight = customExcludedCategories ? customExcludedCategories.total.value : 0;
2830
const consumablesWeight = consumables ? consumables.total.value : 0;
29-
const wornWeight = excludedWeight - consumablesWeight;
31+
const wornWeight = excludedWeight - consumablesWeight - customExcludedWeight;
3032
const baseWeight = totalWeight - excludedWeight;
3133

3234
return (
@@ -47,6 +49,12 @@ const CategoryChart: React.FC<CategoryTableProps> = ({ data, unit }) => {
4749
<div>Worn</div>
4850
<div>{wornWeight.toFixed(2)} {unit}</div>
4951
</CatRow>
52+
{customExcludedCategories &&
53+
<CatRow className="totals">
54+
<div>Other Excluded</div>
55+
<div>{customExcludedWeight.toFixed(2)} {unit}</div>
56+
</CatRow>
57+
}
5058
<CatRow className="totals highlight">
5159
<div>Base Weight</div>
5260
<div>{baseWeight.toFixed(2)} {unit}</div>
@@ -55,4 +63,4 @@ const CategoryChart: React.FC<CategoryTableProps> = ({ data, unit }) => {
5563
)
5664
};
5765

58-
export default React.memo(CategoryChart);
66+
export default React.memo(CategoryChart);

frontend/src/app/components/FormFields/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export { default as Input } from './Input';
22
export { default as Textarea } from './Textarea';
33
export { default as Select } from './Select'
44
export { default as SelectCreatable } from './SelectCreatable'
5+
export { default as SwitchInput } from './SwitchInput'
56
export { Option } from './types';
6-
export { Label } from './styles';
7+
export { Label } from './styles';

frontend/src/app/components/ItemForm/ItemForm.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import * as Yup from 'yup';
33
import FileDownload from 'js-file-download';
44
import { Formik, FormikProps } from "formik";
55
import { Row, Col, Button } from "antd";
6-
import { Input, Select, SelectCreatable, Option } from '../FormFields';
6+
import { Input, Select, SelectCreatable, Option, SwitchInput } from "../FormFields";
77

88
import { AppContext } from 'AppContext';
99
import { CreateItem, ItemConstants } from "types/item";
1010
import { FormSpecs } from "./types";
1111

1212
import withApi from 'app/components/higher-order/with-api';
1313
import { categoryOptions, weightUnitOptions } from "lib/utils/form";
14-
import { categorySelectValue } from "lib/utils/categories";
14+
import { categorySelectValue, categorySelectIfNew } from "lib/utils/categories";
1515

1616
import UploadModal from 'app/Inventory/UploadModal';
1717
import { alertError, alertSuccess } from "../Notifications";
@@ -41,7 +41,8 @@ const ItemForm: React.FC<FormSpecs.Props> = ({ createItem, exportCsv, onSubmit }
4141
weight_unit: default_weight_unit,
4242
price: undefined,
4343
product_url: '',
44-
newCategory: false
44+
newCategory: false,
45+
excludeWeight: false
4546
}}
4647
validationSchema={Yup.object().shape({
4748
name: Yup.string().required('Item name is required'),
@@ -69,6 +70,7 @@ const ItemForm: React.FC<FormSpecs.Props> = ({ createItem, exportCsv, onSubmit }
6970
const wasSubmitted = submitCount > 0;
7071
const weightUnit = values.weight_unit;
7172
const categoryValue = categorySelectValue(app.categories, values.categoryId);
73+
let isNewCategory = categorySelectIfNew(app.categories, values.categoryId);
7274

7375
return (
7476
<SidebarContainer>
@@ -87,14 +89,24 @@ const ItemForm: React.FC<FormSpecs.Props> = ({ createItem, exportCsv, onSubmit }
8789
value={categoryValue || null}
8890
onChange={(option: Option<number>) => {
8991
const value = option ? option.value : undefined;
90-
const isNewCategory = Boolean(option && option.__isNew__);
92+
isNewCategory = Boolean(option && option.__isNew__);
9193
setFieldValue('categoryId', value);
9294
setFieldValue('newCategory', isNewCategory);
9395
}}
9496
error={wasSubmitted && !!errors.categoryId}
9597
errorMsg={errors.categoryId}
9698
clearable={true}
9799
/>
100+
{isNewCategory &&
101+
<SwitchInput
102+
label="Exclude category from base weight"
103+
checked = {values.excludeWeight}
104+
checkedText="Yes"
105+
uncheckedText="No"
106+
tip="All items added to this category will be exluded from base weight"
107+
onChange={v => setFieldValue('excludeWeight', v)}
108+
></SwitchInput>
109+
}
98110
<Input
99111
label="Product Name"
100112
value={values.product_name || ''}
@@ -172,4 +184,4 @@ const ItemFormWithProps = withApi<FormSpecs.ApiProps>(api => ({
172184
exportCsv: api.ItemService.exportCSV
173185
}))(ItemForm);
174186

175-
export default ItemFormWithProps;
187+
export default ItemFormWithProps;

frontend/src/lib/utils/categories.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ export const categorySelectValue = (categories: Category[], value: number | stri
2929
value: currentCategory ? currentCategory.id : value,
3030
label: currentCategory ? currentCategory.name : value.toString()
3131
};
32-
};
32+
};
33+
34+
export const categorySelectIfNew = (categories: Category[], value: number | string | undefined): boolean => {
35+
if (!value) return false;
36+
return categories.find(cat => cat.id === value) ? false : true;
37+
};

frontend/src/types/item.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface Item extends BaseItem {
2121
export type CreateItem = Omit<BaseItem, 'categoryId'> & {
2222
categoryId?: number;
2323
newCategory: boolean;
24+
excludeWeight: boolean;
2425
}
2526

2627
export type UpdateItem = Omit<BaseItem, 'categoryId'> & {
@@ -57,4 +58,4 @@ export enum ItemConstants {
5758
name = 200,
5859
product_name = 500,
5960
product_url = 500
60-
}
61+
}

0 commit comments

Comments
 (0)