CreateUserForm.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. "use client";
  2. import React from "react";
  3. import { Loader2 } from "lucide-react";
  4. import { normalizeBranchIdDraft } from "@/components/admin/users/usersUi";
  5. import { CREATE_ROLE_OPTIONS } from "@/components/admin/users/create-user/createUserUtils";
  6. import { Button } from "@/components/ui/button";
  7. import { Input } from "@/components/ui/input";
  8. import { Label } from "@/components/ui/label";
  9. import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert";
  10. import { DialogFooter } from "@/components/ui/dialog";
  11. import {
  12. DropdownMenu,
  13. DropdownMenuContent,
  14. DropdownMenuLabel,
  15. DropdownMenuRadioGroup,
  16. DropdownMenuRadioItem,
  17. DropdownMenuSeparator,
  18. DropdownMenuTrigger,
  19. } from "@/components/ui/dropdown-menu";
  20. function RoleSelect({ value, onChange, disabled }) {
  21. const label =
  22. CREATE_ROLE_OPTIONS.find((x) => x.value === value)?.label || "Rolle wählen";
  23. return (
  24. <div className="grid gap-2">
  25. <Label>Rolle</Label>
  26. <DropdownMenu>
  27. <DropdownMenuTrigger asChild>
  28. <Button type="button" variant="outline" disabled={disabled}>
  29. {label}
  30. </Button>
  31. </DropdownMenuTrigger>
  32. <DropdownMenuContent align="start" className="min-w-56">
  33. <DropdownMenuLabel>Rolle auswählen</DropdownMenuLabel>
  34. <DropdownMenuSeparator />
  35. <DropdownMenuRadioGroup value={value} onValueChange={onChange}>
  36. {CREATE_ROLE_OPTIONS.map((opt) => (
  37. <DropdownMenuRadioItem key={opt.value} value={opt.value}>
  38. {opt.label}
  39. </DropdownMenuRadioItem>
  40. ))}
  41. </DropdownMenuRadioGroup>
  42. </DropdownMenuContent>
  43. </DropdownMenu>
  44. </div>
  45. );
  46. }
  47. export default function CreateUserForm({
  48. form,
  49. setPatch,
  50. error,
  51. policyLines,
  52. disabled,
  53. isSubmitting,
  54. onCancel,
  55. onSubmit,
  56. }) {
  57. const role = String(form?.role || "branch");
  58. return (
  59. <form onSubmit={onSubmit} className="space-y-4">
  60. {error ? (
  61. <Alert variant="destructive">
  62. <AlertTitle>{error.title}</AlertTitle>
  63. {error.description ? (
  64. <AlertDescription>{error.description}</AlertDescription>
  65. ) : null}
  66. {Array.isArray(error.hints) && error.hints.length > 0 ? (
  67. <AlertDescription>
  68. <ul className="mt-2 list-disc pl-5">
  69. {error.hints.map((line) => (
  70. <li key={line}>{line}</li>
  71. ))}
  72. </ul>
  73. </AlertDescription>
  74. ) : null}
  75. </Alert>
  76. ) : null}
  77. <div className="grid gap-3 md:grid-cols-2">
  78. <div className="grid gap-2">
  79. <Label htmlFor="cu-username">Benutzername</Label>
  80. <Input
  81. id="cu-username"
  82. value={form?.username ?? ""}
  83. onChange={(e) => setPatch({ username: e.target.value })}
  84. disabled={disabled}
  85. autoCapitalize="none"
  86. autoCorrect="off"
  87. spellCheck={false}
  88. placeholder="z. B. branchuser"
  89. />
  90. </div>
  91. <div className="grid gap-2">
  92. <Label htmlFor="cu-email">E-Mail</Label>
  93. <Input
  94. id="cu-email"
  95. value={form?.email ?? ""}
  96. onChange={(e) => setPatch({ email: e.target.value })}
  97. disabled={disabled}
  98. placeholder="name@firma.de"
  99. />
  100. </div>
  101. </div>
  102. <div className="grid gap-3 md:grid-cols-2">
  103. <RoleSelect
  104. value={role}
  105. onChange={(v) => setPatch({ role: v })}
  106. disabled={disabled}
  107. />
  108. {role === "branch" ? (
  109. <div className="grid gap-2">
  110. <Label htmlFor="cu-branch">Niederlassung</Label>
  111. <Input
  112. id="cu-branch"
  113. value={form?.branchId ?? ""}
  114. onChange={(e) => setPatch({ branchId: e.target.value })}
  115. disabled={disabled}
  116. placeholder="z. B. NL01"
  117. />
  118. </div>
  119. ) : (
  120. <div className="grid gap-2">
  121. <Label>Niederlassung</Label>
  122. <Input value="—" disabled />
  123. </div>
  124. )}
  125. </div>
  126. {/* Keep branchId normalized as user types (optional UX polish) */}
  127. {role === "branch" ? (
  128. <div className="text-xs text-muted-foreground">
  129. Aktuell: {normalizeBranchIdDraft(form?.branchId || "") || "—"}
  130. </div>
  131. ) : null}
  132. <div className="grid gap-2">
  133. <Label htmlFor="cu-password">Initiales Passwort</Label>
  134. <Input
  135. id="cu-password"
  136. type="password"
  137. value={form?.initialPassword ?? ""}
  138. onChange={(e) => setPatch({ initialPassword: e.target.value })}
  139. disabled={disabled}
  140. placeholder="••••••••"
  141. />
  142. <ul className="mt-1 list-disc pl-5 text-xs text-muted-foreground">
  143. {(Array.isArray(policyLines) ? policyLines : []).map((line) => (
  144. <li key={line}>{line}</li>
  145. ))}
  146. </ul>
  147. </div>
  148. <DialogFooter>
  149. <Button
  150. type="button"
  151. variant="outline"
  152. disabled={disabled}
  153. onClick={onCancel}
  154. >
  155. Abbrechen
  156. </Button>
  157. <Button type="submit" disabled={disabled}>
  158. {isSubmitting ? (
  159. <>
  160. <Loader2 className="h-4 w-4 animate-spin" />
  161. Speichern…
  162. </>
  163. ) : (
  164. "Anlegen"
  165. )}
  166. </Button>
  167. </DialogFooter>
  168. </form>
  169. );
  170. }