-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into update-openapi-integration
- Loading branch information
Showing
6 changed files
with
65 additions
and
60 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
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,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'; | ||
}; |
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 |
---|---|---|
@@ -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)); | ||
} |
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