@@ -8,11 +8,64 @@ import {
88import { useState } from 'react' ;
99import { themeByProject } from '../utils/theme' ;
1010import { usePage } from '@inertiajs/react' ;
11+ import axios from 'axios' ;
12+ import EditarProductForm from './EditProductModal' ;
13+ import AgentModalWrapper from '../agentsModalWrapper' ;
1114
1215export default function TablaProductosBL ( { productos, openModal, search } ) {
1316 const { props } = usePage ( ) ;
1417 const proyecto = props ?. auth ?. user ?. proyecto || 'AZZU' ;
1518 const theme = themeByProject [ proyecto ] ;
19+ const [ productDetail , setProductDetail ] = useState ( null ) ;
20+ const [ colores , setColores ] = useState ( null ) ;
21+ const [ modalOpenProductEdit , setModalOpenProductEdit ] = useState ( false ) ;
22+ const openModalEditProduct = async ( id ) => {
23+ try {
24+ const response = await axios . get ( `BLProductShow/${ id } ` ) ;
25+ setProductDetail ( response . data . productDetails ) ;
26+ setColores ( response . data . colores ) ;
27+ setModalOpenProductEdit ( true ) ;
28+ } catch ( error ) {
29+ console . error ( "Error al cargar el producto" , error ) ;
30+ }
31+ }
32+ const closeModal = ( ) => {
33+ setModalOpenProductEdit ( false ) ;
34+ } ;
35+ const handleEditarProducto = ( clientData ) => {
36+ console . log ( 'datos producto' , clientData ) ;
37+ router . post ( route ( `clientesBL.update` , clientData . id ) , clientData , {
38+ preserveState : true ,
39+ onSuccess : ( ) => {
40+ setToast ( {
41+ show : true ,
42+ success : true ,
43+ message : "cliente editado correctamente"
44+ } ) ;
45+ // Refrescar la lista de clientes
46+ // router.visit(route('clientes.index'));
47+ } ,
48+ onError : ( errors ) => {
49+ const primerError = Object . values ( errors ) [ 0 ] ;
50+ setToast ( {
51+ show : true ,
52+ success : false ,
53+ message : primerError || "Error al editar el cliente"
54+ } ) ;
55+ } ,
56+ onFinish : ( visit ) => {
57+ // Si hubo error de servidor (status 500 o más)
58+ if ( visit . response ?. status >= 500 ) {
59+ const msg = visit . response ?. data ?. message || "Error interno del servidor" ;
60+ setToast ( {
61+ show : true ,
62+ success : false ,
63+ message : msg
64+ } ) ;
65+ }
66+ }
67+ } ) ;
68+ } ;
1669
1770 const columns = [
1871 { accessorKey : 'tipo_producto' , header : 'Tipo de producto' } ,
@@ -26,7 +79,7 @@ export default function TablaProductosBL({ productos, openModal, search }) {
2679 cell : ( { row } ) => (
2780 < button
2881 className = { `${ theme . text } font-medium hover:underline cursor-pointer` }
29- onClick = { ( ) => openModal ( row . original . id ) }
82+ onClick = { ( ) => openModalEditProduct ( row . original . id ) }
3083 >
3184 Editar
3285 </ button >
@@ -54,44 +107,52 @@ export default function TablaProductosBL({ productos, openModal, search }) {
54107 } ) ;
55108
56109 return (
57- < table className = "min-w-full text-sm text-left text-gray-500 dark:text-gray-400" >
58- < thead className = "text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-800 dark:text-gray-400" >
59- { table . getHeaderGroups ( ) . map ( headerGroup => (
60- < tr key = { headerGroup . id } >
61- { headerGroup . headers . map ( header => (
62- < th
63- key = { header . id }
64- className = "px-6 py-3 cursor-pointer select-none"
65- onClick = { header . column . getToggleSortingHandler ( ) }
66- >
67- { flexRender ( header . column . columnDef . header , header . getContext ( ) ) }
68- { {
69- asc : ' 🔼' ,
70- desc : ' 🔽' ,
71- } [ header . column . getIsSorted ( ) ] ?? null }
72- </ th >
73- ) ) }
74- </ tr >
75- ) ) }
76- </ thead >
77- < tbody >
78- { table . getRowModel ( ) . rows . map ( ( row , index ) => (
79- < tr
80- key = { row . id }
81- className = { `${
82- index % 2 === 0
83- ? 'bg-white dark:bg-gray-800'
84- : 'bg-gray-50 dark:bg-gray-700'
85- } hover:bg-gray-100 dark:hover:bg-gray-600 transition-colors`}
86- >
87- { row . getVisibleCells ( ) . map ( cell => (
88- < td key = { cell . id } className = "px-6 py-4 whitespace-nowrap" >
89- { flexRender ( cell . column . columnDef . cell , cell . getContext ( ) ) }
90- </ td >
91- ) ) }
92- </ tr >
93- ) ) }
94- </ tbody >
95- </ table >
110+ < >
111+ < table className = "min-w-full text-sm text-left text-gray-500 dark:text-gray-400" >
112+ < thead className = "text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-800 dark:text-gray-400" >
113+ { table . getHeaderGroups ( ) . map ( headerGroup => (
114+ < tr key = { headerGroup . id } >
115+ { headerGroup . headers . map ( header => (
116+ < th
117+ key = { header . id }
118+ className = "px-6 py-3 cursor-pointer select-none"
119+ onClick = { header . column . getToggleSortingHandler ( ) }
120+ >
121+ { flexRender ( header . column . columnDef . header , header . getContext ( ) ) }
122+ { {
123+ asc : ' 🔼' ,
124+ desc : ' 🔽' ,
125+ } [ header . column . getIsSorted ( ) ] ?? null }
126+ </ th >
127+ ) ) }
128+ </ tr >
129+ ) ) }
130+ </ thead >
131+ < tbody >
132+ { table . getRowModel ( ) . rows . map ( ( row , index ) => (
133+ < tr
134+ key = { row . id }
135+ className = { `${
136+ index % 2 === 0
137+ ? 'bg-white dark:bg-gray-800'
138+ : 'bg-gray-50 dark:bg-gray-700'
139+ } hover:bg-gray-100 dark:hover:bg-gray-600 transition-colors`}
140+ >
141+ { row . getVisibleCells ( ) . map ( cell => (
142+ < td key = { cell . id } className = "px-6 py-4 whitespace-nowrap" >
143+ { flexRender ( cell . column . columnDef . cell , cell . getContext ( ) ) }
144+ </ td >
145+ ) ) }
146+ </ tr >
147+ ) ) }
148+ </ tbody >
149+ </ table >
150+ { modalOpenProductEdit && (
151+ < AgentModalWrapper closeModal = { closeModal } >
152+ < EditarProductForm onClose = { closeModal } productDetail = { productDetail } onSave = { handleEditarProducto } colores = { colores } />
153+ </ AgentModalWrapper >
154+ )
155+ }
156+ </ >
96157 ) ;
97158}
0 commit comments