summaryrefslogtreecommitdiff
path: root/lib/menu-v2/components/add-node-dialog.tsx
blob: b67628207530a3a032e97c20bcd3f2a247bcf188 (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
"use client";

import { useForm } from "react-hook-form";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import type { 
  MenuDomain, 
  CreateMenuGroupInput, 
  CreateGroupInput, 
  CreateTopLevelMenuInput 
} from "../types";

type DialogType = "menu_group" | "group" | "top_level_menu";

interface AddNodeDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  type: DialogType;
  domain: MenuDomain;
  parentId?: number; // group 생성 시 필요
  onSave: (data: CreateMenuGroupInput | CreateGroupInput | CreateTopLevelMenuInput) => Promise<void>;
}

interface FormData {
  titleKo: string;
  titleEn: string;
  menuPath: string;
}

export function AddNodeDialog({
  open,
  onOpenChange,
  type,
  domain,
  parentId,
  onSave,
}: AddNodeDialogProps) {
  const {
    register,
    handleSubmit,
    reset,
    formState: { isSubmitting, errors },
  } = useForm<FormData>({
    defaultValues: {
      titleKo: "",
      titleEn: "",
      menuPath: "",
    },
  });

  const getTitle = () => {
    switch (type) {
      case "menu_group":
        return "Add Menu Group";
      case "group":
        return "Add Group";
      case "top_level_menu":
        return "Add Top-Level Menu";
      default:
        return "Add";
    }
  };

  const getDescription = () => {
    switch (type) {
      case "menu_group":
        return "A dropdown trigger displayed in the header navigation.";
      case "group":
        return "Groups menus within a menu group.";
      case "top_level_menu":
        return "A single link displayed in the header navigation.";
      default:
        return "";
    }
  };

  const onSubmit = async (data: FormData) => {
    let saveData: CreateMenuGroupInput | CreateGroupInput | CreateTopLevelMenuInput;
    
    if (type === "menu_group") {
      saveData = {
        titleKo: data.titleKo,
        titleEn: data.titleEn || undefined,
      };
    } else if (type === "group" && parentId) {
      saveData = {
        parentId,
        titleKo: data.titleKo,
        titleEn: data.titleEn || undefined,
      };
    } else if (type === "top_level_menu") {
      saveData = {
        titleKo: data.titleKo,
        titleEn: data.titleEn || undefined,
        menuPath: data.menuPath,
      };
    } else {
      return;
    }
    
    await onSave(saveData);
    reset();
    onOpenChange(false);
  };

  const handleClose = () => {
    reset();
    onOpenChange(false);
  };

  return (
    <Dialog open={open} onOpenChange={handleClose}>
      <DialogContent className="max-w-md">
        <DialogHeader>
          <DialogTitle>{getTitle()}</DialogTitle>
          <DialogDescription>{getDescription()}</DialogDescription>
        </DialogHeader>
        
        <form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
          <div className="grid gap-4">
            {/* Korean Name */}
            <div className="grid gap-2">
              <Label htmlFor="titleKo">Name (Korean) *</Label>
              <Input
                id="titleKo"
                {...register("titleKo", { required: "Name is required" })}
                placeholder="Master Data"
              />
              {errors.titleKo && (
                <p className="text-xs text-destructive">{errors.titleKo.message}</p>
              )}
            </div>

            {/* English Name */}
            <div className="grid gap-2">
              <Label htmlFor="titleEn">Name (English)</Label>
              <Input
                id="titleEn"
                {...register("titleEn")}
                placeholder="Master Data"
              />
            </div>

            {/* Menu Path for Top-Level Menu */}
            {type === "top_level_menu" && (
              <div className="grid gap-2">
                <Label htmlFor="menuPath">Menu Path *</Label>
                <Input
                  id="menuPath"
                  {...register("menuPath", { 
                    required: type === "top_level_menu" ? "Path is required" : false 
                  })}
                  placeholder={`/${domain}/dashboard`}
                />
                {errors.menuPath && (
                  <p className="text-xs text-destructive">{errors.menuPath.message}</p>
                )}
                <p className="text-xs text-muted-foreground">
                  e.g., /{domain}/report, /{domain}/faq
                </p>
              </div>
            )}
          </div>

          <DialogFooter>
            <Button type="button" variant="outline" onClick={handleClose}>
              Cancel
            </Button>
            <Button type="submit" disabled={isSubmitting}>
              {isSubmitting ? "Creating..." : "Create"}
            </Button>
          </DialogFooter>
        </form>
      </DialogContent>
    </Dialog>
  );
}