create review and profile

This commit is contained in:
arifal
2026-04-20 00:25:05 +07:00
parent 7159e9108f
commit ba5f64ae0e
37 changed files with 3324 additions and 109 deletions
+28 -5
View File
@@ -2,9 +2,10 @@
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { createTripSchema } from "./schemas";
import { createTripSchema, tripImageUrlsSchema } from "./schemas";
import { tripService } from "@/server/services/trip.service";
import { revalidatePath } from "next/cache";
import { tripStoredInstantFromYmd } from "@/lib/trip-dates";
export async function createTripAction(formData: FormData) {
const session = await getServerSession(authOptions);
@@ -28,21 +29,37 @@ export async function createTripAction(formData: FormData) {
return { error: result.error.issues[0].message };
}
// Collect image URLs from form (multiple inputs named "imageUrls")
const imageUrls = formData
const imageUrlsRaw = formData
.getAll("imageUrls")
.map((v) => (v as string).trim())
.filter(Boolean);
const imagesParsed = tripImageUrlsSchema.safeParse(imageUrlsRaw);
if (!imagesParsed.success) {
return { error: imagesParsed.error.issues[0].message };
}
const imageUrls = imagesParsed.data;
const date = tripStoredInstantFromYmd(result.data.date);
let endDate = result.data.endDate
? tripStoredInstantFromYmd(result.data.endDate)
: undefined;
if (endDate && endDate.getTime() === date.getTime()) {
endDate = undefined;
}
try {
const trip = await tripService.createTrip({
...result.data,
date: new Date(result.data.date),
endDate: result.data.endDate ? new Date(result.data.endDate) : undefined,
date,
endDate,
organizerId: session.user.id,
imageUrls: imageUrls.length > 0 ? imageUrls : undefined,
});
revalidatePath("/trips");
revalidatePath("/");
revalidatePath("/profile");
return { success: true, tripId: trip.id };
} catch (err) {
return { error: (err as Error).message };
@@ -58,6 +75,9 @@ export async function joinTripAction(tripId: string) {
try {
await tripService.joinTrip(tripId, session.user.id);
revalidatePath(`/trips/${tripId}`);
revalidatePath("/trips");
revalidatePath("/");
revalidatePath("/profile");
return { success: true };
} catch (err) {
return { error: (err as Error).message };
@@ -73,6 +93,9 @@ export async function cancelJoinAction(tripId: string) {
try {
await tripService.cancelJoin(tripId, session.user.id);
revalidatePath(`/trips/${tripId}`);
revalidatePath("/trips");
revalidatePath("/");
revalidatePath("/profile");
return { success: true };
} catch (err) {
return { error: (err as Error).message };