2020-11-24 13:26:15 +01:00
|
|
|
/**
|
|
|
|
* This component initializes the stripe's Elements tag with the API key
|
|
|
|
*/
|
|
|
|
|
2020-12-08 17:30:33 +01:00
|
|
|
import React, { memo, useEffect, useState } from 'react';
|
2020-11-24 13:26:15 +01:00
|
|
|
import { Elements } from '@stripe/react-stripe-js';
|
|
|
|
import { loadStripe } from "@stripe/stripe-js";
|
2020-11-24 16:26:18 +01:00
|
|
|
import SettingAPI from '../api/setting';
|
|
|
|
import { SettingName } from '../models/setting';
|
2020-11-24 13:26:15 +01:00
|
|
|
|
2020-11-24 16:26:18 +01:00
|
|
|
const stripePublicKey = SettingAPI.get(SettingName.StripePublicKey);
|
2020-11-24 13:26:15 +01:00
|
|
|
|
2020-12-08 12:26:03 +01:00
|
|
|
export const StripeElements: React.FC = memo(({ children }) => {
|
2020-12-08 17:30:33 +01:00
|
|
|
const [stripe, setStripe] = useState(undefined);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const key = stripePublicKey.read();
|
|
|
|
const promise = loadStripe(key.value);
|
|
|
|
setStripe(promise);
|
|
|
|
}, [])
|
2020-11-24 13:26:15 +01:00
|
|
|
|
|
|
|
return (
|
2020-12-08 17:30:33 +01:00
|
|
|
<div>
|
|
|
|
{stripe && <Elements stripe={stripe}>
|
|
|
|
{children}
|
|
|
|
</Elements>}
|
|
|
|
</div>
|
2020-11-24 13:26:15 +01:00
|
|
|
);
|
2020-12-08 12:26:03 +01:00
|
|
|
})
|