-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// @ts-nocheck | ||
import type { ComponentType } from "react"; | ||
import { useState, useEffect } from "react"; | ||
// @ts-ignore | ||
import { createStore } from "https://framer.com/m/framer/store.js@^1.0.0"; | ||
|
||
const emptyStore = { | ||
email: "", | ||
env: "prod", | ||
isLoading: false, | ||
isSubscribed: false, | ||
isFailed: false, | ||
}; | ||
const useStore = createStore(emptyStore); | ||
|
||
// Buttons | ||
|
||
export const submitNewsletterForm = (Component: any): ComponentType => { | ||
return (props: any) => { | ||
const [store, setStore] = useStore(); | ||
|
||
useEffect(() => { | ||
setStore({ | ||
env: location.host === "giveffektivt.dk" ? "prod" : "dev", | ||
}); | ||
}, [setStore]); | ||
|
||
const onTap = async () => submitNewsletter(store, setStore); | ||
|
||
return store.email.includes("@") && | ||
!store.isLoading && | ||
!store.isSubscribed && | ||
!store.isFailed ? ( | ||
<Component {...props} onTap={onTap} /> | ||
) : null; | ||
}; | ||
}; | ||
|
||
export const submitNewsletterForm_isDisabled = ( | ||
Component: any, | ||
): ComponentType => { | ||
return (props: any) => { | ||
const [store] = useStore(); | ||
return !store.email.includes("@") && | ||
!store.isLoading && | ||
!store.isSubscribed && | ||
!store.isFailed ? ( | ||
<Component {...props} /> | ||
) : null; | ||
}; | ||
}; | ||
|
||
export const submitNewsletterForm_isLoading = ( | ||
Component: any, | ||
): ComponentType => { | ||
return (props: any) => { | ||
const [store] = useStore(); | ||
return store.isLoading ? <Component {...props} /> : null; | ||
}; | ||
}; | ||
|
||
export const submitNewsletterForm_isSubscribed = ( | ||
Component: any, | ||
): ComponentType => { | ||
return (props: any) => { | ||
const [store] = useStore(); | ||
return store.isSubscribed ? <Component {...props} /> : null; | ||
}; | ||
}; | ||
|
||
export const submitNewsletterForm_isFailed = ( | ||
Component: any, | ||
): ComponentType => { | ||
return (props: any) => { | ||
const [store] = useStore(); | ||
return store.isFailed ? <Component {...props} /> : null; | ||
}; | ||
}; | ||
|
||
// Inputs | ||
|
||
const setInput = (Component: any, field: string, isValid): ComponentType => { | ||
return (props: any) => { | ||
const [store, setStore] = useStore(); | ||
|
||
const onValueChange = (value: string) => setStore({ [field]: value }); | ||
|
||
return ( | ||
<Component | ||
{...props} | ||
value={store[field]} | ||
onValueChange={onValueChange} | ||
isError={ | ||
isValid ? store[field] !== "" && !isValid(store[field]) : false | ||
} | ||
/> | ||
); | ||
}; | ||
}; | ||
|
||
export const inputEmail = (Component: any): ComponentType => { | ||
return setInput(Component, "email", (value) => value.includes("@")); | ||
}; | ||
|
||
// Debug | ||
|
||
export const showDebug = (Component: any): ComponentType => { | ||
return (props) => { | ||
const [store] = useStore(); | ||
|
||
return store.env === "prod" ? null : ( | ||
<Component | ||
{...props} | ||
text={`${JSON.stringify(prepareNewsletterPayload(store), null, 4)}`} | ||
/> | ||
); | ||
}; | ||
}; |
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,18 @@ | ||
const prepareNewsletterPayload = (store: any) => ({ email: store.email }); | ||
|
||
const submitNewsletter = async (store: any, setStore: any) => { | ||
try { | ||
setStore({ isLoading: true, isSubscribed: false, isFailed: false }); | ||
track("Newsletter form submitted", 20); | ||
|
||
await submitForm(store.env, "newsletter", prepareNewsletterPayload(store)); | ||
setStore({ isSubscribed: true }); | ||
setTimeout(() => setStore({ email: "" }), 5000); | ||
} catch (err) { | ||
setStore({ isFailed: true }); | ||
notifyAboutClientSideError("submitNewsletter", err?.toString()); | ||
} | ||
|
||
setStore({ isLoading: false }); | ||
setTimeout(() => setStore({ isSubscribed: false, isFailed: false }), 5000); | ||
}; |
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