Skip to content

Commit

Permalink
fix: fix item issues, unit and internal items
Browse files Browse the repository at this point in the history
  • Loading branch information
taronaleksanian committed Apr 23, 2024
1 parent 9486f99 commit eeeaf7c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
8 changes: 5 additions & 3 deletions packages/app/components/feed/FeedCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,6 +59,7 @@ export default function Card({
useTheme();

const { addFavorite } = useAddFavorite();
const [weightUnit] = useItemWeightUnit();

const { data: favorites = [] } = useFetchUserFavorites(user?.id);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -184,7 +186,7 @@ export default function Card({

{type === 'pack' && (
<RText fontSize="$1" color="mediumpurple" ml={-0.5} mt={-1}>
Total Weight: {formattedWeight}
Total Weight: {formatNumber(formattedWeight)} {weightUnit}
</RText>
)}

Expand Down
1 change: 1 addition & 0 deletions packages/app/hooks/items/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { useAddItem } from './useAddItem';
export { useItems } from './useItems';
export { useDeleteItem } from './useDeleteItem';
export { useItemsUpdater } from './useItemsUpdater';
export { useItemWeightUnit } from './useItemWeightUnit';
17 changes: 17 additions & 0 deletions packages/app/hooks/items/useItemWeightUnit.ts
Original file line number Diff line number Diff line change
@@ -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<React.SetStateAction<WeightUnit>>,
] => {
const authUser = useAuthUser();
const userPreferWeight = authUser.preferredWeight as WeightUnit | null;
const [weightUnit, setWeightUnit] = useState<WeightUnit>(
userPreferWeight || 'kg',
);

return [weightUnit, setWeightUnit];
};
5 changes: 2 additions & 3 deletions packages/app/hooks/packs/usePackTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,6 +20,7 @@ export const usePackTable = ({
}

const [checkedItems, setCheckedItems] = useState([...ids]);
const [weightUnit, setWeightUnit] = useItemWeightUnit();

const handleDuplicate = () => {
const data = {
Expand All @@ -32,7 +32,6 @@ export const usePackTable = ({
duplicatePackItem(data);
};

const [weightUnit, setWeightUnit] = useState<WeightUnit>('g');
const data = currentPack?.items;
let totalFoodWeight = 0;
let totalWaterWeight = 0;
Expand Down
13 changes: 9 additions & 4 deletions server/src/services/item/addGlobalItemToPackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ export const addGlobalItemToPackService = async (
): Promise<object> => {
const itemClass = new Item();
const itemPacksClass = new ItemPacks();
const itemOwnersClass = new ItemOwners();
const item = await itemClass.findItem({
id: itemId,
isGlobal: true,
});
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;
};
4 changes: 3 additions & 1 deletion server/src/services/item/deleteItemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const deleteItemService = async (
packId: string,
): Promise<object> => {
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' };
Expand Down

0 comments on commit eeeaf7c

Please sign in to comment.