Next.js website for Rocky Mountain Vending company featuring: - Product catalog with Stripe integration - Service areas and parts pages - Admin dashboard with Clerk authentication - SEO optimized pages with JSON-LD structured data Co-authored-by: Cursor <cursoragent@cursor.com>
54 lines
1.2 KiB
Text
54 lines
1.2 KiB
Text
import {
|
|
validateLoadParams,
|
|
loadScript,
|
|
initStripe,
|
|
LoadStripe,
|
|
LoadParams,
|
|
} from './shared';
|
|
|
|
type SetLoadParams = (params: LoadParams) => void;
|
|
|
|
let loadParams: null | LoadParams;
|
|
let loadStripeCalled = false;
|
|
|
|
export const loadStripe: LoadStripe & {setLoadParameters: SetLoadParams} = (
|
|
...args
|
|
) => {
|
|
loadStripeCalled = true;
|
|
const startTime = Date.now();
|
|
|
|
return loadScript(loadParams).then((maybeStripe) =>
|
|
initStripe(maybeStripe, args, startTime)
|
|
);
|
|
};
|
|
|
|
loadStripe.setLoadParameters = (params): void => {
|
|
// we won't throw an error if setLoadParameters is called with the same values as before
|
|
if (loadStripeCalled && loadParams) {
|
|
const validatedParams = validateLoadParams(params);
|
|
const parameterKeys = Object.keys(validatedParams) as Array<
|
|
keyof LoadParams
|
|
>;
|
|
|
|
const sameParameters = parameterKeys.reduce(
|
|
(previousValue, currentValue) => {
|
|
return (
|
|
previousValue && params[currentValue] === loadParams?.[currentValue]
|
|
);
|
|
},
|
|
true
|
|
);
|
|
|
|
if (sameParameters) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (loadStripeCalled) {
|
|
throw new Error(
|
|
'You cannot change load parameters after calling loadStripe'
|
|
);
|
|
}
|
|
|
|
loadParams = validateLoadParams(params);
|
|
};
|