userManagementUx.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const BRANCH_ID_RE = /^NL\d+$/;
  2. const BRANCH_ID_CAPTURE_RE = /^NL(\d+)$/i;
  3. function normalizeComparableText(value) {
  4. return String(value ?? "")
  5. .trim()
  6. .toLowerCase();
  7. }
  8. export function normalizeUsernameForConfirmation(value) {
  9. return normalizeComparableText(value);
  10. }
  11. export function isUsernameConfirmationMatch({
  12. expectedUsername,
  13. typedUsername,
  14. }) {
  15. const expected = normalizeUsernameForConfirmation(expectedUsername);
  16. const typed = normalizeUsernameForConfirmation(typedUsername);
  17. return Boolean(expected) && expected === typed;
  18. }
  19. export function normalizeBranchIdInput(value) {
  20. return String(value ?? "")
  21. .trim()
  22. .toUpperCase();
  23. }
  24. export function normalizeBranchNumberInput(value) {
  25. const raw = String(value ?? "").trim();
  26. if (!raw) return "";
  27. const digits = raw.replace(/\D+/g, "");
  28. if (!digits) return "";
  29. // Keep "0" as a valid numeric value but collapse leading zeros.
  30. return digits.replace(/^0+(?=\d)/, "");
  31. }
  32. export function formatBranchIdFromNumberInput(value) {
  33. const numberPart = normalizeBranchNumberInput(value);
  34. if (!numberPart) return "";
  35. return `NL${numberPart.padStart(2, "0")}`;
  36. }
  37. export function extractBranchNumberInputFromBranchId(branchId) {
  38. const match = BRANCH_ID_CAPTURE_RE.exec(normalizeBranchIdInput(branchId));
  39. if (!match) return "";
  40. return match[1];
  41. }
  42. export function isValidBranchIdFormat(value) {
  43. return BRANCH_ID_RE.test(normalizeBranchIdInput(value));
  44. }
  45. /**
  46. * Decides branch existence UX state for branch-role create/edit forms.
  47. *
  48. * Rules:
  49. * - Only relevant when role is "branch" and a branchId was entered.
  50. * - If branch list is available (status=ready), unknown branchIds block submit.
  51. * - If branch list failed to load (status=error), do not block submit (fail-open).
  52. */
  53. export function evaluateBranchExistence({
  54. role,
  55. branchId,
  56. branchesStatus,
  57. availableBranchIds,
  58. }) {
  59. const isBranchRole = String(role ?? "").trim() === "branch";
  60. const normalizedBranchId = normalizeBranchIdInput(branchId);
  61. const hasBranchId = Boolean(normalizedBranchId);
  62. const listReady =
  63. branchesStatus === "ready" && Array.isArray(availableBranchIds);
  64. const listError = branchesStatus === "error";
  65. const normalizedAvailable = listReady
  66. ? availableBranchIds.map(normalizeBranchIdInput).filter(Boolean)
  67. : [];
  68. const branchExists =
  69. isBranchRole && hasBranchId && listReady
  70. ? normalizedAvailable.includes(normalizedBranchId)
  71. : null;
  72. const hasUnknownBranch =
  73. isBranchRole && hasBranchId && listReady && branchExists === false;
  74. return {
  75. isBranchRole,
  76. normalizedBranchId,
  77. hasBranchId,
  78. listReady,
  79. listError,
  80. branchExists,
  81. hasUnknownBranch,
  82. shouldBlockSubmit: Boolean(hasUnknownBranch),
  83. };
  84. }