Skip to content

Commit 09d927a

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 f62762b commit 09d927a

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
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: 14 additions & 3 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, categoryCheckIfNew } from "lib/utils/categories";
1515

1616
import UploadModal from 'app/Inventory/UploadModal';
1717
import { alertError, alertSuccess } from "../Notifications";
@@ -42,6 +42,7 @@ const ItemForm: React.FC<FormSpecs.Props> = ({ createItem, exportCsv, onSubmit }
4242
price: undefined,
4343
product_url: '',
4444
newCategory: false,
45+
excludeWeight: false,
4546
notes: ''
4647
}}
4748
validationSchema={Yup.object().shape({
@@ -70,6 +71,7 @@ const ItemForm: React.FC<FormSpecs.Props> = ({ createItem, exportCsv, onSubmit }
7071
const wasSubmitted = submitCount > 0;
7172
const weightUnit = values.weight_unit;
7273
const categoryValue = categorySelectValue(app.categories, values.categoryId);
74+
let isNewCategory = categoryCheckIfNew(app.categories, values.categoryId);
7375

7476
return (
7577
<SidebarContainer>
@@ -88,14 +90,23 @@ const ItemForm: React.FC<FormSpecs.Props> = ({ createItem, exportCsv, onSubmit }
8890
value={categoryValue || null}
8991
onChange={(option: Option<number>) => {
9092
const value = option ? option.value : undefined;
91-
const isNewCategory = Boolean(option && option.__isNew__);
93+
isNewCategory = Boolean(option && option.__isNew__);
9294
setFieldValue('categoryId', value);
9395
setFieldValue('newCategory', isNewCategory);
9496
}}
9597
error={wasSubmitted && !!errors.categoryId}
9698
errorMsg={errors.categoryId}
9799
clearable={true}
98100
/>
101+
{isNewCategory &&
102+
<SwitchInput
103+
label="Exclude category from base weight"
104+
checked = {values.excludeWeight}
105+
checkedText="Yes"
106+
uncheckedText="No"
107+
tip="All items added to this category will be excluded from base weight"
108+
onChange={v => setFieldValue('excludeWeight', v)}/>
109+
}
99110
<Input
100111
label="Product Name"
101112
value={values.product_name || ''}

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 categoryCheckIfNew = (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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface Item extends BaseItem {
2222
export type CreateItem = Omit<BaseItem, 'categoryId'> & {
2323
categoryId?: number;
2424
newCategory: boolean;
25+
excludeWeight: boolean;
2526
}
2627

2728
export type UpdateItem = Omit<BaseItem, 'categoryId'> & {

0 commit comments

Comments
 (0)