refactor updateRole function
This commit is contained in:
parent
248fdd0e41
commit
0a49f8dc7d
@ -1,7 +1,8 @@
|
||||
/* eslint-disable canonical/id-match */
|
||||
'use server';
|
||||
import { authOptions } from '@/config/auth';
|
||||
import { getCustomer, updateCustomerProfile } from '@repo/graphql/api';
|
||||
import { type CustomerInput, type Enum_Customer_Role } from '@repo/graphql/types';
|
||||
import { type CustomerInput, Enum_Customer_Role } from '@repo/graphql/types';
|
||||
import { getServerSession } from 'next-auth/next';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
|
||||
@ -36,21 +37,8 @@ export async function updateProfile(input: CustomerInput) {
|
||||
revalidatePath('/profile');
|
||||
}
|
||||
|
||||
export async function updateRole(role: Enum_Customer_Role) {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
if (session) {
|
||||
const { user } = session;
|
||||
const customer = await getCustomer({ telegramId: user?.telegramId });
|
||||
if (customer) {
|
||||
await updateCustomerProfile({
|
||||
data: {
|
||||
role,
|
||||
},
|
||||
documentId: customer.documentId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
revalidatePath('/profile');
|
||||
export async function updateRole(checked: boolean) {
|
||||
return updateProfile({
|
||||
role: checked ? Enum_Customer_Role.Master : Enum_Customer_Role.Client,
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,14 +1,12 @@
|
||||
/* eslint-disable canonical/id-match */
|
||||
/* eslint-disable promise/prefer-await-to-then */
|
||||
'use client';
|
||||
import { Enum_Customer_Role } from '@repo/graphql/types';
|
||||
import { Checkbox, type CheckboxProps } from '@repo/ui/components/ui/checkbox';
|
||||
import { useState } from 'react';
|
||||
import { useDebouncedCallback } from 'use-debounce';
|
||||
|
||||
type Props = Pick<CheckboxProps, 'checked'> & {
|
||||
readonly description?: string;
|
||||
readonly onChange?: (value: Enum_Customer_Role) => Promise<void> | void;
|
||||
readonly onChange?: (value: boolean) => Promise<void> | void;
|
||||
readonly text: string;
|
||||
};
|
||||
|
||||
@ -39,7 +37,7 @@ export function CheckboxWithText({ checked: initialValue, description, onChange,
|
||||
}
|
||||
|
||||
function useDebouncedOnChangeCallback(
|
||||
callback: ((value: Enum_Customer_Role) => Promise<void> | void) | undefined,
|
||||
callback: ((value: boolean) => Promise<void> | void) | undefined,
|
||||
) {
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
|
||||
@ -47,7 +45,7 @@ function useDebouncedOnChangeCallback(
|
||||
if (!callback) return;
|
||||
|
||||
setIsPending(true);
|
||||
const result = callback(checked ? Enum_Customer_Role.Master : Enum_Customer_Role.Client);
|
||||
const result = callback(checked);
|
||||
|
||||
if (result instanceof Promise) {
|
||||
result.finally(() => setIsPending(false));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user