-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
522b7fd
commit c857773
Showing
25 changed files
with
653 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"extends": ["eslint:recommended", "next/core-web-vitals"] | ||
"extends": ["eslint:recommended", "next/core-web-vitals"], | ||
"env": { | ||
"es2020": true | ||
}, | ||
"rules": { | ||
"no-console": "warn" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { getActivities } from "@/lib/db/activities"; | ||
import getServerSession from "@/lib/get-server-session"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { serializeError } from "serialize-error"; | ||
import { z } from "zod"; | ||
|
||
const originTypeQueryParamSchema = z | ||
.union([z.literal("STRAVA"), z.literal("GPX")]) | ||
.nullable(); | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
const session = await getServerSession(); | ||
|
||
if (!session) { | ||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
} | ||
|
||
const originType = originTypeQueryParamSchema.safeParse( | ||
new URL(req.url).searchParams.get("originType") | ||
); | ||
|
||
if (!originType.success) { | ||
return NextResponse.json(originType.error.issues, { status: 422 }); | ||
} | ||
|
||
const data = await getActivities(session.user.id, originType.data); | ||
|
||
return NextResponse.json(data, { status: 200 }); | ||
} catch (error) { | ||
return NextResponse.json(serializeError(error), { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { z } from "zod"; | ||
|
||
import serverTimings from "@/lib/server-timings"; | ||
import getServerSession from "@/lib/get-server-session"; | ||
import { serializeError } from "serialize-error"; | ||
import { addAscent, deleteAscent } from "@/lib/db/ascent"; | ||
import { activitySchema } from "@/lib/db/activities"; | ||
|
||
const routeContextSchema = z.object({ | ||
params: z.object({ | ||
id: z.string(), | ||
}), | ||
}); | ||
|
||
const bodySchema = z.array( | ||
activitySchema | ||
.omit({ | ||
id: true, | ||
userId: true, | ||
createdAt: true, | ||
updatedAt: true, | ||
}) | ||
.extend({ | ||
startDate: z.string().transform((date) => new Date(date)), | ||
}) | ||
); | ||
|
||
async function handler( | ||
req: NextRequest, | ||
context: z.infer<typeof routeContextSchema> | ||
) { | ||
try { | ||
const safeContext = routeContextSchema.safeParse(context); | ||
|
||
if (!safeContext.success) { | ||
return NextResponse.json(safeContext.error.issues, { status: 422 }); | ||
} | ||
|
||
const id = safeContext.data.params.id; | ||
const serverTiming = new serverTimings(); | ||
const session = await getServerSession(); | ||
|
||
if (!session) { | ||
return NextResponse.json( | ||
{ error: "Unauthorized" }, | ||
{ status: 401, headers: serverTiming.headers() } | ||
); | ||
} | ||
|
||
serverTiming.start("db"); | ||
|
||
let data; | ||
|
||
if (req.method === "PUT") { | ||
const safeBody = bodySchema.safeParse(await req.json()); | ||
|
||
if (!safeBody.success) { | ||
return NextResponse.json(safeBody.error.issues, { status: 422 }); | ||
} | ||
|
||
data = await addAscent(session.user.id, id, safeBody.data); | ||
} else if (req.method === "DELETE") { | ||
data = await deleteAscent(session.user.id, id); | ||
} else { | ||
return NextResponse.json( | ||
{ error: "Method not allowed" }, | ||
{ status: 405 } | ||
); | ||
} | ||
|
||
serverTiming.stop("db"); | ||
|
||
return NextResponse.json(data, { | ||
status: 200, | ||
headers: serverTiming.headers(), | ||
}); | ||
} catch (error) { | ||
return NextResponse.json(serializeError(error), { status: 500 }); | ||
} | ||
} | ||
export const PUT = handler; | ||
export const DELETE = handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getAscents } from "@/lib/db/ascent"; | ||
import getServerSession from "@/lib/get-server-session"; | ||
import { NextResponse } from "next/server"; | ||
import { serializeError } from "serialize-error"; | ||
|
||
export async function GET() { | ||
try { | ||
const session = await getServerSession(); | ||
|
||
if (!session) { | ||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
} | ||
|
||
const data = await getAscents(session.user.id); | ||
|
||
return NextResponse.json(data, { status: 200 }); | ||
} catch (error) { | ||
return NextResponse.json(serializeError(error), { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { getCims } from "@/lib/db/cims"; | ||
import { NextResponse } from "next/server"; | ||
import { serializeError } from "serialize-error"; | ||
|
||
export async function GET() { | ||
try { | ||
const data = await getCims(); | ||
|
||
return NextResponse.json(data, { status: 200 }); | ||
} catch (error) { | ||
return NextResponse.json(serializeError(error), { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { OriginType, activitySchema } from "@/lib/db/activities"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
export function useActivitiesQuery( | ||
{ | ||
originType, | ||
}: { | ||
originType: OriginType; | ||
} = { originType: null } | ||
) { | ||
const { data, error, isFetching } = useQuery({ | ||
queryKey: ["activities", originType], | ||
queryFn: () => | ||
fetch("/api/activities" + `?originType=${originType}`) | ||
.then((res) => res.json()) | ||
.then((data) => activitySchema.array().parse(data)), | ||
initialData: [], | ||
refetchOnMount: false, | ||
}); | ||
|
||
return { data, error, isFetching }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Activity } from "@/lib/db/activities"; | ||
import { | ||
DefaultError, | ||
useMutation, | ||
useQueryClient, | ||
} from "@tanstack/react-query"; | ||
|
||
type Variables = { | ||
cimId: string; | ||
action: "ADD" | "REMOVE"; | ||
activities?: Omit<Activity, "id" | "userId" | "createdAt" | "updatedAt">[]; | ||
}; | ||
|
||
export function useAscentMutation() { | ||
const queryClient = useQueryClient(); | ||
|
||
const { isPending, variables, mutate } = useMutation< | ||
unknown, | ||
DefaultError, | ||
Variables | ||
>({ | ||
mutationKey: ["ascents"], | ||
mutationFn: ({ action, cimId, activities = [] }) => | ||
fetch(`/api/ascents/${cimId}`, { | ||
method: action === "ADD" ? "PUT" : action === "REMOVE" ? "DELETE" : "", | ||
body: JSON.stringify(activities), | ||
}).then((res) => res.json()), | ||
onSuccess: () => | ||
queryClient.invalidateQueries({ | ||
queryKey: ["ascents"], | ||
}), | ||
}); | ||
|
||
return { isPending, variables, mutate }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ascentSchema } from "@/lib/db/ascent"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
export function useAscentsQuery() { | ||
const { data, error, isFetching } = useQuery({ | ||
queryKey: ["ascents"], | ||
queryFn: () => | ||
fetch("/api/ascents") | ||
.then((res) => res.json()) | ||
.then((data) => ascentSchema.array().parse(data)), | ||
initialData: [], | ||
}); | ||
|
||
return { data, error, isFetching }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { cimSchema } from "@/lib/db/cims"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
export function useCimsQuery() { | ||
const { data, error, isFetching } = useQuery({ | ||
queryKey: ["cims"], | ||
queryFn: () => | ||
fetch("/api/cims") | ||
.then((res) => res.json()) | ||
.then((data) => cimSchema.array().parse(data)), | ||
initialData: [], | ||
}); | ||
|
||
return { data, error, isFetching }; | ||
} |
Oops, something went wrong.