diff --git a/packages/app/components/feed/FeedCard.tsx b/packages/app/components/feed/FeedCard.tsx index d7f02b17b..356c48b2f 100644 --- a/packages/app/components/feed/FeedCard.tsx +++ b/packages/app/components/feed/FeedCard.tsx @@ -11,6 +11,8 @@ import { formatNumber } from 'app/utils/formatNumber'; import { useAddFavorite, useFetchUserFavorites } from 'app/hooks/favorites'; import { useAuthUser } from 'app/auth/hooks'; import { useRouter } from 'app/hooks/router'; +import { useItemWeightUnit } from 'app/hooks/items'; +import { convertWeight } from 'app/utils/convertWeight'; interface CardProps { type: string; @@ -57,6 +59,7 @@ export default function Card({ useTheme(); const { addFavorite } = useAddFavorite(); + const [weightUnit] = useItemWeightUnit(); const { data: favorites = [] } = useFetchUserFavorites(user?.id); @@ -97,8 +100,7 @@ export default function Card({ const truncatedName = truncateString(name, 25); const truncatedDestination = truncateString(destination, 25); - // const formattedWeight = formatNumber(total_weight); // TODO convert to user preference once implemented - const formattedWeight = total_weight; + const formattedWeight = convertWeight(total_weight, 'g', weightUnit); let numberOfNights; if (duration) numberOfNights = JSON.parse(duration).numberOfNights; @@ -184,7 +186,7 @@ export default function Card({ {type === 'pack' && ( - Total Weight: {formattedWeight} + Total Weight: {formatNumber(formattedWeight)} {weightUnit} )} diff --git a/packages/app/hooks/items/index.ts b/packages/app/hooks/items/index.ts index 45e4966a6..e36816bb6 100644 --- a/packages/app/hooks/items/index.ts +++ b/packages/app/hooks/items/index.ts @@ -2,3 +2,4 @@ export { useAddItem } from './useAddItem'; export { useItems } from './useItems'; export { useDeleteItem } from './useDeleteItem'; export { useItemsUpdater } from './useItemsUpdater'; +export { useItemWeightUnit } from './useItemWeightUnit'; diff --git a/packages/app/hooks/items/useItemWeightUnit.ts b/packages/app/hooks/items/useItemWeightUnit.ts new file mode 100644 index 000000000..52adfecd3 --- /dev/null +++ b/packages/app/hooks/items/useItemWeightUnit.ts @@ -0,0 +1,17 @@ +import { useState } from 'react'; +import { useAuthUser } from 'app/auth/hooks'; + +type WeightUnit = 'g' | 'kg' | 'oz' | 'lb' | 'lbs'; + +export const useItemWeightUnit = (): [ + WeightUnit, + React.Dispatch>, +] => { + const authUser = useAuthUser(); + const userPreferWeight = authUser.preferredWeight as WeightUnit | null; + const [weightUnit, setWeightUnit] = useState( + userPreferWeight || 'kg', + ); + + return [weightUnit, setWeightUnit]; +}; diff --git a/packages/app/hooks/packs/usePackTable.tsx b/packages/app/hooks/packs/usePackTable.tsx index e49528416..9cb2fab84 100644 --- a/packages/app/hooks/packs/usePackTable.tsx +++ b/packages/app/hooks/packs/usePackTable.tsx @@ -3,8 +3,7 @@ import { ItemCategoryEnum } from 'app/constants/itemCategory'; import { convertWeight } from 'app/utils/convertWeight'; import { useAuthUser } from 'app/auth/hooks'; import { useDuplicatePackItem } from './useDuplicatePackItem'; - -type WeightUnit = 'g' | 'kg' | 'oz' | 'lb' | 'lbs'; +import { useItemWeightUnit } from 'app/hooks/items'; export const usePackTable = ({ currentPack, @@ -21,6 +20,7 @@ export const usePackTable = ({ } const [checkedItems, setCheckedItems] = useState([...ids]); + const [weightUnit, setWeightUnit] = useItemWeightUnit(); const handleDuplicate = () => { const data = { @@ -32,7 +32,6 @@ export const usePackTable = ({ duplicatePackItem(data); }; - const [weightUnit, setWeightUnit] = useState('g'); const data = currentPack?.items; let totalFoodWeight = 0; let totalWaterWeight = 0; diff --git a/server/src/services/item/addGlobalItemToPackService.ts b/server/src/services/item/addGlobalItemToPackService.ts index 021446038..4be07ad27 100644 --- a/server/src/services/item/addGlobalItemToPackService.ts +++ b/server/src/services/item/addGlobalItemToPackService.ts @@ -19,7 +19,6 @@ export const addGlobalItemToPackService = async ( ): Promise => { const itemClass = new Item(); const itemPacksClass = new ItemPacks(); - const itemOwnersClass = new ItemOwners(); const item = await itemClass.findItem({ id: itemId, isGlobal: true, @@ -27,7 +26,13 @@ export const addGlobalItemToPackService = async ( if (!item) { throw new Error('Global Item does not exist!'); } - await itemPacksClass.create({ itemId: item.id, packId }); - // await itemOwnersClass.create({ itemId: item.id, ownerId }); - return item; + const { id, ...duplicatedItemValues } = item; + const newItem = await itemClass.create({ + ...duplicatedItemValues, + global: false, + ownerId, + }); + await itemPacksClass.create({ itemId: newItem.id, packId }); + + return newItem; }; diff --git a/server/src/services/item/deleteItemService.ts b/server/src/services/item/deleteItemService.ts index 0b1315e68..a0faf9717 100644 --- a/server/src/services/item/deleteItemService.ts +++ b/server/src/services/item/deleteItemService.ts @@ -15,7 +15,9 @@ export const deleteItemService = async ( packId: string, ): Promise => { const ItemPacksClass = new ItemPacks(); - console.log(itemId); + const itemClass = new Item(); + + await itemClass.delete(itemId); await ItemPacksClass.delete(itemId, packId); return { message: 'Item deleted successfully' };