-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcategories.ts
More file actions
37 lines (31 loc) · 1.36 KB
/
categories.ts
File metadata and controls
37 lines (31 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { uniqBy } from 'lodash'
import { Item, PackItem } from "types/item";
import { Category, CategoryItems } from "types/category";
import { Option } from "app/components/FormFields/types";
export const getCategories = (items: (PackItem | Item)[]): Category[] => {
const categories = items.map(item => item.Category);
return uniqBy(categories, 'id').sort((a, b) => a.id - b.id);
};
export const getItemsByCategory = (items: (PackItem | Item)[]): CategoryItems[] => {
const categories = getCategories(items);
return categories.map(category => {
const catItems = items.filter(i => i.categoryId === category.id);
return {
...category,
items: catItems
}
});
};
// handles the weirdness between categories with IDs and those without Ids
export const categorySelectValue = (categories: Category[], value: number | string | undefined): Option<any> | undefined => {
if (!value) return;
const currentCategory = categories.find(cat => cat.id === value);
return {
value: currentCategory ? currentCategory.id : value,
label: currentCategory ? currentCategory.name : value.toString()
};
};
export const categoryCheckIfNew = (categories: Category[], value: number | string | undefined): boolean => {
if (!value) return false;
return categories.find(cat => cat.id === value) ? false : true;
};