Skip to content

Commit

Permalink
checkout tarons commits
Browse files Browse the repository at this point in the history
pinocchio-life-like committed Sep 16, 2024
1 parent 8cdb8ee commit 9261042
Showing 11 changed files with 87 additions and 63 deletions.
11 changes: 3 additions & 8 deletions packages/app/modules/feed/components/FeedCard/utils.ts
Original file line number Diff line number Diff line change
@@ -29,15 +29,10 @@ export const feedItemPackCardConverter: Converter<
? roundNumber(input.similarityScore)
: undefined,
weight: input.total_weight,
quantity:
input?.itemPacks?.reduce(
(accumulator, currentValue) =>
accumulator + currentValue?.item?.quantity,
0,
) ?? 0,
quantity: input.quantity,
},
isUserFavorite: input?.userFavoritePacks?.some(
(obj) => obj?.userId === currentUserId,
isUserFavorite: input?.userFavoritePacks?.some?.(
(userId) => userId === currentUserId,
),
favoriteCount: input.favorites_count,
};
5 changes: 3 additions & 2 deletions packages/app/modules/feed/components/FeedSearchFilter.tsx
Original file line number Diff line number Diff line change
@@ -164,7 +164,8 @@ export const FeedSearchFilter = ({
</RSwitch>*/}
</RStack>
)}
<RStack
{/* DISABLE SORTS */}
{/* <RStack
style={{
flexDirection: 'row',
alignItems: 'center',
@@ -190,7 +191,7 @@ export const FeedSearchFilter = ({
placeholder={queryString}
/>
</View>
</RStack>
</RStack> */}
{(feedType === 'userPacks' || feedType === 'userTrips') && (
<RButton
style={{ marginLeft: 'auto', marginTop: 8 }}
3 changes: 2 additions & 1 deletion packages/app/modules/feed/hooks/useAddFavorite.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@ export function useAddFavorite() {
onSuccess: () => {
// Invalidate and refetch. Update to be more specific
utils.getUserFavorites.invalidate();
utils.getPublicPacks.invalidate();
utils.getUserPacksFeed.invalidate();
utils.getPublicFeed.invalidate();
utils.getPacks.invalidate(userId ? { ownerId: userId } : undefined);
},
});
3 changes: 1 addition & 2 deletions packages/app/modules/feed/hooks/useFeed.ts
Original file line number Diff line number Diff line change
@@ -25,10 +25,9 @@ export const useFeed = ({
selectedTypes: Object;
id: string;
}> = {}): UseFeedResult => {
console.log("querystring", queryString)
switch (feedType) {
case 'public':
return usePublicFeed(queryString, selectedTypes); // Use the typed return from usePublicFeed
return usePublicFeed(queryString, selectedTypes);
case 'userPacks':
return useUserPacks(ownerId || undefined, queryString);
case 'userTrips':
91 changes: 55 additions & 36 deletions packages/app/modules/feed/hooks/usePublicFeed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { queryTrpc } from 'app/trpc';
import { useState, useEffect, useCallback } from 'react';
import { useState, useEffect, useCallback, useMemo } from 'react';
import { debounce } from 'lodash';

type DataType = {
type: string;
@@ -20,7 +21,7 @@ export const usePublicFeed = (
queryString: string,
selectedTypes,
initialPage = 1,
initialLimit = 10
initialLimit = 10
) => {
const [page, setPage] = useState(initialPage);
const [data, setData] = useState<OptionalDataType>([]);
@@ -48,51 +49,69 @@ export const usePublicFeed = (
{ enabled: selectedTypes.trip }
);

useEffect(() => {
const processFetchedData = () => {
// if (!isPacksLoading && !isTripsLoading && (publicPacksData || publicTripsData)) {
if (!isPacksLoading && (publicPacksData || publicTripsData)) {
let newData: OptionalDataType = [];
// Process fetched data
const processedData = useMemo(() => {
let newData: OptionalDataType = [];

// Add packs to the data
if (selectedTypes.pack && publicPacksData) {
newData = [...newData, ...publicPacksData.map((item) => ({ ...item, type: 'pack' }))];
}
if (selectedTypes.pack && publicPacksData) {
newData = [...newData, ...publicPacksData.map((item) => ({ ...item, type: 'pack' }))];
}

// Add trips to the data
if (selectedTypes.trip && publicTripsData) {
newData = [...newData, ...publicTripsData.map((item) => ({ ...item, type: 'trip' }))];
}
if (selectedTypes.trip && publicTripsData) {
newData = [...newData, ...publicTripsData.map((item) => ({ ...item, type: 'trip' }))];
}

// Append or reset data based on the current page
setData((prevData) => (page === initialPage ? newData : [...prevData, ...newData]));
return newData;
}, [publicPacksData, publicTripsData, selectedTypes]);

// Check if there is more data to fetch (if the fetched data length is less than the limit, there's no more data)
// Use effect to update data and loading states
useEffect(() => {
const processFetchedData = () => {
// if (!isPacksLoading && !isTripsLoading && processedData.length > 0) {
if (!isPacksLoading && processedData.length > 0) {
// Only update if data is different to avoid unnecessary re-renders
setData((prevData) => {
const newData = page === initialPage ? processedData : [...prevData, ...processedData];
return JSON.stringify(newData) !== JSON.stringify(prevData) ? newData : prevData;
});

// Check if there is more data to fetch
const hasMorePacks = publicPacksData && publicPacksData.length === initialLimit;
const hasMoreTrips = publicTripsData && publicTripsData.length === initialLimit;

setHasMore(hasMorePacks || hasMoreTrips); // Properly set `hasMore`

setIsFetchingNextPage(false);
setIsLoading(false);
setHasMore(hasMorePacks || hasMoreTrips);
}

setIsFetchingNextPage(false);
setIsLoading(false);
};

processFetchedData();
}, [publicPacksData, publicTripsData, isPacksLoading, isTripsLoading, page, selectedTypes]);

// Fetch next page of data
const fetchNextPage = useCallback(async () => {
if (hasMore && !isFetchingNextPage && !isLoading) {
setIsFetchingNextPage(true);
setPage((prevPage) => prevPage + 1);

await refetchPacks();
if (selectedTypes.trip) {
await refetchTrips();
}, [
publicPacksData,
publicTripsData,
isPacksLoading,
isTripsLoading,
page,
processedData,
initialPage,
initialLimit,
]);

// Debounced fetchNextPage to prevent rapid triggers
const fetchNextPage = useCallback(
debounce(async () => {
if (hasMore && !isFetchingNextPage && !isLoading) {
setIsFetchingNextPage(true);
setPage((prevPage) => prevPage + 1);

await refetchPacks();
if (selectedTypes.trip) {
await refetchTrips();
}
}
}
}, [hasMore, isFetchingNextPage, isLoading, refetchPacks, refetchTrips, selectedTypes]);
}, 300), // Debounce with a delay of 300ms
[hasMore, isFetchingNextPage, isLoading, refetchPacks, refetchTrips, selectedTypes]
);

return { data, isLoading, hasMore, fetchNextPage, refetch: refetchPacks, isFetchingNextPage };
};
3 changes: 2 additions & 1 deletion packages/app/modules/feed/model.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,8 @@ export interface FeedItem {
favorited_by: Array<{
id: string;
}>;
userFavoritePacks?: Array<{ userId: string }>;
quantity?: number;
userFavoritePacks?: string[];
favorites_count: number;
owner_id: string | { id: string };
destination: string;
3 changes: 2 additions & 1 deletion packages/app/modules/item/hooks/useSimilarItems.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { queryTrpc } from 'app/trpc';

export const useSimilarItems = (id: string) => {
export const useSimilarItems = (id: string, enabled = true) => {
const { data, error, isLoading, refetch } =
queryTrpc.getSimilarItems.useQuery(
{ id, limit: 10 },
{
enabled,
refetchOnWindowFocus: true,
},
);
19 changes: 11 additions & 8 deletions packages/app/modules/pack/components/PackCard/PackPrimaryCard.tsx
Original file line number Diff line number Diff line change
@@ -10,19 +10,22 @@ import { type PackDetails } from 'app/modules/pack/model';
import { DuplicateIcon } from 'app/assets/icons';
import { useItemWeightUnit } from 'app/modules/item';
import { convertWeight } from 'app/utils/convertWeight';
import { roundNumber } from 'app/utils';

interface PackCardProps extends FeedCardProps<PackDetails> {}

export const PackPrimaryCard: FC<PackCardProps> = (props) => {
const [weightUnit] = useItemWeightUnit();
const packDetails = Object.entries(props.details).map(([key, value]) => ({
key,
label: key,
value:
key === 'weight'
? `${convertWeight(value, 'g', weightUnit)} ${weightUnit}`
: value,
}));
const packDetails = Object.entries(props.details)
.filter(([key]) => key !== 'similarityScore')
.map(([key, value]) => ({
key,
label: key,
value:
key === 'weight'
? `${roundNumber(convertWeight(value, 'kg', weightUnit))} ${weightUnit}`
: value,
}));

return (
<Card
3 changes: 2 additions & 1 deletion packages/app/modules/pack/hooks/useSimilarPacks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { queryTrpc } from 'app/trpc';

export const useSimilarPacks = (id: string) => {
export const useSimilarPacks = (id: string, enabled: boolean = true) => {
const { data, error, isLoading, refetch } =
queryTrpc.getSimilarPacks.useQuery(
{ id, limit: 10 },
{
enabled,
refetchOnWindowFocus: true,
},
);
7 changes: 5 additions & 2 deletions packages/app/modules/trip/hooks/useUserTrips.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { queryTrpc } from 'app/trpc';

export const useUserTrips = (ownerId: string | undefined) => {
export const useUserTrips = (
ownerId: string | undefined,
queryEnabled: boolean = true,
) => {
// If ownerId is not provided, don’t run the query.
const enabled = !!ownerId;
const enabled = queryEnabled && !!ownerId;

// Leverage the query hook provided by tRPC
const { data, error, isLoading, refetch } = queryTrpc.getTrips.useQuery(
2 changes: 1 addition & 1 deletion packages/app/modules/user/hooks/useProfile.ts
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ export const useProfile = (id = null) => {
data: allPacks,
isLoading: allPacksLoading,
error: allPacksError,
} = useUserPacks(userId); // TODO: Add enabled as parameter
} = useUserPacks(userId, { isPublic: true }, '', true); // TODO: Add enabled as parameter

const {
data: allTrips,

0 comments on commit 9261042

Please sign in to comment.