added role func, fixed some ui
This commit is contained in:
@@ -22,6 +22,7 @@ import { useCardReasons } from "@/features/cardReasons/hooks/useCardReasons";
|
||||
import { useBranches } from "@/features/branches/hooks/useBranches";
|
||||
import { useDistricts } from "@/features/districts/hooks/useDistricts";
|
||||
import { useInfiniteClients } from "@/features/clients/hooks/useClients";
|
||||
import { useWebSelfLock } from "@/features/shared/hooks/useWebSelfLock";
|
||||
import { PASSPORT_SERIES_OPTIONS, getStatusOptions } from "@/constants";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
@@ -107,6 +108,9 @@ interface StepContentProps {
|
||||
branchOptions: SelectOption[];
|
||||
districtOptions: SelectOption[];
|
||||
mode: "create" | "edit";
|
||||
userLocked: boolean;
|
||||
selfUserId: string;
|
||||
userName: string;
|
||||
}
|
||||
|
||||
// ─── Step panels ──────────────────────────────────────────────────────────────
|
||||
@@ -419,9 +423,18 @@ function StepPassport({ form, errors, set, t, mode }: StepContentProps) {
|
||||
);
|
||||
}
|
||||
|
||||
function StepContact({ form, errors, set, t, initialData }: StepContentProps & { initialData?: CardOrder }) {
|
||||
function StepContact({
|
||||
form,
|
||||
errors,
|
||||
set,
|
||||
t,
|
||||
initialData,
|
||||
userLocked,
|
||||
selfUserId,
|
||||
userName,
|
||||
}: StepContentProps & { initialData?: CardOrder }) {
|
||||
const [userSearch, setUserSearch] = useState("");
|
||||
const usersQuery = useInfiniteClients(userSearch);
|
||||
const usersQuery = useInfiniteClients(userSearch, !userLocked);
|
||||
|
||||
const userOptions: InfiniteSelectOption[] = useMemo(() => {
|
||||
const opts: InfiniteSelectOption[] = (usersQuery.data?.pages ?? [])
|
||||
@@ -429,7 +442,12 @@ function StepContact({ form, errors, set, t, initialData }: StepContentProps & {
|
||||
.map((u) => ({
|
||||
value: String(u.id),
|
||||
label: u.name || `${u.firstName || ""} ${u.lastName || ""}`.trim() || u.username || String(u.id),
|
||||
}));
|
||||
}))
|
||||
.filter((o) => !userLocked || o.value === selfUserId);
|
||||
|
||||
if (selfUserId && !opts.some((o) => o.value === selfUserId)) {
|
||||
opts.unshift({ value: selfUserId, label: userName });
|
||||
}
|
||||
|
||||
if (initialData?.user?.id && !opts.some((o) => o.value === String(initialData.user!.id))) {
|
||||
opts.unshift({
|
||||
@@ -440,7 +458,7 @@ function StepContact({ form, errors, set, t, initialData }: StepContentProps & {
|
||||
});
|
||||
}
|
||||
return opts;
|
||||
}, [usersQuery.data, initialData]);
|
||||
}, [usersQuery.data, initialData, userLocked, selfUserId, userName]);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
@@ -475,6 +493,7 @@ function StepContact({ form, errors, set, t, initialData }: StepContentProps & {
|
||||
options={userOptions}
|
||||
placeholder={t("loanOrderForm.placeholders.searchableSelect") || "Saýlamak üçin basyň"}
|
||||
error={errors.userId}
|
||||
disabled={userLocked}
|
||||
loading={usersQuery.isLoading}
|
||||
isLoadingMore={usersQuery.isFetchingNextPage}
|
||||
hasNextPage={!!usersQuery.hasNextPage}
|
||||
@@ -496,6 +515,8 @@ export function CardOrderForm({ mode, initialData }: CardOrderFormProps) {
|
||||
|
||||
const lang = (i18n.language === "ru" ? "ru" : i18n.language === "en" ? "en" : "tm") as "tm" | "ru" | "en";
|
||||
|
||||
const { locked: userLocked, userId: selfUserId, userName } = useWebSelfLock(true);
|
||||
|
||||
const createMutation = useCreateCard();
|
||||
const updateMutation = useUpdateCard(initialData?.id ?? 0);
|
||||
const isPending = createMutation.isPending || updateMutation.isPending;
|
||||
@@ -549,7 +570,9 @@ export function CardOrderForm({ mode, initialData }: CardOrderFormProps) {
|
||||
clearErrors,
|
||||
setError,
|
||||
} = useForm<OrderNewCardFormData>({
|
||||
defaultValues: initialData ? { ...DEFAULT_FORM_VALUES, ...mapInitial(initialData) } : DEFAULT_FORM_VALUES,
|
||||
defaultValues: initialData
|
||||
? { ...DEFAULT_FORM_VALUES, ...mapInitial(initialData), ...(userLocked ? { userId: selfUserId } : {}) }
|
||||
: { ...DEFAULT_FORM_VALUES, userId: selfUserId },
|
||||
});
|
||||
|
||||
const form = watch();
|
||||
@@ -593,8 +616,21 @@ export function CardOrderForm({ mode, initialData }: CardOrderFormProps) {
|
||||
}, [rhfErrors, allSubmittedErrors]);
|
||||
|
||||
const stepProps = useMemo(
|
||||
() => ({ form, errors, set, t, cardTypeOptions, cardStateOptions, branchOptions, districtOptions, mode }),
|
||||
[form, errors, set, t, cardTypeOptions, cardStateOptions, branchOptions, districtOptions, mode],
|
||||
() => ({
|
||||
form,
|
||||
errors,
|
||||
set,
|
||||
t,
|
||||
cardTypeOptions,
|
||||
cardStateOptions,
|
||||
branchOptions,
|
||||
districtOptions,
|
||||
mode,
|
||||
userLocked,
|
||||
selfUserId,
|
||||
userName,
|
||||
}),
|
||||
[form, errors, set, t, cardTypeOptions, cardStateOptions, branchOptions, districtOptions, mode, userLocked, selfUserId, userName],
|
||||
);
|
||||
|
||||
// ── Navigation ──────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user