summaryrefslogtreecommitdiff
path: root/lib/tech-vendors/contacts-table/update-contact-sheet.tsx
blob: b75ddd1e73552bffd108b256dcc05b84e7bd2f8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"use client"

import * as React from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { toast } from "sonner"

import { Button } from "@/components/ui/button"
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import {
  Sheet,
  SheetContent,
  SheetDescription,
  SheetHeader,
  SheetTitle,
} from "@/components/ui/sheet"
import { Checkbox } from "@/components/ui/checkbox"
import { Loader2 } from "lucide-react"

import type { TechVendorContact } from "@/db/schema/techVendors"
import { updateTechVendorContactSchema, type UpdateTechVendorContactSchema } from "../validations"
import { updateTechVendorContact } from "../service"

interface UpdateContactSheetProps
  extends React.ComponentPropsWithoutRef<typeof Sheet> {
  contact: TechVendorContact | null
  vendorId: number
}

export function UpdateContactSheet({ contact, vendorId, ...props }: UpdateContactSheetProps) {
  const [isPending, startTransition] = React.useTransition()

  const form = useForm<UpdateTechVendorContactSchema>({
    resolver: zodResolver(updateTechVendorContactSchema),
    defaultValues: {
      contactName: contact?.contactName ?? "",
      contactPosition: contact?.contactPosition ?? "",
      contactEmail: contact?.contactEmail ?? "",
      contactPhone: contact?.contactPhone ?? "",
      contactCountry: contact?.contactCountry ?? "",
      isPrimary: contact?.isPrimary ?? false,
    },
  })

  React.useEffect(() => {
    if (contact) {
      form.reset({
        contactName: contact.contactName,
        contactPosition: contact.contactPosition ?? "",
        contactEmail: contact.contactEmail,
        contactPhone: contact.contactPhone ?? "",
        contactCountry: contact.contactCountry ?? "",
        isPrimary: contact.isPrimary,
      })
    }
  }, [contact, form])

  async function onSubmit(data: UpdateTechVendorContactSchema) {
    if (!contact) return
    
    startTransition(async () => {
      try {
        const { error } = await updateTechVendorContact({ 
          id: contact.id,
          vendorId: vendorId,
          ...data 
        })
        
        if (error) throw new Error(error)
        
        toast.success("연락처 정보가 업데이트되었습니다!")
        form.reset()
        props.onOpenChange?.(false)
      } catch (err: unknown) {
        toast.error(String(err))
      }
    })
  }

  return (
    <Sheet {...props}>
      <SheetContent className="space-y-4 w-[600px] sm:max-w-[600px]">
        <SheetHeader>
          <SheetTitle>연락처 수정</SheetTitle>
          <SheetDescription>
            연락처 정보를 수정하세요. 완료되면 저장 버튼을 클릭하세요.
          </SheetDescription>
        </SheetHeader>
        
        <Form {...form}>
          <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
            <div className="grid grid-cols-1 gap-4">
              <FormField
                control={form.control}
                name="contactName"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>담당자명 *</FormLabel>
                    <FormControl>
                      <Input placeholder="담당자명을 입력하세요" {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <FormField
                control={form.control}
                name="contactPosition"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>직책</FormLabel>
                    <FormControl>
                      <Input placeholder="직책을 입력하세요" {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <FormField
                control={form.control}
                name="contactEmail"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>이메일</FormLabel>
                    <FormControl>
                      <Input 
                        type="email" 
                        placeholder="이메일을 입력하세요" 
                        {...field} 
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <FormField
                control={form.control}
                name="contactPhone"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>전화번호</FormLabel>
                    <FormControl>
                      <Input placeholder="전화번호를 입력하세요" {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <FormField
                control={form.control}
                name="contactCountry"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>국가</FormLabel>
                    <FormControl>
                      <Input placeholder="국가를 입력하세요" {...field} />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <FormField
                control={form.control}
                name="isPrimary"
                render={({ field }) => (
                  <FormItem className="flex flex-row items-start space-x-3 space-y-0">
                    <FormControl>
                      <Checkbox
                        checked={field.value}
                        onCheckedChange={field.onChange}
                      />
                    </FormControl>
                    <div className="space-y-1 leading-none">
                      <FormLabel>주 담당자</FormLabel>
                    </div>
                  </FormItem>
                )}
              />
            </div>

            <div className="flex justify-end space-x-2">
              <Button
                type="button"
                variant="outline"
                onClick={() => props.onOpenChange?.(false)}
              >
                취소
              </Button>
              <Button type="submit" disabled={isPending}>
                {isPending && (
                  <Loader2
                    className="mr-2 h-4 w-4 animate-spin"
                    aria-hidden="true"
                  />
                )}
                저장
              </Button>
            </div>
          </form>
        </Form>
      </SheetContent>
    </Sheet>
  )
}