Use callback to clear local storage on success (#462)
Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									803acd1fc4
								
							
						
					
					
						commit
						6c203a4476
					
				@ -484,6 +484,21 @@ export function Form ({
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }, [])
 | 
					  }, [])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  function clearLocalStorage (values) {
 | 
				
			||||||
 | 
					    Object.keys(values).forEach(v => {
 | 
				
			||||||
 | 
					      window.localStorage.removeItem(storageKeyPrefix + '-' + v)
 | 
				
			||||||
 | 
					      if (Array.isArray(values[v])) {
 | 
				
			||||||
 | 
					        values[v].forEach(
 | 
				
			||||||
 | 
					          (iv, i) => {
 | 
				
			||||||
 | 
					            Object.keys(iv).forEach(k => {
 | 
				
			||||||
 | 
					              window.localStorage.removeItem(`${storageKeyPrefix}-${v}[${i}].${k}`)
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					            window.localStorage.removeItem(`${storageKeyPrefix}-${v}[${i}]`)
 | 
				
			||||||
 | 
					          })
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // if `invoiceable` is set,
 | 
					  // if `invoiceable` is set,
 | 
				
			||||||
  // support for payment per invoice if they are lurking or don't have enough balance
 | 
					  // support for payment per invoice if they are lurking or don't have enough balance
 | 
				
			||||||
  // is added to submit handlers.
 | 
					  // is added to submit handlers.
 | 
				
			||||||
@ -491,7 +506,7 @@ export function Form ({
 | 
				
			|||||||
  // and use them as variables in their GraphQL mutation
 | 
					  // and use them as variables in their GraphQL mutation
 | 
				
			||||||
  if (invoiceable && onSubmit) {
 | 
					  if (invoiceable && onSubmit) {
 | 
				
			||||||
    const options = typeof invoiceable === 'object' ? invoiceable : undefined
 | 
					    const options = typeof invoiceable === 'object' ? invoiceable : undefined
 | 
				
			||||||
    onSubmit = useInvoiceable(onSubmit, options)
 | 
					    onSubmit = useInvoiceable(onSubmit, { callback: clearLocalStorage, ...options })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
@ -506,18 +521,7 @@ export function Form ({
 | 
				
			|||||||
          if (onSubmit) {
 | 
					          if (onSubmit) {
 | 
				
			||||||
            const options = await onSubmit(values, ...args)
 | 
					            const options = await onSubmit(values, ...args)
 | 
				
			||||||
            if (!storageKeyPrefix || options?.keepLocalStorage) return
 | 
					            if (!storageKeyPrefix || options?.keepLocalStorage) return
 | 
				
			||||||
            Object.keys(values).forEach(v => {
 | 
					            clearLocalStorage(values)
 | 
				
			||||||
              window.localStorage.removeItem(storageKeyPrefix + '-' + v)
 | 
					 | 
				
			||||||
              if (Array.isArray(values[v])) {
 | 
					 | 
				
			||||||
                values[v].forEach(
 | 
					 | 
				
			||||||
                  (iv, i) => {
 | 
					 | 
				
			||||||
                    Object.keys(iv).forEach(k => {
 | 
					 | 
				
			||||||
                      window.localStorage.removeItem(`${storageKeyPrefix}-${v}[${i}].${k}`)
 | 
					 | 
				
			||||||
                    })
 | 
					 | 
				
			||||||
                    window.localStorage.removeItem(`${storageKeyPrefix}-${v}[${i}]`)
 | 
					 | 
				
			||||||
                  })
 | 
					 | 
				
			||||||
              }
 | 
					 | 
				
			||||||
            })
 | 
					 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        } catch (err) {
 | 
					        } catch (err) {
 | 
				
			||||||
          console.log(err)
 | 
					          console.log(err)
 | 
				
			||||||
 | 
				
			|||||||
@ -131,6 +131,7 @@ const MutationInvoice = ({ id, hash, hmac, errorCount, repeat, onClose, expiresA
 | 
				
			|||||||
const defaultOptions = {
 | 
					const defaultOptions = {
 | 
				
			||||||
  forceInvoice: false,
 | 
					  forceInvoice: false,
 | 
				
			||||||
  requireSession: false,
 | 
					  requireSession: false,
 | 
				
			||||||
 | 
					  callback: null // (formValues) => void
 | 
				
			||||||
  replaceModal: false
 | 
					  replaceModal: false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
export const useInvoiceable = (onSubmit, options = defaultOptions) => {
 | 
					export const useInvoiceable = (onSubmit, options = defaultOptions) => {
 | 
				
			||||||
@ -156,7 +157,10 @@ export const useInvoiceable = (onSubmit, options = defaultOptions) => {
 | 
				
			|||||||
        const repeat = () =>
 | 
					        const repeat = () =>
 | 
				
			||||||
          // call onSubmit handler and pass invoice data
 | 
					          // call onSubmit handler and pass invoice data
 | 
				
			||||||
          onSubmit({ satsReceived, hash, hmac, ...formValues }, ...submitArgs)
 | 
					          onSubmit({ satsReceived, hash, hmac, ...formValues }, ...submitArgs)
 | 
				
			||||||
            .then(onClose)
 | 
					            .then(() => {
 | 
				
			||||||
 | 
					              onClose()
 | 
				
			||||||
 | 
					              options?.callback?.(formValues)
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
            .catch((error) => {
 | 
					            .catch((error) => {
 | 
				
			||||||
              // if error happened after payment, show repeat and cancel options
 | 
					              // if error happened after payment, show repeat and cancel options
 | 
				
			||||||
              // by passing `errorCount` and `repeat`
 | 
					              // by passing `errorCount` and `repeat`
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user