CreateUserForm.jsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. "use client";
  2. import React from "react";
  3. import { Loader2 } from "lucide-react";
  4. import BranchNumberInput from "@/components/admin/users/BranchNumberInput";
  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. branchesStatus,
  53. branchExistence,
  54. disabled,
  55. isSubmitting,
  56. canSubmit,
  57. onCancel,
  58. onSubmit,
  59. }) {
  60. const role = String(form?.role || "branch");
  61. const branchMessageId = "cu-branch-message";
  62. const showUnknownBranch =
  63. role === "branch" && Boolean(branchExistence?.hasUnknownBranch);
  64. const showFailOpenNote = role === "branch" && Boolean(branchExistence?.listError);
  65. const showBranchLoading =
  66. role === "branch" && !showUnknownBranch && branchesStatus === "loading";
  67. return (
  68. <form onSubmit={onSubmit} className="space-y-4">
  69. {error ? (
  70. <Alert variant="destructive">
  71. <AlertTitle>{error.title}</AlertTitle>
  72. {error.description ? (
  73. <AlertDescription>{error.description}</AlertDescription>
  74. ) : null}
  75. {Array.isArray(error.hints) && error.hints.length > 0 ? (
  76. <AlertDescription>
  77. <ul className="mt-2 list-disc pl-5">
  78. {error.hints.map((line) => (
  79. <li key={line}>{line}</li>
  80. ))}
  81. </ul>
  82. </AlertDescription>
  83. ) : null}
  84. </Alert>
  85. ) : null}
  86. <div className="grid gap-3 md:grid-cols-2">
  87. <div className="grid gap-2">
  88. <Label htmlFor="cu-username">Benutzername</Label>
  89. <Input
  90. id="cu-username"
  91. value={form?.username ?? ""}
  92. onChange={(e) => setPatch({ username: e.target.value })}
  93. disabled={disabled}
  94. autoCapitalize="none"
  95. autoCorrect="off"
  96. spellCheck={false}
  97. placeholder="z. B. branchuser"
  98. />
  99. </div>
  100. <div className="grid gap-2">
  101. <Label htmlFor="cu-email">E-Mail</Label>
  102. <Input
  103. id="cu-email"
  104. value={form?.email ?? ""}
  105. onChange={(e) => setPatch({ email: e.target.value })}
  106. disabled={disabled}
  107. placeholder="name@firma.de"
  108. />
  109. </div>
  110. </div>
  111. <div className="grid gap-3 md:grid-cols-2">
  112. <RoleSelect
  113. value={role}
  114. onChange={(v) => setPatch({ role: v })}
  115. disabled={disabled}
  116. />
  117. {role === "branch" ? (
  118. <div className="grid gap-2">
  119. <BranchNumberInput
  120. id="cu-branch"
  121. branchId={form?.branchId ?? ""}
  122. onBranchIdChange={(branchId) => setPatch({ branchId })}
  123. disabled={disabled}
  124. invalid={showUnknownBranch}
  125. describedBy={showUnknownBranch ? branchMessageId : undefined}
  126. />
  127. {showUnknownBranch ? (
  128. <p id={branchMessageId} className="text-xs text-destructive">
  129. Diese Niederlassung ist nicht in der aktuellen Liste vorhanden.
  130. </p>
  131. ) : null}
  132. {showFailOpenNote ? (
  133. <p className="text-xs text-muted-foreground">
  134. Hinweis: Die Niederlassungsliste konnte nicht geladen werden.
  135. Die Existenz kann aktuell nicht geprüft werden.
  136. </p>
  137. ) : null}
  138. {showBranchLoading ? (
  139. <p className="text-xs text-muted-foreground">
  140. Niederlassungsliste wird geladen…
  141. </p>
  142. ) : null}
  143. </div>
  144. ) : (
  145. <div className="grid gap-2">
  146. <Label>Niederlassung</Label>
  147. <Input value="—" disabled />
  148. </div>
  149. )}
  150. </div>
  151. <div className="grid gap-2">
  152. <Label htmlFor="cu-password">Initiales Passwort</Label>
  153. <Input
  154. id="cu-password"
  155. type="password"
  156. value={form?.initialPassword ?? ""}
  157. onChange={(e) => setPatch({ initialPassword: e.target.value })}
  158. disabled={disabled}
  159. placeholder="••••••••"
  160. />
  161. <ul className="mt-1 list-disc pl-5 text-xs text-muted-foreground">
  162. {(Array.isArray(policyLines) ? policyLines : []).map((line) => (
  163. <li key={line}>{line}</li>
  164. ))}
  165. </ul>
  166. </div>
  167. <DialogFooter>
  168. <Button
  169. type="button"
  170. variant="outline"
  171. disabled={disabled}
  172. onClick={onCancel}
  173. >
  174. Abbrechen
  175. </Button>
  176. <Button type="submit" disabled={disabled || !canSubmit}>
  177. {isSubmitting ? (
  178. <>
  179. <Loader2 className="h-4 w-4 animate-spin" />
  180. Speichern…
  181. </>
  182. ) : (
  183. "Anlegen"
  184. )}
  185. </Button>
  186. </DialogFooter>
  187. </form>
  188. );
  189. }