Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions app/(tabs)/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ export const GET_USER = gql(`
email
birth_date
gender
actualList {
_id
status
}
}
}
`);
Expand Down Expand Up @@ -82,7 +78,7 @@ export const CREATE_LIST = gql(`
`);

export default function HomeScreen() {
const { id: userId, setId } = useAuth();
const { id: userId, setId, currentListId, setCurrentListId } = useAuth();
const [upsertProduct] = useMutation(ADD_HOMESCREEN_PRODUCTS);
const [createList] = useMutation(CREATE_LIST);
const router = useRouter();
Expand All @@ -109,10 +105,7 @@ export default function HomeScreen() {
return;
}

let listId =
userData.user?.actualList?.status === Status.Active
? userData.user?.actualList._id
: undefined;
let listId = currentListId;

if (!listId) {
const result = await createList({
Expand All @@ -128,13 +121,14 @@ export default function HomeScreen() {
}

listId = result.data.createList._id;
setCurrentListId(listId).catch(toastOnError);
}

await upsertProduct({
variables: {
models: EANS.map((ean) => ({
id_composite: {
listId: listId,
listId: listId ?? "",
productEan: ean,
},
quantity: 1,
Expand Down Expand Up @@ -254,15 +248,15 @@ export default function HomeScreen() {
}}
>
<NavModal
userName={userData?.user?.first_name || "Guest"}
userName={userData?.user?.first_name ?? "Guest"}
onLogout={async () => {
await setId(null);
router.push("/login");
}}
onProfilePress={() =>
router.push({
pathname: "/profile",
params: { user: JSON.stringify(userData?.user || {}) },
params: { user: JSON.stringify(userData?.user ?? {}) },
})
}
onHomePress={() => router.push("/(tabs)")}
Expand Down
29 changes: 13 additions & 16 deletions app/(tabs)/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import Path from "@/assets/images/path.png";
import { useToast } from "@/hooks/useToast";

const GET_FILTERED_SUPERMARKETS = gql(`
query GetFilteredSupermarkets($userId: String!, $coordinateFilter: CoordinateFilter!) {
user(id: $userId) {
actualList {
products {
quantity
supermarketInfo {
price
}
query GetFilteredSupermarkets($listId: String!, $coordinateFilter: CoordinateFilter!) {
list(id: $listId) {
products {
quantity
supermarketInfo {
price
}
}
}
Expand All @@ -49,15 +47,15 @@ const GET_FILTERED_SUPERMARKETS = gql(`
`);

const NavigationScreen = () => {
const { id } = useAuth();
const { toast } = useToast();
const { currentListId } = useAuth();
const { toast, toastOnError } = useToast();
const { setState: setBackground } = useBackground();
const iconBackgroundColor = useThemeColor("text");
const [userLocation, setUserLocation] = useState<Coordinate | null>(null);

const { loading, error, data } = useQuery(GET_FILTERED_SUPERMARKETS, {
variables: {
userId: id!,
listId: currentListId ?? "",
coordinateFilter: {
op: Operator.Le,
value: {
Expand All @@ -69,8 +67,7 @@ const NavigationScreen = () => {
},
},
},
skip: !userLocation,
onError: (err) => console.error("Query error:", err),
skip: !userLocation || !currentListId,
});

const isLoading = loading || !userLocation;
Expand Down Expand Up @@ -102,14 +99,14 @@ const NavigationScreen = () => {
});
};

getLocation();
}, [toast]);
getLocation().catch(toastOnError);
}, [toast, toastOnError]);

const { totalQuantity, totalPrice } = useMemo(() => {
let quantity = 0;
let price = 0;

data?.user?.actualList?.products?.forEach(
data?.list?.products?.forEach(
(item: { quantity: number; supermarketInfo: { price: number } }) => {
quantity += item.quantity;
price += (item.supermarketInfo?.price || 0) * item.quantity;
Expand Down
Loading
Loading