@@ -7,30 +7,25 @@ import { useProductsStorage } from './products.storage';
77import productService from '../infrastructure/product.service' ;
88// Core
99import { useAppStorage } from '@modules/core/application/app.storage' ;
10- import { useConnectivityStore } from '@modules/core/application/connectivity.storage' ;
10+ import { getIsConnected } from '@modules/core/application/connectivity.storage' ;
1111// Config
1212import { QUERY_KEYS } from '@config/query.keys' ;
1313
1414export function useProductCreate ( ) {
1515 const queryClient = useQueryClient ( ) ;
16- // Storage
17- const { addProduct } = useProductsStorage ( ) ;
16+ // Storage (read state directly to avoid calling hooks outside React)
17+ const addProduct = useProductsStorage . getState ( ) . addProduct ;
1818 const { show } = useAppStorage ( s => s . toast ) ;
19- const { isConnected } = useConnectivityStore ( ) ;
2019
2120 return useMutation ( {
2221 mutationFn : async ( form : ProductFormData ) => {
23- const payload = productFormToPayloadAdapter ( form ) ;
24-
25- if ( ! isConnected ) {
26- show ( {
27- message : 'Sin conexión a internet.' ,
28- type : 'info' ,
29- } ) ;
30-
31- return ;
22+ const connected = getIsConnected ( ) ;
23+ if ( ! connected ) {
24+ throw new Error ( 'No internet connection' ) ;
3225 }
3326
27+ const payload = productFormToPayloadAdapter ( form ) ;
28+
3429 // Si hay conexión, crear en el servidor
3530 const result = await productService . create ( payload ) ;
3631 if ( result instanceof Error ) {
@@ -61,21 +56,17 @@ export function useProductUpdate() {
6156 const queryClient = useQueryClient ( ) ;
6257 // Storage
6358 const { show } = useAppStorage ( s => s . toast ) ;
64- const { isConnected } = useConnectivityStore ( ) ;
65- const { updateProduct } = useProductsStorage ( ) ;
59+ const updateProduct = useProductsStorage . getState ( ) . updateProduct ;
6660
6761 return useMutation ( {
6862 mutationFn : async ( { id, form } : { id : string ; form : ProductFormData } ) => {
69- const payload = productFormToPayloadAdapter ( form ) ;
70-
71- if ( ! isConnected ) {
72- show ( {
73- message : 'Sin conexión a internet.' ,
74- type : 'info' ,
75- } ) ;
76- return ;
63+ const connected = getIsConnected ( ) ;
64+ if ( ! connected ) {
65+ throw new Error ( 'No internet connection' ) ;
7766 }
7867
68+ const payload = productFormToPayloadAdapter ( form ) ;
69+
7970 // Si hay conexión, actualizar en el servidor
8071 const result = await productService . update ( id , payload ) ;
8172 if ( result instanceof Error ) {
@@ -109,17 +100,13 @@ export function useProductDelete() {
109100 const queryClient = useQueryClient ( ) ;
110101 // Storage
111102 const { show } = useAppStorage ( s => s . toast ) ;
112- const { isConnected } = useConnectivityStore ( ) ;
113- const { deleteProduct } = useProductsStorage ( ) ;
103+ const deleteProduct = useProductsStorage . getState ( ) . deleteProduct ;
114104
115105 return useMutation ( {
116106 mutationFn : async ( id : string ) => {
117- if ( ! isConnected ) {
118- show ( {
119- message : 'Sin conexión a internet.' ,
120- type : 'info' ,
121- } ) ;
122- return ;
107+ const connected = getIsConnected ( ) ;
108+ if ( ! connected ) {
109+ throw new Error ( 'No internet connection' ) ;
123110 }
124111
125112 // Si hay conexión, eliminar del servidor
0 commit comments