Fix edit lint/typing issues and preload listing images #7
2 changed files with 18 additions and 5 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { ListingStatus, UserRole, UserStatus } from '@prisma/client';
|
import { ListingStatus, Role, UserStatus } from '@prisma/client';
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { prisma } from '../../../../lib/prisma';
|
import { prisma } from '../../../../lib/prisma';
|
||||||
import { requireAuth } from '../../../../lib/jwt';
|
import { requireAuth } from '../../../../lib/jwt';
|
||||||
|
|
@ -117,7 +117,7 @@ export async function PUT(req: Request, { params }: { params: { id: string } })
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = existing.status;
|
let status = existing.status;
|
||||||
const autoApprove = !saveDraft && (process.env.AUTO_APPROVE_LISTINGS === 'true' || auth.role === UserRole.ADMIN);
|
const autoApprove = !saveDraft && (process.env.AUTO_APPROVE_LISTINGS === 'true' || auth.role === Role.ADMIN);
|
||||||
if (saveDraft) {
|
if (saveDraft) {
|
||||||
status = ListingStatus.DRAFT;
|
status = ListingStatus.DRAFT;
|
||||||
} else if (existing.status === ListingStatus.PUBLISHED) {
|
} else if (existing.status === ListingStatus.PUBLISHED) {
|
||||||
|
|
@ -186,7 +186,7 @@ export async function PUT(req: Request, { params }: { params: { id: string } })
|
||||||
const updateData: any = {
|
const updateData: any = {
|
||||||
status,
|
status,
|
||||||
approvedAt: status === ListingStatus.PUBLISHED ? existing.approvedAt ?? new Date() : null,
|
approvedAt: status === ListingStatus.PUBLISHED ? existing.approvedAt ?? new Date() : null,
|
||||||
approvedById: status === ListingStatus.PUBLISHED && auth.role === UserRole.ADMIN ? auth.userId : existing.approvedById,
|
approvedById: status === ListingStatus.PUBLISHED && auth.role === Role.ADMIN ? auth.userId : existing.approvedById,
|
||||||
country: country || null,
|
country: country || null,
|
||||||
region: region || null,
|
region: region || null,
|
||||||
city: city || null,
|
city: city || null,
|
||||||
|
|
|
||||||
|
|
@ -88,8 +88,12 @@ export default function EditListingPage({ params }: { params: { id: string } })
|
||||||
}
|
}
|
||||||
const listing = data.listing;
|
const listing = data.listing;
|
||||||
setInitialStatus(listing.status as ListingStatus);
|
setInitialStatus(listing.status as ListingStatus);
|
||||||
const translationMap = { ...translations };
|
const translationMap: Record<Locale, LocaleFields> = {
|
||||||
const slugMap = { ...suggestedSlugs };
|
en: { title: '', description: '', teaser: '' },
|
||||||
|
fi: { title: '', description: '', teaser: '' },
|
||||||
|
sv: { title: '', description: '', teaser: '' },
|
||||||
|
};
|
||||||
|
const slugMap: Record<Locale, string> = { en: '', fi: '', sv: '' };
|
||||||
SUPPORTED_LOCALES.forEach((loc) => {
|
SUPPORTED_LOCALES.forEach((loc) => {
|
||||||
const found = listing.translations.find((t: any) => t.locale === loc);
|
const found = listing.translations.find((t: any) => t.locale === loc);
|
||||||
if (found) {
|
if (found) {
|
||||||
|
|
@ -138,6 +142,15 @@ export default function EditListingPage({ params }: { params: { id: string } })
|
||||||
if (listing.images?.length) {
|
if (listing.images?.length) {
|
||||||
const coverIdx = listing.images.find((img: any) => img.isCover)?.order ?? 1;
|
const coverIdx = listing.images.find((img: any) => img.isCover)?.order ?? 1;
|
||||||
setCoverImageIndex(coverIdx);
|
setCoverImageIndex(coverIdx);
|
||||||
|
setSelectedImages(
|
||||||
|
listing.images.map((img: any) => ({
|
||||||
|
name: img.altText || img.url || `image-${img.id}`,
|
||||||
|
size: img.size || 0,
|
||||||
|
mimeType: img.mimeType || 'image/jpeg',
|
||||||
|
dataUrl: img.url || `/api/images/${img.id}`,
|
||||||
|
isExisting: true,
|
||||||
|
})),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setError(err.message || 'Failed to load listing');
|
setError(err.message || 'Failed to load listing');
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue