added role func, fixed some ui
This commit is contained in:
1 parent
cc5b5fca24
commit
7445e09d5f
22 files changed
+1074
-622
No files matched your search
@@ -10,6 +10,7 @@ import { useCreateCardBalance, useUpdateCardBalance } from "@/features/cardBalan
|
||||
import { cardBalanceFormSchema, DEFAULT_FORM_VALUES, mapInitial, buildPayload } from "@/features/cardBalance/schemas/cardBalance.schema";
|
||||
import type { CardBalanceFormData } from "@/features/cardBalance/schemas/cardBalance.schema";
|
||||
import { useInfiniteUsers } from "@/features/allUsers/hooks/useAllUsers";
|
||||
import { useWebSelfLock } from "@/features/shared/hooks/useWebSelfLock";
|
||||
import { MONTH_OPTIONS, CARD_YEAR_OPTIONS } from "@/constants";
|
||||
|
||||
interface CardBalanceFormProps {
|
||||
@@ -54,12 +55,17 @@ export function CardBalanceForm({ mode, initialData }: CardBalanceFormProps) {
|
||||
const isPending = createMutation.isPending || updateMutation.isPending;
|
||||
|
||||
const [userSearch, setUserSearch] = useState("");
|
||||
const userQuery = useInfiniteUsers(userSearch);
|
||||
const { locked: userLocked, userId: selfUserId, userName } = useWebSelfLock(true);
|
||||
const userQuery = useInfiniteUsers(userSearch, !userLocked);
|
||||
|
||||
const userOptions: InfiniteSelectOption[] = useMemo(() => {
|
||||
const opts: InfiniteSelectOption[] = (userQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p?.users ?? [])
|
||||
.map((u) => ({ value: String(u.id), label: u.name || u.username || String(u.id) }));
|
||||
.map((u) => ({ value: String(u.id), label: u.name || 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?.createdBy?.id && !opts.some((o) => o.value === String(initialData.createdBy.id))) {
|
||||
opts.unshift({
|
||||
value: String(initialData.createdBy.id),
|
||||
@@ -67,7 +73,7 @@ export function CardBalanceForm({ mode, initialData }: CardBalanceFormProps) {
|
||||
});
|
||||
}
|
||||
return opts;
|
||||
}, [userQuery, initialData]);
|
||||
}, [userQuery, initialData, userLocked, selfUserId, userName]);
|
||||
|
||||
const {
|
||||
watch,
|
||||
@@ -77,7 +83,9 @@ export function CardBalanceForm({ mode, initialData }: CardBalanceFormProps) {
|
||||
clearErrors,
|
||||
setError,
|
||||
} = useForm<CardBalanceFormData>({
|
||||
defaultValues: initialData ? mapInitial(initialData) : DEFAULT_FORM_VALUES,
|
||||
defaultValues: initialData
|
||||
? { ...mapInitial(initialData), ...(userLocked ? { createdBy: selfUserId } : {}) }
|
||||
: { ...DEFAULT_FORM_VALUES, createdBy: selfUserId },
|
||||
});
|
||||
|
||||
const form = watch();
|
||||
@@ -214,6 +222,7 @@ export function CardBalanceForm({ mode, initialData }: CardBalanceFormProps) {
|
||||
onChange={(v) => set("createdBy", v)}
|
||||
options={userOptions}
|
||||
placeholder={t("Select user", "Ulanyjyny saýlaň")}
|
||||
disabled={userLocked}
|
||||
loading={userQuery.isLoading}
|
||||
isLoadingMore={userQuery.isFetchingNextPage}
|
||||
hasNextPage={!!userQuery.hasNextPage}
|
||||
|
||||
Reference in new issue
Block a user