Skip to content

Commit 9823932

Browse files
authored
Fix formspree submit function (#727)
* Fix formspree integration * Changeset * Fix lint
1 parent 5d56b9c commit 9823932

File tree

6 files changed

+68
-64
lines changed

6 files changed

+68
-64
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/integration-formspree': patch
3+
---
4+
5+
Fix formspree submit function

bun.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
},
8888
"integrations/formspree": {
8989
"name": "@gitbook/integration-formspree",
90-
"version": "0.2.3",
90+
"version": "0.2.4",
9191
"dependencies": {
9292
"@gitbook/runtime": "packages/runtime",
9393
},
@@ -340,7 +340,7 @@
340340
},
341341
"integrations/openapi": {
342342
"name": "@gitbook/integration-openapi",
343-
"version": "0.0.0",
343+
"version": "0.0.1",
344344
"dependencies": {
345345
"@gitbook/api": "*",
346346
"@gitbook/document": "*",
@@ -534,7 +534,7 @@
534534
},
535535
"packages/api": {
536536
"name": "@gitbook/api",
537-
"version": "0.94.0",
537+
"version": "0.96.0",
538538
"dependencies": {
539539
"event-iterator": "^2.0.0",
540540
"eventsource-parser": "^3.0.0",
@@ -577,7 +577,7 @@
577577
},
578578
"packages/document": {
579579
"name": "@gitbook/document",
580-
"version": "0.0.0",
580+
"version": "0.1.0",
581581
"dependencies": {
582582
"@gitbook/api": "*",
583583
},
@@ -588,7 +588,7 @@
588588
},
589589
"packages/runtime": {
590590
"name": "@gitbook/runtime",
591-
"version": "0.18.0",
591+
"version": "0.19.0",
592592
"dependencies": {
593593
"@gitbook/api": "*",
594594
},

integrations/formspree/src/index.tsx

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,16 @@
1-
import {
2-
createIntegration,
3-
createComponent,
4-
RuntimeContext,
5-
RuntimeEnvironment,
6-
} from '@gitbook/runtime';
1+
import { createIntegration, createComponent } from '@gitbook/runtime';
72

83
import { 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

2411
const 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
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { RuntimeContext, RuntimeEnvironment } from '@gitbook/runtime';
2+
3+
export type FormspreeBodyData = {
4+
email: string;
5+
name: string;
6+
message: string;
7+
};
8+
9+
export type FormspreeConfiguration = FormspreeBodyData & {
10+
formspree_id: string;
11+
};
12+
13+
type FormspreeResponseSuccess = {
14+
formSubmitted: true;
15+
};
16+
17+
type FormspreeResponseFailure = FormspreeBodyData & {
18+
formSubmitted: boolean;
19+
};
20+
21+
export type FormspreeActionResponse = FormspreeResponseSuccess | FormspreeResponseFailure;
22+
23+
export type FormspreeEnvironment = RuntimeEnvironment<FormspreeConfiguration>;
24+
export type FormspreeContext = RuntimeContext<FormspreeEnvironment>;
25+
26+
export type FormspreeAction = {
27+
action: 'submit';
28+
};
Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
1-
// Handle errors better
1+
import type { FormspreeBodyData } from './types';
22

3-
export async function handleSubmit(formspreeId: string, body: any) {
4-
const cleanedFormBody = await removeEmptyValues(body);
3+
// Submit form data to Formspree
4+
export async function handleSubmit(formspreeId: string, body: FormspreeBodyData) {
5+
const cleanedFormBody = removeEmptyValues(body);
56

6-
fetch(formspreeId, {
7+
return fetch(formspreeId, {
78
method: 'POST',
8-
body: JSON.stringify({ data: cleanedFormBody, form: 'GitBook Integration' }),
9+
body: JSON.stringify(cleanedFormBody),
910
headers: {
1011
Accept: 'application/json',
1112
},
12-
})
13-
.then((response) => {
14-
if (response.ok) {
15-
return true;
16-
} else {
17-
return false;
18-
}
19-
})
20-
.catch((error) => {
21-
console.error(error);
22-
});
13+
});
2314
}
2415

2516
// Clean data object being submitted
26-
export async function removeEmptyValues(object: any) {
27-
for (const key in object) {
28-
if (object.hasOwnProperty(key)) {
29-
const value = object[key];
30-
if (value === null || value === undefined || value === '') {
31-
delete object[key];
32-
}
33-
}
34-
}
35-
return object;
17+
export function removeEmptyValues(object: FormspreeBodyData) {
18+
return Object.fromEntries(Object.entries(object).filter(([, value]) => !!value));
3619
}

packages/runtime/src/jsx-runtime.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import type {
22
ContentKitBox,
33
ContentKitButton,
44
ContentKitCard,
@@ -25,7 +25,7 @@ import {
2525
ContentKitConfiguration,
2626
} from '@gitbook/api';
2727

28-
import { jsx, jsxDEV, jsxs, Fragment } from './contentkit-jsx';
28+
import type { jsx, jsxDEV, jsxs, Fragment } from './contentkit-jsx';
2929

3030
/**
3131
* This is a workaround for Typescript not supporting subpath exports in package.json
@@ -44,7 +44,7 @@ type OmitType<T> = Omit<T, 'type'>;
4444
declare global {
4545
namespace JSX {
4646
interface ElementChildrenAttribute {
47-
children: {}; // specify children name to use
47+
children: Record<string, unknown>; // specify children name to use
4848
}
4949

5050
interface IntrinsicElements {

0 commit comments

Comments
 (0)