import MoralisType from "moralis" ; import { useCallback , useState } from "react" ; import { useMoralis } from "../useMoralis" ; export interface MoralisObjectSaveOptions { cascadeSave ? : boolean ; context ? : unknown ; onError ? : ( error : Error ) => void ; onSuccess ? : ( result : MoralisType . Object ) => void ; onComplete ? : ( ) => void ; throwOnError ? : boolean ; } export type MoralisObjectSaveData = Record < string , unknown > ; /**
 * A hook for making/saving new Moralis Objects
 */ export const useNewMoralisObject = ( objectClassName ) => { const { Moralis } = useMoralis ( ) ; const [ isSaving , setIsSaving ] = useState ( false ) ; const [ error , setError ] = useState < Error | null > ( null ) ; const [ object , setObject ] = useState < MoralisType . Object | null > ( null ) ; /**
     * Save function to save data to the object
     */ const save = useCallback ( async ( data = { } , { cascadeSave , throwOnError , context , onSuccess , onComplete , onError , } = { } ) => { setIsSaving ( true ) ; setError ( null ) ; try { const Object = Moralis . Object . extend ( objectClassName ) ; const object = new Object ( ) ; await object . save ( data , { cascadeSave , context } ) ; setObject ( object ) ; if ( onSuccess ) { onSuccess ( object ) ; } return object ; } catch ( error ) { setError ( error ) ; if ( throwOnError ) { throw error ; } if ( onError ) { onError ( error ) ; } } finally { setIsSaving ( false ) ; if ( onComplete ) { onComplete ( ) ; } } } , [ objectClassName ] ) ; return { isSaving , object , error , save , } ; } ;