Chuyển tới nội dung chính

Utils (@vppos/core/utils)

import { cn, toastUtils, getApiErrorMessage, formatDate } from "@vppos/core/utils";

Role Utilities​

role.utils.ts​

Các helper function để kiểm tra vai trò người dùng hiện tại (Single Source of Truth). Luôn import từ @vppos/core/utils.

import { isSuperAdmin, isAdmin, hasAnyRole } from "@vppos/core/utils";

// true nếu userRoles có chứa "superadmin"
isSuperAdmin(userRoles);

// true nếu userRoles có chứa "admin"
isAdmin(userRoles);

// true nếu userRoles có chứa ít nhất 1 role trong requiredRoles
hasAnyRole(userRoles, ["admin", "superadmin"]);

Styling​

cn(...classes)​

Merge Tailwind classes (dùng clsx + tailwind-merge):

cn("px-4 py-2", isActive && "bg-primary-500 text-white");

Toast​

toastUtils​

Toast notifications. Không import toast từ react-toastify.

toastUtils.success("Tạo sản phẩm thành công");
toastUtils.error("Có lỗi xảy ra");
toastUtils.warning("Vui lòng kiểm tra lại");

Error Handling​

getApiErrorMessage(error)​

Lấy message lỗi từ RTK Query error:

try {
await createProduct(data).unwrap();
} catch (err) {
toastUtils.error(getApiErrorMessage(err));
}

getApiError(error)​

Lấy full error object (bao gồm fieldErrors cho form):

const apiError = getApiError(err);
if (apiError?.fieldErrors) {
setFormErrors(setError, apiError.fieldErrors);
}

setFormErrors(setError, fieldErrors)​

Map server validation errors vào React Hook Form:

setFormErrors(setError, fieldErrors);
// Tự động set error cho từng field theo tên

Date/Time​

formatDate(date, format?)​

Format date dùng dayjs.

getCurrentYear()​

Lấy năm hiện tại.

cảnh báo

Luôn dùng dayjs() thay cho new Date().

Number/Currency​

Các helper format số, tiền VND.