editar, modificar, ver y crear productos listo
This commit is contained in:
@@ -4,7 +4,8 @@ import { url } from 'inspector';
|
||||
import { z } from 'zod';
|
||||
|
||||
export type InventoryTable = z.infer<typeof seeProduct>;
|
||||
export type EditInventory = z.infer<typeof editInventory>;
|
||||
export type EditInventory = z.infer<typeof updateInventory>;
|
||||
export type CreateInventory = z.infer<typeof createProduct>;
|
||||
export type ProductApiResponseSchema = z.infer<typeof productApiResponseSchema>;
|
||||
export type allProducts = z.infer<typeof productDetails>;
|
||||
|
||||
@@ -35,19 +36,58 @@ export const productDetails = seeProduct.extend({
|
||||
email: z.string().email().nullable()
|
||||
})
|
||||
|
||||
export const editInventory = z.object({
|
||||
const validateProduct = z.object({
|
||||
id: z.number().optional(),
|
||||
title: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }),
|
||||
description: z.string().min(10, { message: "Debe de tener 10 o más caracteres" }),
|
||||
stock: z.number(),
|
||||
address: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }),
|
||||
price: z.string(),
|
||||
price: z.string().min(1, { message: "Debe de tener 1 o más caracteres" }),
|
||||
urlImg: z.custom<FileList | undefined>(),
|
||||
status: z.string().min(1, { message: "Debe de seleccionar un valor" }),
|
||||
userId: z.number().optional(),
|
||||
})
|
||||
|
||||
export const updateInventory = validateProduct.extend({
|
||||
urlImg: z.custom<FileList | undefined>()
|
||||
.refine((files) => (files && files.length <= 10) || files === undefined, "Máximo 10 imágenes")
|
||||
.refine((files) =>
|
||||
// (files && Array.from(files).every(file => file.size <= MAX_FILE_SIZE)) || files === undefined
|
||||
{
|
||||
if (files) {
|
||||
let size = 0;
|
||||
Array.from(files).map(file => {
|
||||
size += file.size;
|
||||
})
|
||||
if (size <= MAX_FILE_SIZE) return true;
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
,
|
||||
`El tamaño máximo entre toda las imagenes es de 5MB`
|
||||
).refine((files) =>
|
||||
(files && Array.from(files).every(file => ACCEPTED_IMAGE_TYPES.includes(file.type))) || files === undefined,
|
||||
"Solo se aceptan archivos .jpg, .jpeg, .png y .webp"
|
||||
).refine((files) =>
|
||||
(files && Array.from(files).every(file => file.name.length <= MAX_FILENAME_LENGTH)) || files === undefined,
|
||||
`El nombre de cada archivo no puede superar los ${MAX_FILENAME_LENGTH} caracteres`
|
||||
),
|
||||
})
|
||||
|
||||
export const createProduct = validateProduct.extend({
|
||||
urlImg: z.custom<FileList | undefined>()
|
||||
.refine((files) => files && files.length > 0, "Se requiere al menos una imagen")
|
||||
.refine((files) => files && files.length <= 10, "Máximo 10 imágenes")
|
||||
.refine((files) =>
|
||||
files && Array.from(files).every(file => file.size <= MAX_FILE_SIZE),
|
||||
`El tamaño máximo de cada imagen es de 5MB`
|
||||
.refine((files) => {
|
||||
let size = 0;
|
||||
if (files) Array.from(files).map(file => {
|
||||
size += file.size;
|
||||
})
|
||||
if (size <= MAX_FILE_SIZE) return true;
|
||||
return false
|
||||
},
|
||||
`El tamaño máximo entre toda las imagenes es de 5MB`
|
||||
).refine((files) =>
|
||||
files && Array.from(files).every(file => ACCEPTED_IMAGE_TYPES.includes(file.type)),
|
||||
"Solo se aceptan archivos .jpg, .jpeg, .png y .webp"
|
||||
@@ -55,13 +95,11 @@ export const editInventory = z.object({
|
||||
files && Array.from(files).every(file => file.name.length <= MAX_FILENAME_LENGTH),
|
||||
`El nombre de cada archivo no puede superar los ${MAX_FILENAME_LENGTH} caracteres`
|
||||
),
|
||||
status: z.string().min(1, { message: "Debe de seleccionar un valor" }),
|
||||
userId: z.number().optional(),
|
||||
})
|
||||
|
||||
export const ApiResponseSchema = z.object({
|
||||
message: z.string(),
|
||||
data: z.array(product),
|
||||
data: z.array(seeProduct),
|
||||
meta: z.object({
|
||||
page: z.number(),
|
||||
limit: z.number(),
|
||||
@@ -89,11 +127,6 @@ export const productApiResponseSchema = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
export const test = z.object({
|
||||
// message: z.string(),
|
||||
data: z.array(z.string()),
|
||||
})
|
||||
|
||||
export const productMutate = z.object({
|
||||
message: z.string(),
|
||||
data: seeProduct,
|
||||
@@ -102,4 +135,8 @@ export const productMutate = z.object({
|
||||
export const getProduct = z.object({
|
||||
message: z.string(),
|
||||
data: productDetails,
|
||||
})
|
||||
|
||||
export const deleteProduct = z.object({
|
||||
message: z.string(),
|
||||
})
|
||||
Reference in New Issue
Block a user