|
|
@@ -1,14 +1,15 @@
|
|
|
-// ---------------------------------------------------------------------------
|
|
|
-// Ordner: components/admin/users/edit-user
|
|
|
-// Datei: useEditUserDialog.js
|
|
|
-// Relativer Pfad: components/admin/users/edit-user/useEditUserDialog.js
|
|
|
-// ---------------------------------------------------------------------------
|
|
|
"use client";
|
|
|
|
|
|
import React from "react";
|
|
|
|
|
|
import { adminUpdateUser, ApiClientError } from "@/lib/frontend/apiClient";
|
|
|
import { buildLoginUrl, LOGIN_REASONS } from "@/lib/frontend/authRedirect";
|
|
|
+import { useSearchBranches } from "@/lib/frontend/search/useSearchBranches";
|
|
|
+import {
|
|
|
+ evaluateBranchExistence,
|
|
|
+ isValidBranchIdFormat,
|
|
|
+ normalizeBranchIdInput,
|
|
|
+} from "@/lib/frontend/admin/users/userManagementUx";
|
|
|
import {
|
|
|
notifySuccess,
|
|
|
notifyError,
|
|
|
@@ -16,11 +17,9 @@ import {
|
|
|
notifyInfo,
|
|
|
} from "@/lib/frontend/ui/toast";
|
|
|
|
|
|
-import { normalizeBranchIdDraft } from "@/components/admin/users/usersUi";
|
|
|
import {
|
|
|
EDIT_ROLE_OPTIONS,
|
|
|
EMAIL_RE,
|
|
|
- BRANCH_RE,
|
|
|
normalizeUsername,
|
|
|
normalizeEmail,
|
|
|
} from "@/components/admin/users/edit-user/editUserUtils";
|
|
|
@@ -47,7 +46,7 @@ function validateClient(form) {
|
|
|
const isKnownRole = EDIT_ROLE_OPTIONS.some((x) => x.value === role);
|
|
|
|
|
|
const branchId =
|
|
|
- role === "branch" ? normalizeBranchIdDraft(form?.branchId || "") : "";
|
|
|
+ role === "branch" ? normalizeBranchIdInput(form?.branchId || "") : "";
|
|
|
|
|
|
if (!username) return { title: "Benutzername fehlt.", description: null };
|
|
|
if (!email) return { title: "E-Mail fehlt.", description: null };
|
|
|
@@ -70,7 +69,7 @@ function validateClient(form) {
|
|
|
description: "Für Niederlassungs-User ist eine NL erforderlich.",
|
|
|
};
|
|
|
}
|
|
|
- if (!BRANCH_RE.test(branchId)) {
|
|
|
+ if (!isValidBranchIdFormat(branchId)) {
|
|
|
return {
|
|
|
title: "Niederlassung ist ungültig.",
|
|
|
description: "Format: NL01, NL02, ...",
|
|
|
@@ -113,7 +112,7 @@ function buildNormalizedForm(form) {
|
|
|
email: normalizeEmail(form?.email),
|
|
|
role,
|
|
|
branchId:
|
|
|
- role === "branch" ? normalizeBranchIdDraft(form?.branchId || "") : "",
|
|
|
+ role === "branch" ? normalizeBranchIdInput(form?.branchId || "") : "",
|
|
|
mustChangePassword: Boolean(form?.mustChangePassword),
|
|
|
};
|
|
|
}
|
|
|
@@ -163,7 +162,22 @@ export function useEditUserDialog({ user, disabled = false, onUpdated } = {}) {
|
|
|
const [form, setForm] = React.useState(() => buildInitialFormFromUser(user));
|
|
|
const [error, setError] = React.useState(null);
|
|
|
|
|
|
+ const role = String(form?.role || "branch").trim();
|
|
|
+ const shouldLoadBranches = open && role === "branch";
|
|
|
+ const { status: branchesStatus, branches: availableBranchIds } =
|
|
|
+ useSearchBranches({ enabled: shouldLoadBranches });
|
|
|
+
|
|
|
const effectiveDisabled = Boolean(disabled || isSubmitting || !user?.id);
|
|
|
+ const branchExistence = React.useMemo(
|
|
|
+ () =>
|
|
|
+ evaluateBranchExistence({
|
|
|
+ role,
|
|
|
+ branchId: form?.branchId,
|
|
|
+ branchesStatus,
|
|
|
+ availableBranchIds,
|
|
|
+ }),
|
|
|
+ [role, form?.branchId, branchesStatus, availableBranchIds],
|
|
|
+ );
|
|
|
|
|
|
const patchPreview = React.useMemo(() => {
|
|
|
return buildPatch({ user, form });
|
|
|
@@ -182,8 +196,12 @@ export function useEditUserDialog({ user, disabled = false, onUpdated } = {}) {
|
|
|
]);
|
|
|
|
|
|
const canSubmit = React.useMemo(() => {
|
|
|
- return !effectiveDisabled && Object.keys(patchPreview).length > 0;
|
|
|
- }, [effectiveDisabled, patchPreview]);
|
|
|
+ return (
|
|
|
+ !effectiveDisabled &&
|
|
|
+ Object.keys(patchPreview).length > 0 &&
|
|
|
+ !branchExistence.shouldBlockSubmit
|
|
|
+ );
|
|
|
+ }, [effectiveDisabled, patchPreview, branchExistence.shouldBlockSubmit]);
|
|
|
|
|
|
const setPatch = React.useCallback((patch) => {
|
|
|
setForm((prev) => ({ ...prev, ...(patch || {}) }));
|
|
|
@@ -238,6 +256,17 @@ export function useEditUserDialog({ user, disabled = false, onUpdated } = {}) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ if (branchExistence.shouldBlockSubmit) {
|
|
|
+ const mapped = {
|
|
|
+ title: "Niederlassung existiert nicht.",
|
|
|
+ description:
|
|
|
+ "Die gewählte Niederlassung ist nicht in der aktuellen Liste vorhanden.",
|
|
|
+ };
|
|
|
+ setError(mapped);
|
|
|
+ notifyError(mapped);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
const patch = buildPatch({ user, form });
|
|
|
|
|
|
if (Object.keys(patch).length === 0) {
|
|
|
@@ -342,6 +371,7 @@ export function useEditUserDialog({ user, disabled = false, onUpdated } = {}) {
|
|
|
onUpdated,
|
|
|
redirectToLoginExpired,
|
|
|
resetForm,
|
|
|
+ branchExistence.shouldBlockSubmit,
|
|
|
],
|
|
|
);
|
|
|
|
|
|
@@ -351,6 +381,8 @@ export function useEditUserDialog({ user, disabled = false, onUpdated } = {}) {
|
|
|
form,
|
|
|
setPatch,
|
|
|
error,
|
|
|
+ branchesStatus,
|
|
|
+ branchExistence,
|
|
|
isSubmitting,
|
|
|
effectiveDisabled,
|
|
|
canSubmit,
|