summaryrefslogtreecommitdiff
path: root/lib/tags-plant/table/update-tag-sheet.tsx
blob: 2be1e7321818379eeec40c2b7badee12097ea361 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
"use client"

import * as React from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { Loader2, Check, ChevronsUpDown } from "lucide-react"
import { useForm } from "react-hook-form"
import { toast } from "sonner"
import { z } from "zod"

import { Button } from "@/components/ui/button"
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
import {
  Sheet,
  SheetClose,
  SheetContent,
  SheetDescription,
  SheetFooter,
  SheetHeader,
  SheetTitle,
} from "@/components/ui/sheet"
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover"
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from "@/components/ui/command"
import { Badge } from "@/components/ui/badge"
import { cn } from "@/lib/utils"

import { Tag } from "@/db/schema/vendorData"
import { updateTag, getSubfieldsByTagType, getClassOptions, TagTypeOption } from "@/lib/tags-plant/service"

// SubFieldDef 인터페이스
interface SubFieldDef {
  name: string
  label: string
  type: "select" | "text"
  options?: { value: string; label: string }[]
  expression?: string
  delimiter?: string
}

// 클래스 옵션 인터페이스
interface UpdatedClassOption {
  code: string
  label: string
  tagTypeCode: string
  tagTypeDescription?: string
}

// UpdateTagSchema 정의
const updateTagSchema = z.object({
  class: z.string().min(1, "Class is required"),
  tagType: z.string().min(1, "Tag Type is required"),
  tagNo: z.string().min(1, "Tag Number is required"),
  description: z.string().optional(),
  // 추가 필드들은 동적으로 처리됨
})

// TypeScript 타입 정의
type UpdateTagSchema = z.infer<typeof updateTagSchema> & Record<string, string>

interface UpdateTagSheetProps extends React.ComponentPropsWithRef<typeof Sheet> {
  tag: Tag | null
  packageCode: string
  projectCode: string
}

export function UpdateTagSheet({ tag, packageCode, projectCode,...props }: UpdateTagSheetProps) {
  const [isUpdatePending, startUpdateTransition] = React.useTransition()
  const [tagTypeList, setTagTypeList] = React.useState<TagTypeOption[]>([])
  const [selectedTagTypeCode, setSelectedTagTypeCode] = React.useState<string | null>(null)
  const [subFields, setSubFields] = React.useState<SubFieldDef[]>([])
  const [classOptions, setClassOptions] = React.useState<UpdatedClassOption[]>([])
  const [classSearchTerm, setClassSearchTerm] = React.useState("")
  const [isLoadingClasses, setIsLoadingClasses] = React.useState(false)
  const [isLoadingSubFields, setIsLoadingSubFields] = React.useState(false)

  // ID management for popover elements
  const selectIdRef = React.useRef(0)
  const fieldIdsRef = React.useRef<Record<string, string>>({})
  const classOptionIdsRef = React.useRef<Record<string, string>>({})


  // Load class options when sheet opens
  React.useEffect(() => {
    const loadClassOptions = async () => {
      if (!props.open || !tag) return

      setIsLoadingClasses(true)
      try {
        const result = await getClassOptions(packageCode, projectCode)
        setClassOptions(result)
      } catch (err) {
        toast.error("클래스 옵션을 불러오는데 실패했습니다.")
      } finally {
        setIsLoadingClasses(false)
      }
    }

    loadClassOptions()
  }, [props.open, tag])

  // Form setup
  const form = useForm<UpdateTagSchema>({
    resolver: zodResolver(updateTagSchema),
    defaultValues: {
      class: "",
      tagType: "",
      tagNo: "",
      description: "",
    },
  })

  // Load tag data into form when tag changes
  React.useEffect(() => {
    if (!tag) return

    // 필요한 필드만 선택적으로 추출
    const formValues = {
      tagNo: tag.tagNo,
      tagType: tag.tagType,
      class: tag.class,
      description: tag.description || ""
      // 참고: 실제 태그 데이터에는 서브필드(functionCode, seqNumber 등)가 없음
    };

    // 폼 초기화
    form.reset(formValues)

    // 태그 타입 코드 설정 (추가 필드 로딩을 위해)
    if (tag.tagType) {
      // 해당 태그 타입에 맞는 클래스 옵션을 찾아서 태그 타입 코드 설정
      const foundClass = classOptions.find(opt => opt.label === tag.class)
      if (foundClass?.tagTypeCode) {
        setSelectedTagTypeCode(foundClass.tagTypeCode)
        loadSubFieldsByTagTypeCode(foundClass.tagTypeCode)
      }
    }
  }, [tag, classOptions, form])

  // Load subfields by tag type code
  async function loadSubFieldsByTagTypeCode(tagTypeCode: string) {
    setIsLoadingSubFields(true)
    try {
      const { subFields: apiSubFields } = await getSubfieldsByTagType(tagTypeCode, projectCode)
      const formattedSubFields: SubFieldDef[] = apiSubFields.map(field => ({
        name: field.name,
        label: field.label,
        type: field.type,
        options: field.options || [],
        expression: field.expression ?? undefined,
        delimiter: field.delimiter ?? undefined,
      }))
      setSubFields(formattedSubFields)
      return true
    } catch (err) {
      toast.error("서브필드를 불러오는데 실패했습니다.")
      setSubFields([])
      return false
    } finally {
      setIsLoadingSubFields(false)
    }
  }

  // Handle class selection
  async function handleSelectClass(classOption: UpdatedClassOption) {
    form.setValue("class", classOption.label, { shouldValidate: true })

    if (classOption.tagTypeCode) {
      setSelectedTagTypeCode(classOption.tagTypeCode)

      // Set tag type
      const tagType = tagTypeList.find(t => t.id === classOption.tagTypeCode)
      if (tagType) {
        form.setValue("tagType", tagType.label, { shouldValidate: true })
      } else if (classOption.tagTypeDescription) {
        form.setValue("tagType", classOption.tagTypeDescription, { shouldValidate: true })
      }

      await loadSubFieldsByTagTypeCode(classOption.tagTypeCode)
    }
  }

  // Form submission handler
  function onSubmit(data: UpdateTagSchema) {
    startUpdateTransition(async () => {
      if (!tag) return

      try {
        // 기본 필드와 서브필드 데이터 결합
        const tagData = {
          id: tag.id,
          tagType: data.tagType,
          class: data.class,
          tagNo: data.tagNo,
          description: data.description,
          ...Object.fromEntries(
            subFields.map(field => [field.name, data[field.name] || ""])
          ),
        }

        const result = await updateTag(tagData, projectCode,packageCode )

        if ("error" in result) {
          toast.error(result.error)
          return
        }

        form.reset()
        props.onOpenChange?.(false)
        toast.success("태그가 성공적으로 업데이트되었습니다")
      } catch (error) {
        console.error("Error updating tag:", error)
        toast.error("태그 업데이트 중 오류가 발생했습니다")
      }
    })
  }

  // Render class field
  function renderClassField(field: any) {
    const [popoverOpen, setPopoverOpen] = React.useState(false)

    const buttonId = React.useMemo(
      () => `class-button-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
      []
    )
    const popoverContentId = React.useMemo(
      () => `class-popover-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
      []
    )
    const commandId = React.useMemo(
      () => `class-command-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
      []
    )

    return (
      <FormItem>
        <FormLabel>Class</FormLabel>
        <FormControl>
          <Popover open={popoverOpen} onOpenChange={setPopoverOpen}>
            <PopoverTrigger asChild>
              <Button
                key={buttonId}
                type="button"
                variant="outline"
                className="w-full justify-between relative h-9"
                disabled={isLoadingClasses}
              >
                {isLoadingClasses ? (
                  <>
                    <span>클래스 로딩 중...</span>
                    <Loader2 className="ml-2 h-4 w-4 animate-spin" />
                  </>
                ) : (
                  <>
                    <span className="truncate mr-1 flex-grow text-left">
                      {field.value || "클래스 선택..."}
                    </span>
                    <ChevronsUpDown className="h-4 w-4 opacity-50 flex-shrink-0" />
                  </>
                )}
              </Button>
            </PopoverTrigger>
            <PopoverContent key={popoverContentId} className="w-[300px] p-0">
              <Command key={commandId}>
                <CommandInput
                  key={`${commandId}-input`}
                  placeholder="클래스 검색..."
                  value={classSearchTerm}
                  onValueChange={setClassSearchTerm}
                />
                <CommandList key={`${commandId}-list`} className="max-h-[300px]">
                  <CommandEmpty key={`${commandId}-empty`}>검색 결과가 없습니다.</CommandEmpty>
                  <CommandGroup key={`${commandId}-group`}>
                    {classOptions.map((opt, optIndex) => {
                      if (!classOptionIdsRef.current[opt.code]) {
                        classOptionIdsRef.current[opt.code] =
                          `class-${opt.code}-${Date.now()}-${Math.random()
                            .toString(36)
                            .slice(2, 9)}`
                      }
                      const optionId = classOptionIdsRef.current[opt.code]

                      return (
                        <CommandItem
                          key={`${optionId}-${optIndex}`}
                          onSelect={() => {
                            field.onChange(opt.label)
                            setPopoverOpen(false)
                            handleSelectClass(opt)
                          }}
                          value={opt.label}
                          className="truncate"
                          title={opt.label}
                        >
                          <span className="truncate">{opt.label}</span>
                          <Check
                            key={`${optionId}-check`}
                            className={cn(
                              "ml-auto h-4 w-4 flex-shrink-0",
                              field.value === opt.label ? "opacity-100" : "opacity-0"
                            )}
                          />
                        </CommandItem>
                      )
                    })}
                  </CommandGroup>
                </CommandList>
              </Command>
            </PopoverContent>
          </Popover>
        </FormControl>
        <FormMessage />
      </FormItem>
    )
  }

  // Render TagType field (readonly)
  function renderTagTypeField(field: any) {
    return (
      <FormItem>
        <FormLabel>Tag Type</FormLabel>
        <FormControl>
          <div className="relative">
            <Input
              {...field}
              readOnly
              className="h-9 bg-muted"
            />
          </div>
        </FormControl>
        <FormMessage />
      </FormItem>
    )
  }

  // Render Tag Number field (readonly)
  function renderTagNoField(field: any) {
    return (
      <FormItem>
        <FormLabel>Tag Number</FormLabel>
        <FormControl>
          <div className="relative">
            <Input
              {...field}
              readOnly
              className="h-9 bg-muted font-mono"
            />
          </div>
        </FormControl>
        <FormMessage />
      </FormItem>
    )
  }

  // Render form fields for each subfield
  function renderSubFields() {
    if (isLoadingSubFields) {
      return (
        <div className="flex justify-center items-center py-4">
          <Loader2 className="h-6 w-6 animate-spin text-primary" />
          <div className="ml-3 text-muted-foreground">필드 로딩 중...</div>
        </div>
      )
    }

    if (subFields.length === 0) {
      return null
    }

    return (
      <div className="space-y-4">
        <div className="text-sm font-medium text-muted-foreground">추가 필드</div>
        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          {subFields.map((sf, index) => (
            <FormField
              key={`subfield-${sf.name}-${index}`}
              control={form.control}
              name={sf.name}
              render={({ field }) => (
                <FormItem>
                  <FormLabel>{sf.label}</FormLabel>
                  <FormControl>
                    {sf.type === "select" ? (
                      <Select
                        value={field.value || ""}
                        onValueChange={field.onChange}
                      >
                        <SelectTrigger className="w-full h-9">
                          <SelectValue placeholder={`${sf.label} 선택...`} />
                        </SelectTrigger>
                        <SelectContent
                          align="start"
                          side="bottom"
                          className="max-h-[250px]"
                          style={{ minWidth: "250px", maxWidth: "350px" }}
                        >
                          {sf.options?.map((opt, optIndex) => (
                            <SelectItem
                              key={`${sf.name}-${opt.value}-${optIndex}`}
                              value={opt.value}
                              title={opt.label}
                              className="whitespace-normal py-2 break-words"
                            >
                              {opt.label}
                            </SelectItem>
                          ))}
                        </SelectContent>
                      </Select>
                    ) : (
                      <Input
                        {...field}
                        className="h-9"
                        placeholder={`${sf.label} 입력...`}
                      />
                    )}
                  </FormControl>
                  {sf.expression && (
                    <p className="text-xs text-muted-foreground mt-1" title={sf.expression}>
                      {sf.expression}
                    </p>
                  )}
                  <FormMessage />
                </FormItem>
              )}
            />
          ))}
        </div>
      </div>
    )
  }

  // 컴포넌트 렌더링
  return (
    <Sheet {...props}>
      {/* <SheetContent className="flex flex-col gap-0 sm:max-w-md overflow-y-auto"> */}
      <SheetContent className="flex flex-col gap-6 sm:max-w-lg overflow-y-auto">
        <SheetHeader className="text-left">
          <SheetTitle>태그 수정</SheetTitle>
          <SheetDescription>
            태그 정보를 업데이트하고 변경 사항을 저장하세요
          </SheetDescription>
        </SheetHeader>

        <div className="flex-1 overflow-y-auto py-4">
          <Form {...form}>
            <form
              id="update-tag-form"
              onSubmit={form.handleSubmit(onSubmit)}
              className="space-y-6"
            >
              {/* 기본 태그 정보 */}
              <div className="space-y-4">
                {/* Class */}
                <FormField
                  control={form.control}
                  name="class"
                  render={({ field }) => renderClassField(field)}
                />

                {/* Tag Type */}
                <FormField
                  control={form.control}
                  name="tagType"
                  render={({ field }) => renderTagTypeField(field)}
                />

                {/* Tag Number */}
                <FormField
                  control={form.control}
                  name="tagNo"
                  render={({ field }) => renderTagNoField(field)}
                />

                {/* Description */}
                <FormField
                  control={form.control}
                  name="description"
                  render={({ field }) => (
                    <FormItem>
                      <FormLabel>Description</FormLabel>
                      <FormControl>
                        <Input
                          {...field}
                          placeholder="태그 설명 입력..."
                          className="h-9"
                        />
                      </FormControl>
                      <FormMessage />
                    </FormItem>
                  )}
                />
              </div>

              {/* 서브필드 */}
              {renderSubFields()}
            </form>
          </Form>
        </div>

        <SheetFooter className="pt-2">
          <SheetClose asChild>
            <Button type="button" variant="outline">
              취소
            </Button>
          </SheetClose>
          <Button
            type="submit"
            form="update-tag-form"
            disabled={isUpdatePending || isLoadingSubFields}
          >
            {isUpdatePending ? (
              <>
                <Loader2 className="mr-2 h-4 w-4 animate-spin" aria-hidden="true" />
                저장 중...
              </>
            ) : (
              "저장"
            )}
          </Button>
        </SheetFooter>
      </SheetContent>
    </Sheet>
  )
}