trails/apps/journal/app/routes/activities.new.server.ts
Ullrich Schäfer 2cb32cd2d3
activity-sport-type: schema, contract, write/read paths, federation
Implements the activity-sport-type change (specs/activity-sport-type):

- db: nullable `sport_type` column on journal.activities + SportType /
  SPORT_TYPES (text().$type<> convention).
- api: optional sportType on the activity read + create schemas (mirrored
  SPORT_TYPES; @trails-cool/api stays zod-only).
- write: ActivityInput + createActivity persist it; mapSportType() normalizes
  provider strings (Komoot bulk import passes tour.sport; Garmin unset);
  threaded through the unified importActivity.
- read/display: sportType added to the detail/feed/profile loaders and the v1
  REST endpoints; shared SportBadge (glyph + i18n label) on detail, feed, and
  profile; sport-aware feed verb; create-form <select>.
- i18n: journal.activities.sport.* (labels + verbs) in en + de.
- federation: `sport` PropertyValue on the Note when set.

Tests: mapSportType unit table; federation asserts the sport attachment is
present when set and omitted when unset. typecheck + lint + unit all green.
E2E (create→badge) still to add.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-12 14:11:41 +02:00

51 lines
1.7 KiB
TypeScript

// Server-only loader/action for /activities/new. See `home.server.ts`.
import { data, redirect } from "react-router";
import { requireSessionUser } from "~/lib/auth/session.server";
import { createActivity } from "~/lib/activities.server";
import { listRoutes } from "~/lib/routes.server";
import { SPORT_TYPES } from "@trails-cool/db/schema/journal";
import type { SportType } from "@trails-cool/db/schema/journal";
export async function loadActivitiesNew(request: Request) {
const user = await requireSessionUser(request);
const userRoutes = await listRoutes(user.id);
return {
routes: userRoutes.map((r) => ({ id: r.id, name: r.name })),
};
}
export async function activitiesNewAction(request: Request) {
const user = await requireSessionUser(request);
const formData = await request.formData();
const name = formData.get("name") as string;
const description = formData.get("description") as string;
const routeId = formData.get("routeId") as string | null;
const sportRaw = formData.get("sportType");
const gpxFile = formData.get("gpx") as File | null;
if (!name) return data({ error: "Name is required" }, { status: 400 });
// Empty/absent selection → unspecified; anything else must be a known value.
const sportType =
typeof sportRaw === "string" && (SPORT_TYPES as readonly string[]).includes(sportRaw)
? (sportRaw as SportType)
: undefined;
let gpx: string | undefined;
if (gpxFile && gpxFile.size > 0) {
gpx = await gpxFile.text();
}
const activityId = await createActivity(user.id, {
name,
description,
sportType,
gpx,
routeId: routeId || undefined,
});
return redirect(`/activities/${activityId}`);
}