-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replaces convertkit with local database-backed form component and provider - adds SubscribeToCoursebuilderForm component - adds local email list provider - updates docs with usage examples
- Loading branch information
Showing
9 changed files
with
175 additions
and
15 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
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,5 +1,5 @@ | ||
import SubscribeToConvertkitForm, { | ||
import SubscribeToCoursebuilderForm, { | ||
redirectUrlBuilder, | ||
} from './convertkit-subscribe-form' | ||
} from './coursebuilder-subscribe-form' | ||
|
||
export { SubscribeToConvertkitForm, redirectUrlBuilder } | ||
export { SubscribeToCoursebuilderForm, redirectUrlBuilder } |
72 changes: 72 additions & 0 deletions
72
apps/go-local-first/src/convertkit/useCoursebuilderSubscribeForm.ts
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,72 @@ | ||
import { type Subscriber } from '@/schemas/subscriber' | ||
import axios from 'axios' | ||
import { useFormik } from 'formik' | ||
import * as Yup from 'yup' | ||
|
||
export function useCoursebuilderSubscribeForm({ | ||
submitUrl = `/api/coursebuilder/subscribe-to-list/coursebuilder`, | ||
formId = 0, | ||
fields, | ||
onSuccess, | ||
onError, | ||
validationSchema, | ||
validateOnChange = false, | ||
}: { | ||
submitUrl?: string | ||
formId?: number | ||
onSuccess: (subscriber: Subscriber, email?: string) => void | ||
onError: (error?: any) => void | ||
fields?: any | ||
validationSchema?: Yup.ObjectSchema<any> | ||
validateOnChange?: boolean | ||
}): { | ||
isSubmitting: boolean | ||
status: string | ||
handleChange: any | ||
handleSubmit: any | ||
errors: any | ||
touched: any | ||
} { | ||
const { isSubmitting, status, handleChange, handleSubmit, errors, touched } = | ||
useFormik({ | ||
initialStatus: '', | ||
initialValues: { | ||
email: '', | ||
first_name: '', | ||
}, | ||
validationSchema: | ||
validationSchema || | ||
Yup.object().shape({ | ||
email: Yup.string() | ||
.email('Invalid email address') | ||
.required('Required'), | ||
first_name: Yup.string(), | ||
}), | ||
validateOnChange: validateOnChange, | ||
enableReinitialize: true, | ||
onSubmit: async ({ email, first_name }, { setStatus }) => { | ||
return axios | ||
.post(submitUrl, { | ||
email, | ||
name: first_name, | ||
...(formId > 0 ? { form: formId } : {}), | ||
fields, | ||
}) | ||
.then((response: any) => { | ||
const subscriber: Subscriber = response.data | ||
onSuccess(subscriber, email) | ||
setStatus('success') | ||
if (!subscriber) { | ||
setStatus('error') | ||
} | ||
}) | ||
.catch((error: Error) => { | ||
onError(error) | ||
setStatus('error') | ||
console.log(error) | ||
}) | ||
}, | ||
}) | ||
|
||
return { isSubmitting, status, handleChange, handleSubmit, errors, touched } | ||
} |
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