summaryrefslogtreecommitdiff
path: root/lib/vendor-candidates/table/add-candidates-dialog.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vendor-candidates/table/add-candidates-dialog.tsx')
-rw-r--r--lib/vendor-candidates/table/add-candidates-dialog.tsx171
1 files changed, 119 insertions, 52 deletions
diff --git a/lib/vendor-candidates/table/add-candidates-dialog.tsx b/lib/vendor-candidates/table/add-candidates-dialog.tsx
index db475064..733d3716 100644
--- a/lib/vendor-candidates/table/add-candidates-dialog.tsx
+++ b/lib/vendor-candidates/table/add-candidates-dialog.tsx
@@ -8,10 +8,13 @@ import i18nIsoCountries from "i18n-iso-countries"
import enLocale from "i18n-iso-countries/langs/en.json"
import koLocale from "i18n-iso-countries/langs/ko.json"
import { cn } from "@/lib/utils"
+import { useSession } from "next-auth/react" // next-auth 세션 훅 추가
import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
+import { Textarea } from "@/components/ui/textarea"
+import { useToast } from "@/hooks/use-toast"
import {
Popover,
PopoverContent,
@@ -36,19 +39,9 @@ import {
FormMessage,
} from "@/components/ui/form"
-// shadcn/ui Select
-import {
- Select,
- SelectContent,
- SelectGroup,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/components/ui/select"
import { createVendorCandidateSchema, CreateVendorCandidateSchema } from "../validations"
import { createVendorCandidate } from "../service"
-import { vendorCandidates } from "@/db/schema/vendors"
// Register locales for countries
i18nIsoCountries.registerLocale(enLocale)
@@ -65,34 +58,65 @@ const countryArray = Object.entries(countryMap).map(([code, label]) => ({
export function AddCandidateDialog() {
const [open, setOpen] = React.useState(false)
const [isSubmitting, setIsSubmitting] = React.useState(false)
+ const { toast } = useToast()
+ const { data: session, status } = useSession()
// react-hook-form 세팅
const form = useForm<CreateVendorCandidateSchema>({
resolver: zodResolver(createVendorCandidateSchema),
defaultValues: {
companyName: "",
- contactEmail: "",
+ contactEmail: "", // 이제 빈 문자열이 허용됨
contactPhone: "",
+ taxId: "",
+ address: "",
country: "",
source: "",
- status: "COLLECTED", // Default status set to COLLECTED
+ items: "",
+ remark: "",
+ status: "COLLECTED",
},
- })
+ });
async function onSubmit(data: CreateVendorCandidateSchema) {
setIsSubmitting(true)
try {
- const result = await createVendorCandidate(data)
+ // 세션 유효성 검사
+ if (!session || !session.user || !session.user.id) {
+ toast({
+ title: "인증 오류",
+ description: "로그인 정보를 찾을 수 없습니다. 다시 로그인해주세요.",
+ variant: "destructive",
+ })
+ return
+ }
+
+ // userId 추출 (세션 구조에 따라 조정 필요)
+ const userId = session.user.id
+
+ const result = await createVendorCandidate(data, Number(userId))
if (result.error) {
- alert(`에러: ${result.error}`)
+ toast({
+ title: "오류 발생",
+ description: result.error,
+ variant: "destructive",
+ })
return
}
// 성공 시 모달 닫고 폼 리셋
+ toast({
+ title: "등록 완료",
+ description: "협력업체 후보가 성공적으로 등록되었습니다.",
+ })
form.reset()
setOpen(false)
} catch (error) {
console.error("Failed to create vendor candidate:", error)
- alert("An unexpected error occurred")
+ toast({
+ title: "오류 발생",
+ description: "예상치 못한 오류가 발생했습니다.",
+ variant: "destructive",
+ })
} finally {
setIsSubmitting(false)
}
@@ -114,7 +138,7 @@ export function AddCandidateDialog() {
</Button>
</DialogTrigger>
- <DialogContent className="sm:max-w-[425px]">
+ <DialogContent className="sm:max-w-[525px]">
<DialogHeader>
<DialogTitle>Create New Vendor Candidate</DialogTitle>
<DialogDescription>
@@ -124,17 +148,15 @@ export function AddCandidateDialog() {
{/* shadcn/ui Form을 이용해 react-hook-form과 연결 */}
<Form {...form}>
- <form onSubmit={form.handleSubmit(onSubmit)}>
- <div className="space-y-4 py-4">
+ <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
+ <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{/* Company Name 필드 */}
<FormField
control={form.control}
name="companyName"
render={({ field }) => (
<FormItem>
- <FormLabel className="after:content-['*'] after:ml-0.5 after:text-red-500">
- Company Name
- </FormLabel>
+ <FormLabel>Company Name <span className="text-red-500">*</span></FormLabel>
<FormControl>
<Input
placeholder="Enter company name"
@@ -147,15 +169,32 @@ export function AddCandidateDialog() {
)}
/>
+ {/* Tax ID 필드 (새로 추가) */}
+ <FormField
+ control={form.control}
+ name="taxId"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Tax ID</FormLabel>
+ <FormControl>
+ <Input
+ placeholder="Tax identification number"
+ {...field}
+ disabled={isSubmitting}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
{/* Contact Email 필드 */}
<FormField
control={form.control}
name="contactEmail"
render={({ field }) => (
<FormItem>
- <FormLabel className="after:content-['*'] after:ml-0.5 after:text-red-500">
- Contact Email
- </FormLabel>
+ <FormLabel>Contact Email</FormLabel>
<FormControl>
<Input
placeholder="email@example.com"
@@ -188,6 +227,25 @@ export function AddCandidateDialog() {
)}
/>
+ {/* Address 필드 */}
+ <FormField
+ control={form.control}
+ name="address"
+ render={({ field }) => (
+ <FormItem className="col-span-full">
+ <FormLabel>Address</FormLabel>
+ <FormControl>
+ <Input
+ placeholder="Company address"
+ {...field}
+ disabled={isSubmitting}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
{/* Country 필드 */}
<FormField
control={form.control}
@@ -260,7 +318,7 @@ export function AddCandidateDialog() {
name="source"
render={({ field }) => (
<FormItem>
- <FormLabel>Source</FormLabel>
+ <FormLabel>Source <span className="text-red-500">*</span></FormLabel>
<FormControl>
<Input
placeholder="Where this candidate was found"
@@ -273,37 +331,46 @@ export function AddCandidateDialog() {
)}
/>
- {/* Status 필드 */}
- {/* <FormField
+
+ {/* Items 필드 (새로 추가) */}
+ <FormField
control={form.control}
- name="status"
+ name="items"
render={({ field }) => (
- <FormItem>
- <FormLabel>Status</FormLabel>
- <Select
- onValueChange={field.onChange}
- defaultValue={field.value}
- disabled={isSubmitting}
- >
- <FormControl>
- <SelectTrigger>
- <SelectValue placeholder="Select status" />
- </SelectTrigger>
- </FormControl>
- <SelectContent>
- <SelectGroup>
- {vendorCandidates.status.enumValues.map((status) => (
- <SelectItem key={status} value={status}>
- {status}
- </SelectItem>
- ))}
- </SelectGroup>
- </SelectContent>
- </Select>
+ <FormItem className="col-span-full">
+ <FormLabel>Items <span className="text-red-500">*</span></FormLabel>
+ <FormControl>
+ <Textarea
+ placeholder="List of items or products this vendor provides"
+ className="min-h-[80px]"
+ {...field}
+ disabled={isSubmitting}
+ />
+ </FormControl>
<FormMessage />
</FormItem>
)}
- /> */}
+ />
+
+ {/* Remark 필드 (새로 추가) */}
+ <FormField
+ control={form.control}
+ name="remark"
+ render={({ field }) => (
+ <FormItem className="col-span-full">
+ <FormLabel>Remarks</FormLabel>
+ <FormControl>
+ <Textarea
+ placeholder="Additional notes or comments"
+ className="min-h-[80px]"
+ {...field}
+ disabled={isSubmitting}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
</div>
<DialogFooter>