Skip to content

Commit

Permalink
fix: weight unit convert
Browse files Browse the repository at this point in the history
  • Loading branch information
taronaleksanian committed Sep 29, 2024
1 parent 8f9d1cf commit cebc8e1
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 19 deletions.
8 changes: 2 additions & 6 deletions packages/app/modules/item/components/AddItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ export const AddItem = ({
const { editPackItem } = useEditPackItem(isItemPage);

const handleSubmit = (data: Item) => {
const convertedData = {
...data,
weight: convertWeighToSmallestUnit(data.unit as ItemUnit, data.weight),
};
if (isEdit) {
editPackItem(convertedData as any);
editPackItem(data as any);
} else {
addPackItem(convertedData);
addPackItem(data);
}
if (closeModalHandler) closeModalHandler();
};
Expand Down
4 changes: 2 additions & 2 deletions packages/app/modules/item/components/ImportForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const ImportForm: FC<ImportFormProps> = ({
}}
>
<CascadedDropdownComponent
value={selectedType}
value={selectedType.value}
data={[
...(currentpage !== 'items'
? csvOption
Expand All @@ -166,4 +166,4 @@ export const ImportForm: FC<ImportFormProps> = ({
</RButton>
</View>
);
};
};
9 changes: 8 additions & 1 deletion packages/app/modules/item/screens/ItemDetailsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import useResponsive from 'app/hooks/useResponsive';
import { CustomCard } from 'app/components/card';
import LargeCard from 'app/components/card/LargeCard';
import { FeedPreview } from 'app/modules/feed';
import { convertWeight } from 'app/utils/convertWeight';
import { SMALLEST_ITEM_UNIT } from '../constants';

export function ItemDetailsScreen() {
const { limit, handleLimitChange, page, handlePageChange } = usePagination();
Expand Down Expand Up @@ -36,7 +38,12 @@ export function ItemDetailsScreen() {
<RStack>
<RText>Category: {item?.category?.name || '-'}</RText>
<RText>
Weight: {item?.weight}
Weight:{' '}
{convertWeight(
Number(item?.weight),
SMALLEST_ITEM_UNIT,
item?.unit as any,
)}
{item?.unit}
</RText>
<RText>Quantity: {item?.quantity}</RText>
Expand Down
8 changes: 5 additions & 3 deletions packages/app/modules/item/screens/ItemsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ export function ItemsScreen() {
<BaseModal title="Add a global Item" trigger="Add Item">
<AddItemGlobal />
</BaseModal>
{ role === 'admin' && <BaseModal title="Import global Item" trigger="Import Item">
<ImportItemGlobal />
</BaseModal>}
{role === 'admin' && (
<BaseModal title="Import global Item" trigger="Import Item">
<ImportItemGlobal />
</BaseModal>
)}
</View>
</RStack>
{!isError && data?.items && Array.isArray(data.items) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
getPaginationRowModel,
} from '@tanstack/react-table';
import { useMedia } from 'tamagui';
import { convertWeight } from 'app/utils/convertWeight';
import { Text, View, Button, getTokenValue } from 'tamagui';
import { Table } from './common/tableParts';
import { Pressable } from 'react-native';
import { useRouter } from '@packrat/crosspath';
import { SMALLEST_ITEM_UNIT } from 'app/modules/item/constants';

const ITEMS_PER_PAGE = 10;

Expand Down Expand Up @@ -66,7 +68,12 @@ export function PaginatedSortedTable({
footer: (info) => info.column.id,
}),
columnHelper.accessor('weight', {
cell: (info) => info.getValue(),
cell: (info) =>
convertWeight(
info.getValue(),
SMALLEST_ITEM_UNIT,
info.row.original.unit as any,
),
header: () => 'Weight',
footer: (info) => info.column.id,
}),
Expand Down
3 changes: 2 additions & 1 deletion server/src/services/item/addItemGlobalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Item } from '../../drizzle/methods/Item';
import { ItemCategory } from '../../drizzle/methods/itemcategory';
import { ItemCategory as categories } from '../../utils/itemCategory';
import { VectorClient } from '../../vector/client';
import { convertWeight, SMALLEST_WEIGHT_UNIT } from 'src/utils/convertWeight';
// import { prisma } from '../../prisma';

/**
Expand Down Expand Up @@ -37,7 +38,7 @@ export const addItemGlobalService = async (
}
const newItem = await itemClass.create({
name,
weight,
weight: convertWeight(Number(weight), unit as any, SMALLEST_WEIGHT_UNIT),
quantity,
unit,
categoryId: category.id,
Expand Down
3 changes: 2 additions & 1 deletion server/src/services/item/addItemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ItemOwners } from '../../drizzle/methods/ItemOwners';
import { ItemCategory as categories } from '../../utils/itemCategory';
import { type InsertItemCategory } from '../../db/schema';
import { VectorClient } from '../../vector/client';
import { convertWeight, SMALLEST_WEIGHT_UNIT } from 'src/utils/convertWeight';
import { type ExecutionContext } from 'hono';

/**
Expand Down Expand Up @@ -42,7 +43,7 @@ export const addItemService = async (
const item = await itemClass.create(
{
name,
weight,
weight: convertWeight(Number(weight), unit as any, SMALLEST_WEIGHT_UNIT),
quantity,
unit,
// packs: [packId],
Expand Down
3 changes: 2 additions & 1 deletion server/src/services/item/bulkAddGlobalItemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type InsertItemCategory } from '../../db/schema';
import { ItemCategory } from '../../drizzle/methods/itemcategory';
import { DbClient } from 'src/db/client';
import { item as ItemTable } from '../../db/schema';
import { convertWeight, SMALLEST_WEIGHT_UNIT } from 'src/utils/convertWeight';

export const bulkAddItemsGlobalService = async (
items: Array<{
Expand Down Expand Up @@ -35,7 +36,7 @@ export const bulkAddItemsGlobalService = async (

const newItem = {
name,
weight,
weight: convertWeight(Number(weight), unit as any, SMALLEST_WEIGHT_UNIT),
quantity,
unit,
categoryId: category.id,
Expand Down
11 changes: 8 additions & 3 deletions server/src/services/item/editItemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Item } from '../../drizzle/methods/Item';
import { ItemCategory } from '../../drizzle/methods/itemcategory';
import { ItemCategory as categories } from '../../utils/itemCategory';
import { VectorClient } from '../../vector/client';
import { convertWeight, SMALLEST_WEIGHT_UNIT } from 'src/utils/convertWeight';

/**
* Edit an item in the service.
Expand Down Expand Up @@ -55,12 +56,16 @@ export const editItemService = async (
});
}
}

const itemUnit = unit || item.unit;
// const item = await itemClass.findItem({ id });
const newItem = await itemClass.update(id, {
name: name || item.name,
weight: weight || item.weight,
unit: unit || item.unit,
weight: convertWeight(
Number(weight || item.weight),
itemUnit as any,
SMALLEST_WEIGHT_UNIT,
),
unit: itemUnit,
quantity: quantity || item.quantity,
categoryId: category.id || item.categoryId,
});
Expand Down
1 change: 1 addition & 0 deletions server/src/utils/convertWeight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type WeightUnit = 'g' | 'kg' | 'oz' | 'lb' | 'lbs';
export const SMALLEST_WEIGHT_UNIT = 'g';

const units: Record<WeightUnit, number> = {
g: 1,
Expand Down

0 comments on commit cebc8e1

Please sign in to comment.