1- import {
2- createIntegration ,
3- createComponent ,
4- RuntimeContext ,
5- RuntimeEnvironment ,
6- } from '@gitbook/runtime' ;
1+ import { createIntegration , createComponent } from '@gitbook/runtime' ;
72
83import { handleSubmit } from './utils' ;
9-
10- type FormspreeConfiguration = {
11- formspree_id : string ;
12- email : string ;
13- name : string ;
14- message : string ;
15- } ;
16-
17- type FormspreeEnvironment = RuntimeEnvironment < FormspreeConfiguration > ;
18- type FormspreeContext = RuntimeContext < FormspreeEnvironment > ;
19-
20- type FormspreeAction = {
21- action : 'submit' ;
22- } ;
4+ import type {
5+ FormspreeActionResponse ,
6+ FormspreeConfiguration ,
7+ FormspreeAction ,
8+ FormspreeContext ,
9+ } from './types' ;
2310
2411const formspreeBlock = createComponent <
25- { } ,
26- | { formSubmitted : true }
27- | {
28- email : string ;
29- name : string ;
30- message : string ;
31- formSubmitted : boolean ;
32- } ,
12+ Record < string , string > ,
13+ FormspreeActionResponse ,
3314 FormspreeAction ,
3415 FormspreeContext
3516> ( {
@@ -42,12 +23,12 @@ const formspreeBlock = createComponent<
4223 } ,
4324 action : async ( element , action , context ) => {
4425 switch ( action . action ) {
45- case 'submit' :
26+ case 'submit' : {
4627 if ( element . state . formSubmitted ) {
4728 return element ;
4829 }
4930
50- handleSubmit (
31+ const success = await handleSubmit (
5132 ( context . environment . spaceInstallation ?. configuration as FormspreeConfiguration )
5233 . formspree_id ,
5334 {
@@ -56,11 +37,17 @@ const formspreeBlock = createComponent<
5637 message : element . state . message ,
5738 } ,
5839 ) ;
40+
41+ if ( ! success ) {
42+ return element ;
43+ }
44+
5945 return {
6046 state : {
6147 formSubmitted : true ,
6248 } ,
6349 } ;
50+ }
6451 }
6552 } ,
6653 render : async ( element , context : FormspreeContext ) => {
@@ -113,6 +100,7 @@ const formspreeBlock = createComponent<
113100 label = { element . state . formSubmitted ? 'Submitted' : 'Submit' }
114101 onPress = { { action : 'submit' } }
115102 disabled = { element . state . formSubmitted }
103+ style = "primary"
116104 />
117105 </ block >
118106 ) ;
0 commit comments