Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formspree submit function #727

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-singers-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/integration-formspree': patch
---

Fix formspree submit function
10 changes: 5 additions & 5 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
},
"integrations/formspree": {
"name": "@gitbook/integration-formspree",
"version": "0.2.3",
"version": "0.2.4",
"dependencies": {
"@gitbook/runtime": "packages/runtime",
},
Expand Down Expand Up @@ -340,7 +340,7 @@
},
"integrations/openapi": {
"name": "@gitbook/integration-openapi",
"version": "0.0.0",
"version": "0.0.1",
"dependencies": {
"@gitbook/api": "*",
"@gitbook/document": "*",
Expand Down Expand Up @@ -534,7 +534,7 @@
},
"packages/api": {
"name": "@gitbook/api",
"version": "0.94.0",
"version": "0.96.0",
"dependencies": {
"event-iterator": "^2.0.0",
"eventsource-parser": "^3.0.0",
Expand Down Expand Up @@ -577,7 +577,7 @@
},
"packages/document": {
"name": "@gitbook/document",
"version": "0.0.0",
"version": "0.1.0",
"dependencies": {
"@gitbook/api": "*",
},
Expand All @@ -588,7 +588,7 @@
},
"packages/runtime": {
"name": "@gitbook/runtime",
"version": "0.18.0",
"version": "0.19.0",
"dependencies": {
"@gitbook/api": "*",
},
Expand Down
48 changes: 18 additions & 30 deletions integrations/formspree/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
import {
createIntegration,
createComponent,
RuntimeContext,
RuntimeEnvironment,
} from '@gitbook/runtime';
import { createIntegration, createComponent } from '@gitbook/runtime';

import { handleSubmit } from './utils';

type FormspreeConfiguration = {
formspree_id: string;
email: string;
name: string;
message: string;
};

type FormspreeEnvironment = RuntimeEnvironment<FormspreeConfiguration>;
type FormspreeContext = RuntimeContext<FormspreeEnvironment>;

type FormspreeAction = {
action: 'submit';
};
import type {
FormspreeActionResponse,
FormspreeConfiguration,
FormspreeAction,
FormspreeContext,
} from './types';

const formspreeBlock = createComponent<
{},
| { formSubmitted: true }
| {
email: string;
name: string;
message: string;
formSubmitted: boolean;
},
Record<string, string>,
FormspreeActionResponse,
FormspreeAction,
FormspreeContext
>({
Expand All @@ -42,12 +23,12 @@ const formspreeBlock = createComponent<
},
action: async (element, action, context) => {
switch (action.action) {
case 'submit':
case 'submit': {
if (element.state.formSubmitted) {
return element;
}

handleSubmit(
const success = await handleSubmit(
(context.environment.spaceInstallation?.configuration as FormspreeConfiguration)
.formspree_id,
{
Expand All @@ -56,11 +37,17 @@ const formspreeBlock = createComponent<
message: element.state.message,
},
);

if (!success) {
return element;
}

return {
state: {
formSubmitted: true,
},
};
}
}
},
render: async (element, context: FormspreeContext) => {
Expand Down Expand Up @@ -113,6 +100,7 @@ const formspreeBlock = createComponent<
label={element.state.formSubmitted ? 'Submitted' : 'Submit'}
onPress={{ action: 'submit' }}
disabled={element.state.formSubmitted}
style="primary"
/>
</block>
);
Expand Down
28 changes: 28 additions & 0 deletions integrations/formspree/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { RuntimeContext, RuntimeEnvironment } from '@gitbook/runtime';

export type FormspreeBodyData = {
email: string;
name: string;
message: string;
};

export type FormspreeConfiguration = FormspreeBodyData & {
formspree_id: string;
};

type FormspreeResponseSuccess = {
formSubmitted: true;
};

type FormspreeResponseFailure = FormspreeBodyData & {
formSubmitted: boolean;
};

export type FormspreeActionResponse = FormspreeResponseSuccess | FormspreeResponseFailure;

export type FormspreeEnvironment = RuntimeEnvironment<FormspreeConfiguration>;
export type FormspreeContext = RuntimeContext<FormspreeEnvironment>;

export type FormspreeAction = {
action: 'submit';
};
35 changes: 9 additions & 26 deletions integrations/formspree/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
// Handle errors better
import type { FormspreeBodyData } from './types';

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

fetch(formspreeId, {
return fetch(formspreeId, {
method: 'POST',
body: JSON.stringify({ data: cleanedFormBody, form: 'GitBook Integration' }),
body: JSON.stringify(cleanedFormBody),
headers: {
Accept: 'application/json',
},
})
.then((response) => {
if (response.ok) {
return true;
} else {
return false;
}
})
.catch((error) => {
console.error(error);
});
});
}

// Clean data object being submitted
export async function removeEmptyValues(object: any) {
for (const key in object) {
if (object.hasOwnProperty(key)) {
const value = object[key];
if (value === null || value === undefined || value === '') {
delete object[key];
}
}
}
return object;
export function removeEmptyValues(object: FormspreeBodyData) {
return Object.fromEntries(Object.entries(object).filter(([, value]) => !!value));
}
6 changes: 3 additions & 3 deletions packages/runtime/src/jsx-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
ContentKitBox,
ContentKitButton,
ContentKitCard,
Expand All @@ -25,7 +25,7 @@ import {
ContentKitConfiguration,
} from '@gitbook/api';

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

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

interface IntrinsicElements {
Expand Down
Loading