chore: add load more functionality, with refactored css
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import * as api from "@/lib/api"
|
||||
import {
|
||||
benefits,
|
||||
extendedUser,
|
||||
nextLevelPerks,
|
||||
previousStays,
|
||||
upcomingStays,
|
||||
} from "./temp"
|
||||
import {
|
||||
badRequestError,
|
||||
forbiddenError,
|
||||
@@ -13,9 +8,15 @@ import {
|
||||
unauthorizedError,
|
||||
} from "@/server/errors/trpc"
|
||||
import { protectedProcedure, router } from "@/server/trpc"
|
||||
import { z } from "zod"
|
||||
|
||||
import { getUserSchema } from "./output"
|
||||
import {
|
||||
benefits,
|
||||
extendedUser,
|
||||
nextLevelPerks,
|
||||
previousStays,
|
||||
upcomingStays,
|
||||
} from "./temp"
|
||||
|
||||
function fakingRequest<T>(payload: T): Promise<T> {
|
||||
return new Promise((resolve) => {
|
||||
@@ -34,7 +35,6 @@ export const userQueryRouter = router({
|
||||
Authorization: `Bearer ${ctx.session.token.access_token}`,
|
||||
},
|
||||
})
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
switch (apiResponse.status) {
|
||||
case 400:
|
||||
@@ -67,7 +67,7 @@ export const userQueryRouter = router({
|
||||
name: `${verifiedData.data.name} ${verifiedData.data.lastName}`,
|
||||
}
|
||||
} catch (error) {
|
||||
console.info(`GEt User Error`)
|
||||
console.info(`Get User Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
}
|
||||
@@ -90,25 +90,74 @@ export const userQueryRouter = router({
|
||||
.object({
|
||||
perPage: z.number().min(0).default(6),
|
||||
page: z.number().min(0).default(0),
|
||||
cursor: z.number().nullish(),
|
||||
})
|
||||
.default({})
|
||||
)
|
||||
.query(async (opts) => {
|
||||
const { perPage, page } = opts.input
|
||||
return previousStays.slice(page * perPage, page * perPage + perPage)
|
||||
const { perPage, page, cursor } = opts.input
|
||||
let nextCursor: typeof cursor | undefined = undefined
|
||||
const nrPages = Math.ceil(previousStays.length / perPage)
|
||||
|
||||
let stays, nextPage
|
||||
if (cursor) {
|
||||
stays = previousStays.slice(cursor, perPage + cursor + 1)
|
||||
nextPage = cursor / perPage + 1
|
||||
} else {
|
||||
stays = previousStays.slice(
|
||||
page * perPage,
|
||||
page * perPage + perPage + 1
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
(nextPage && nextPage < nrPages && stays.length == perPage + 1) ||
|
||||
(!nextPage && nrPages > 1)
|
||||
) {
|
||||
const nextItem = stays.pop()
|
||||
if (nextItem) {
|
||||
nextCursor = previousStays.indexOf(nextItem)
|
||||
}
|
||||
} // TODO: Make request to get user data from Scandic API
|
||||
return { data: stays, nextCursor }
|
||||
}),
|
||||
|
||||
upcoming: protectedProcedure
|
||||
.input(
|
||||
z
|
||||
.object({
|
||||
perPage: z.number().min(0).default(6),
|
||||
page: z.number().min(0).default(0),
|
||||
cursor: z.number().nullish(),
|
||||
})
|
||||
.default({})
|
||||
)
|
||||
.query(async (opts) => {
|
||||
const { perPage, page } = opts.input
|
||||
return upcomingStays.slice(page * perPage, page * perPage + perPage)
|
||||
const { perPage, page, cursor } = opts.input
|
||||
let nextCursor: typeof cursor | undefined = undefined
|
||||
const nrPages = Math.ceil(upcomingStays.length / perPage)
|
||||
|
||||
let stays, nextPage
|
||||
if (cursor) {
|
||||
stays = upcomingStays.slice(cursor, perPage + cursor + 1)
|
||||
nextPage = cursor / perPage + 1
|
||||
} else {
|
||||
stays = upcomingStays.slice(
|
||||
page * perPage,
|
||||
page * perPage + perPage + 1
|
||||
)
|
||||
}
|
||||
|
||||
if (
|
||||
(nextPage && nextPage < nrPages && stays.length == perPage + 1) ||
|
||||
(!nextPage && nrPages > 1)
|
||||
) {
|
||||
const nextItem = stays.pop()
|
||||
if (nextItem) {
|
||||
nextCursor = upcomingStays.indexOf(nextItem)
|
||||
}
|
||||
} // TODO: Make request to get user data from Scandic API
|
||||
return { data: stays, nextCursor }
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user