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

Utils (@vppos/core/utils)

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

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.