Skip to content

Commit

Permalink
Merge branch 'main' into update-openapi-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Feb 27, 2025
2 parents c0f52b1 + 0b72286 commit 3231317
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 60 deletions.
6 changes: 6 additions & 0 deletions integrations/formspree/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @gitbook/integration-formspree

## 0.2.5

### Patch Changes

- 9823932: Fix formspree submit function

## 0.2.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion integrations/formspree/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gitbook/integration-formspree",
"version": "0.2.4",
"version": "0.2.5",
"private": true,
"dependencies": {
"@gitbook/runtime": "workspace:*"
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

0 comments on commit 3231317

Please sign in to comment.