fix: 매장 등록 폼 검증 실패 시 입력값 보존
서버 검증 에러 발생 시 fieldValues를 반환하고 각 입력 필드에 defaultValue로 바인딩하여 사용자 입력 유지
This commit is contained in:
@@ -8,17 +8,25 @@ import type { CreateStoreDraftInput } from '@startover/domain';
|
||||
|
||||
export type StoreFormState = {
|
||||
error?: string;
|
||||
fieldValues?: {
|
||||
listingTitle?: string;
|
||||
regionClusterCode?: string;
|
||||
industryLeafCode?: string;
|
||||
roadAddress?: string;
|
||||
depositAmount?: string;
|
||||
monthlyRentAmount?: string;
|
||||
premiumAmount?: string;
|
||||
remainingLeaseMonths?: string;
|
||||
exclusiveAreaSqm?: string;
|
||||
floorLevel?: string;
|
||||
kitchenEquipmentSummary?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export async function createStoreDraftAction(
|
||||
_prevState: StoreFormState,
|
||||
formData: FormData,
|
||||
): Promise<StoreFormState> {
|
||||
const session = await auth();
|
||||
if (!session?.user?.dbId) {
|
||||
return { error: '로그인이 필요합니다.' };
|
||||
}
|
||||
|
||||
const listingTitle = (formData.get('listingTitle') as string | null)?.trim() ?? '';
|
||||
const regionClusterCode = (formData.get('regionClusterCode') as string | null) ?? '';
|
||||
const industryLeafCode = (formData.get('industryLeafCode') as string | null) ?? '';
|
||||
@@ -32,6 +40,25 @@ export async function createStoreDraftAction(
|
||||
const kitchenEquipmentSummary =
|
||||
(formData.get('kitchenEquipmentSummary') as string | null)?.trim() ?? '';
|
||||
|
||||
const fieldValues = {
|
||||
listingTitle,
|
||||
regionClusterCode,
|
||||
industryLeafCode,
|
||||
roadAddress,
|
||||
depositAmount: depositAmount ?? undefined,
|
||||
monthlyRentAmount: monthlyRentAmount ?? undefined,
|
||||
premiumAmount: premiumAmount ?? undefined,
|
||||
remainingLeaseMonths: remainingLeaseMonths ?? undefined,
|
||||
exclusiveAreaSqm: exclusiveAreaSqm ?? undefined,
|
||||
floorLevel: floorLevel ?? undefined,
|
||||
kitchenEquipmentSummary,
|
||||
};
|
||||
|
||||
const session = await auth();
|
||||
if (!session?.user?.dbId) {
|
||||
return { error: '로그인이 필요합니다.', fieldValues };
|
||||
}
|
||||
|
||||
const input: CreateStoreDraftInput = {
|
||||
ownerUserId: session.user.dbId,
|
||||
listingTitle,
|
||||
@@ -66,7 +93,7 @@ export async function createStoreDraftAction(
|
||||
const result = await createStoreDraftService(prisma, input);
|
||||
|
||||
if (!result.ok) {
|
||||
return { error: result.error.message };
|
||||
return { error: result.error.message, fieldValues };
|
||||
}
|
||||
|
||||
redirect(`/stores/${result.value.publicId}`);
|
||||
|
||||
Reference in New Issue
Block a user