import { useReducer } from "react" ; import { Model } from "../../Model" ; import { ActionType } from "../../utils/ActionsTypes" ; import { InitialState , reducer } from "../ReducerUtils" ; export const useSave = ( model ) => { const [ state , dispatch ] = useReducer ( reducer , InitialState ) ; const save = async ( input ) => { if ( state . loading ) { return ; } dispatch ( { type : ActionType . INITIATE_REQUEST } ) ; try { const result = await model . save ( input ) ; dispatch ( { type : ActionType . REQUEST_COMPLETE , data : result } ) ; return result ; } catch ( error ) { dispatch ( { type : ActionType . REQUEST_COMPLETE , error } ) ; } } ; return { ... state , save } ; } ;