fix: handle empty stays in query

This commit is contained in:
Christel Westerberg
2024-05-13 09:46:14 +02:00
parent 058f0cc898
commit a91ff342f0
5 changed files with 30 additions and 199 deletions

View File

@@ -27,19 +27,20 @@ export default function PreviousStays({
)
function loadMoreData() {
fetchNextPage()
if (hasNextPage) {
fetchNextPage()
}
}
const stays = data?.pages.flatMap((page) => page.data) ?? []
return (
<Container>
<Header title={title} subtitle={subtitle} link={link} />
{data?.pages.length ? (
{stays.length ? (
<ListContainer>
<StayList
lang={lang}
stays={data?.pages.flatMap((page) => page.data) ?? []}
/>
<StayList lang={lang} stays={stays} />
{hasNextPage ? (
<ShowMoreButton disabled={isFetching} loadMoreData={loadMoreData} />
) : null}

View File

@@ -33,16 +33,15 @@ export default function UpcomingStays({
}
}
const stays = data?.pages.flatMap((page) => page.data) ?? []
return (
<Container>
<Header title={title} subtitle={subtitle} link={link} />
{data?.pages.length ? (
{stays.length ? (
<ListContainer>
<StayList
lang={lang}
stays={data?.pages.flatMap((page) => page.data) ?? []}
/>
<StayList lang={lang} stays={stays} />
{hasNextPage ? (
<ShowMoreButton disabled={isFetching} loadMoreData={loadMoreData} />
) : null}

View File

@@ -78,14 +78,16 @@ export const getStaysSchema = z.object({
}),
})
),
links: z.object({
self: z.string(),
offset: z.number(),
limit: z.number(),
totalCount: z.number(),
}),
links: z
.object({
self: z.string(),
offset: z.number(),
limit: z.number(),
totalCount: z.number(),
})
.nullable(),
})
type GetStaysData = z.infer<typeof getStaysSchema>
export type GetStaysData = z.infer<typeof getStaysSchema>
export type Stay = GetStaysData["data"][number]
export type Stay = GetStaysData["data"][0]

View File

@@ -1,5 +1,3 @@
import { z } from "zod"
import * as api from "@/lib/api"
import {
badRequestError,
@@ -43,6 +41,7 @@ export const userQueryRouter = router({
}
const apiJson = await apiResponse.json()
if (!apiJson.data?.length) {
throw internalServerError()
}
@@ -113,7 +112,7 @@ export const userQueryRouter = router({
}
const apiJson = await apiResponse.json()
if (!apiJson.data?.length) {
if (!apiJson.data) {
console.error(`Get Previous Stays - No data found from api call`)
throw internalServerError()
}
@@ -127,6 +126,7 @@ export const userQueryRouter = router({
}
const nextCursor =
verifiedData.data.links &&
verifiedData.data.links.offset < verifiedData.data.links.totalCount
? verifiedData.data.links.offset
: undefined
@@ -177,7 +177,7 @@ export const userQueryRouter = router({
}
const apiJson = await apiResponse.json()
if (!apiJson.data?.length) {
if (!apiJson.data) {
console.error(`Get Upcoming Stays - No data found from api call`)
throw internalServerError()
}
@@ -191,6 +191,7 @@ export const userQueryRouter = router({
}
const nextCursor =
verifiedData.data.links &&
verifiedData.data.links.offset < verifiedData.data.links.totalCount
? verifiedData.data.links.offset
: undefined

View File

@@ -1,5 +1,3 @@
import { randomUUID } from "crypto"
export const benefits = [
{
id: 1,
@@ -71,6 +69,10 @@ export const nextLevelPerks = {
id: 3,
explanation: "25% extra bonus points on each stay",
},
{
id: 4,
explanation: "25% extra bonus points on each stay",
},
],
}
@@ -113,180 +115,6 @@ export const shortcuts = [
// },
]
export const previousStays = [
{
uid: randomUUID(),
dateArrive: new Date("04 27 2024"),
dateDepart: new Date("04 28 2024"),
guests: 2,
hotel: "Scandic Helsinki Hub",
},
{
uid: randomUUID(),
dateArrive: new Date("05 27 2024"),
dateDepart: new Date("05 28 2024"),
guests: 2,
hotel: "Scandic Örebro Central",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Oslo City",
},
{
uid: randomUUID(),
dateArrive: new Date("04 27 2024"),
dateDepart: new Date("04 28 2024"),
guests: 2,
hotel: "Scandic Lorem",
},
{
uid: randomUUID(),
dateArrive: new Date("05 27 2024"),
dateDepart: new Date("05 28 2024"),
guests: 2,
hotel: "Scandic Ipsum",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Dolor Sin Amet",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Anglais",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Park",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Klara",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Järva Krog",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Kiruna",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Umeå",
},
]
export const upcomingStays = [
{
uid: randomUUID(),
dateArrive: new Date("04 27 2024"),
dateDepart: new Date("04 28 2024"),
guests: 2,
hotel: "Scandic Helsinki Hub",
},
{
uid: randomUUID(),
dateArrive: new Date("05 27 2024"),
dateDepart: new Date("05 28 2024"),
guests: 2,
hotel: "Scandic Örebro Central",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Oslo City",
},
{
uid: randomUUID(),
dateArrive: new Date("04 27 2024"),
dateDepart: new Date("04 28 2024"),
guests: 2,
hotel: "Scandic Lorem",
},
{
uid: randomUUID(),
dateArrive: new Date("05 27 2024"),
dateDepart: new Date("05 28 2024"),
guests: 2,
hotel: "Scandic Ipsum",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Dolor Sin Amet",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Anglais",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Park",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Klara",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Järva Krog",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Kiruna",
},
{
uid: randomUUID(),
dateArrive: new Date("06 27 2024"),
dateDepart: new Date("06 28 2024"),
guests: 2,
hotel: "Scandic Umeå",
},
]
export const extendedUser = {
journeys: challenges.journeys,
nights: 14,