-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* provide form instance for developer * remove manager hash * fix examples * up version * revert env file * remove console.log
- Loading branch information
1 parent
2e6aaf1
commit eba319a
Showing
8 changed files
with
167 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { | ||
VGSCollectForm, | ||
VGSCollectFormState, | ||
VGSCollectHttpStatusCode, | ||
VGSCollectVaultEnvironment, | ||
useVGSCollectResponse, | ||
useVGSCollectState, | ||
useVGSCollectFormInstance, | ||
IVGSCollectForm | ||
} from 'collect-js-react'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
import { loadVGSCollect } from '@vgs/collect-js'; | ||
|
||
const { TextField } = VGSCollectForm; | ||
|
||
const { REACT_APP_VAULT_ID, REACT_APP_ENVIRONMENT, REACT_APP_COLLECT_VERSION } = process.env; | ||
|
||
const SubmitHandling = (e: any) => { | ||
const [isVGSCollectScriptLoaded, setCollectScriptLoaded] = useState(false); | ||
const [formInstance, setFormInstance] = useState<IVGSCollectForm | null>(null); | ||
const [isFormSubmitting, setFormSubmitting] = useState(false); | ||
const VGSCollectFieldStyles = { | ||
'&::placeholder': { | ||
color: '#686868' | ||
}, | ||
padding: '.5rem 1rem', | ||
boxSizing: 'border-box' | ||
}; | ||
|
||
const [state] = useVGSCollectState(); | ||
const [response] = useVGSCollectResponse(); | ||
const [form] = useVGSCollectFormInstance(); | ||
|
||
useEffect(() => { | ||
/** | ||
* Track form state | ||
*/ | ||
}, [state]); | ||
|
||
useEffect(() => { | ||
/** | ||
* Track response from the VGS Collect form | ||
*/ | ||
}, [response]); | ||
|
||
useEffect(() => { | ||
setFormInstance(form); | ||
}, [form]); | ||
|
||
useEffect(() => { | ||
/** | ||
* Loading VGS Collect script from and attaching it to the <head> | ||
*/ | ||
loadVGSCollect({ | ||
vaultId: REACT_APP_VAULT_ID as string, | ||
environment: REACT_APP_ENVIRONMENT as VGSCollectVaultEnvironment, | ||
version: REACT_APP_COLLECT_VERSION as string | ||
}).then(() => { | ||
setCollectScriptLoaded(true); | ||
}); | ||
}, []); | ||
|
||
const onErrorCallback = (errors: VGSCollectFormState) => { | ||
/** | ||
* Receive information about Erorrs (client-side validation, or rejection in async headers function) | ||
*/ | ||
}; | ||
|
||
const onUpdateCallback = (state: VGSCollectFormState) => { | ||
/** | ||
* Listen to the VGS Collect form state | ||
*/ | ||
}; | ||
|
||
const customHandling = (event: any) => { | ||
event.preventDefault(); | ||
if (!isFormSubmitting && formInstance) { | ||
setFormSubmitting(true); | ||
formInstance.submit( | ||
'/post', | ||
{}, | ||
(status, data) => { | ||
console.log('Response:', status, data); | ||
setFormSubmitting(false); | ||
}, | ||
(errors) => { | ||
console.log(errors); | ||
setFormSubmitting(false); | ||
} | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{isVGSCollectScriptLoaded && ( | ||
<div className='left'> | ||
<h2>Custom Submit Handling</h2> | ||
{/** | ||
* VGS Collect form wrapper element. Abstraction over the VGSCollect.create() | ||
* https://www.verygoodsecurity.com/docs/api/collect/#api-vgscollectcreate | ||
*/} | ||
<VGSCollectForm | ||
vaultId={REACT_APP_VAULT_ID as string} | ||
environment={REACT_APP_ENVIRONMENT as VGSCollectVaultEnvironment} | ||
onCustomSubmit={customHandling} | ||
onUpdateCallback={onUpdateCallback} | ||
onErrorCallback={onErrorCallback} | ||
> | ||
{/** | ||
* VGS Collect text field component: | ||
* https://www.verygoodsecurity.com/docs/api/collect/#api-formfield | ||
*/} | ||
<TextField name='textField' validations={['required']} css={VGSCollectFieldStyles} /> | ||
<button type='submit'>Submit</button> | ||
</VGSCollectForm> | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default SubmitHandling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters