Skip to content

Commit ed798c7

Browse files
authored
Add support for staging environments (#459)
* Add support for staging environments * factorize url getting * improvement * Update readme * lint
1 parent f2f6483 commit ed798c7

13 files changed

Lines changed: 76 additions & 27 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ The below code will use Every.org styling, because the `a` tag has the `data-eve
3030
</html>
3131
```
3232

33+
Note that you can use the donate button in our staging environment by using a staging link like: https://staging.every.org/ofsds#/donate
34+
3335
## Configure
3436

3537
To configure your button manually, add `?explicit=1` to the script `src`
@@ -69,6 +71,8 @@ Here is an example html file with a manual configuration.
6971
</html>
7072
```
7173

74+
Note that you can use the donate button in our staging environment by adding the `staging: true` parameter to the `create*` calls.
75+
7276
### Widget
7377

7478
#### Configuration options

packages/donate-button-v4/src/autoPlayMode.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {render as preactRender} from 'preact';
22
import EmbedButton from 'src/components/embed-button';
33
import {WidgetConfig} from 'src/components/widget/types/WidgetConfig';
4+
import {BASE_URL, STAGING_BASE_URL} from 'src/constants/url';
45
import {parseDonateUrl} from 'src/helpers/parseDonateUrl';
56
import shouldApplyEveryStyleForAllLinks from 'src/helpers/shouldApplyEveryStyleForAllLinks';
67
import {loadFonts} from 'src/loadFonts';
@@ -114,7 +115,9 @@ function createButton({element, ...options}: CreateButtonProps) {
114115
}
115116

116117
function findAndReplaceLinks() {
117-
const links = document.querySelectorAll("*[href^='https://www.every.org/']");
118+
const links = document.querySelectorAll(
119+
`*[href^='${BASE_URL}'], *[href^='${STAGING_BASE_URL}']`
120+
);
118121

119122
links.forEach((link) => {
120123
const urlString = link.getAttribute('href');

packages/donate-button-v4/src/components/embed-button/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export interface DonateButtonOptions {
2323
readonly noExit?: boolean;
2424

2525
url?: string;
26+
27+
staging?: boolean;
2628
}
2729
export interface EmbedButtonOptions extends DonateButtonOptions {
2830
readonly label?: string;

packages/donate-button-v4/src/components/widget/api/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {DonateFlowCustomization} from 'src/components/widget/types/DonateFlowCustomization';
22
import {Fundraiser} from 'src/components/widget/types/Fundraiser';
33
import {Nonprofit} from 'src/components/widget/types/Nonprofit';
4-
import {BASE_API_URL, BASE_COINGECKO_URL} from 'src/constants/url';
4+
import {BASE_COINGECKO_URL, getApiUrl} from 'src/constants/url';
55

66
type NonprofitResponse = {
77
message: string;
@@ -17,19 +17,22 @@ type FundraiserResponse = {
1717
};
1818
};
1919

20-
export async function getNonprofit(nonprofitSlug: string) {
20+
export async function getNonprofit(nonprofitSlug: string, staging?: boolean) {
21+
const apiUrl = getApiUrl(staging);
2122
const data: NonprofitResponse = await fetch(
22-
`${BASE_API_URL}/${nonprofitSlug}`
23+
`${apiUrl}/${nonprofitSlug}`
2324
).then(async (response) => response.json());
2425

2526
return data.data.nonprofit;
2627
}
2728

2829
export async function getFundraiser(
2930
nonprofitSlug: string,
30-
fundraiserSlug: string
31+
fundraiserSlug: string,
32+
staging?: boolean
3133
) {
32-
const url = `${BASE_API_URL}/${nonprofitSlug}/fundraiser/${fundraiserSlug}`;
34+
const apiUrl = getApiUrl(staging);
35+
const url = `${apiUrl}/${nonprofitSlug}/fundraiser/${fundraiserSlug}`;
3336
const data: FundraiserResponse = await fetch(url).then(async (response) =>
3437
response.json()
3538
);
@@ -52,8 +55,13 @@ export async function getCoingeckoRate(coingeckoId: string) {
5255
return (data as CoingeckoData).market_data.current_price.usd;
5356
}
5457

55-
export async function getCustomization(nonprofitId: string, code?: string) {
56-
const url = `${BASE_API_URL}/${nonprofitId}/customization${
58+
export async function getCustomization(
59+
nonprofitId: string,
60+
code?: string,
61+
staging?: boolean
62+
) {
63+
const apiUrl = getApiUrl(staging);
64+
const url = `${apiUrl}/${nonprofitId}/customization${
5765
code ? `?code=${code}` : ''
5866
}`;
5967
const response = await fetch(url).then(async (response) => {

packages/donate-button-v4/src/components/widget/components/PaymentProcess/CryptoFlow/CryptoSelector.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {filter as fuzzyFilter} from 'fuzzy';
2+
import {getSiteUrl} from 'src/constants/url';
23
import {useRef, useState} from 'preact/hooks';
34
import {Fragment} from 'preact/jsx-runtime';
45
import {linkCss} from 'src/components/widget/components/FundraiserLink/styles';
@@ -102,8 +103,9 @@ const CryptoSelectorDropDownItem = ({
102103
const CryptoSupprotLink = () => {
103104
const nonprofit = useNonprofitOrError();
104105

105-
const {primaryColor} = useConfigContext();
106-
const cryptoSupportBody = `Contents: I would like to make a crypto donation to support https://www.every.org/${nonprofit.primarySlug}.\n\nMy name:\nToken name:\nToken symbol:\nToken quantity:\n\nPlease reply back with an address where I can donate, as this is worth over $100,000 USD.`;
106+
const {primaryColor, staging} = useConfigContext();
107+
const baseUrl = getSiteUrl(staging);
108+
const cryptoSupportBody = `Contents: I would like to make a crypto donation to support ${baseUrl}${nonprofit.primarySlug}.\n\nMy name:\nToken name:\nToken symbol:\nToken quantity:\n\nPlease reply back with an address where I can donate, as this is worth over $100,000 USD.`;
107109

108110
return (
109111
<a

packages/donate-button-v4/src/components/widget/context/FundraiserContext.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const FundraiserContext = createContext<FundraiserContextData>({
2121
export const FundraiserContextProvider: FunctionalComponent<{
2222
fundraiserSlug?: string;
2323
}> = ({children}) => {
24-
const {fundraiserSlug, nonprofitSlug} = useConfigContext();
24+
const {fundraiserSlug, nonprofitSlug, staging} = useConfigContext();
2525
const [fundraiser, setFundraiser] =
2626
useState<FundraiserContextData['fundraiser']>(FundraiserFetching);
2727

@@ -32,12 +32,16 @@ export const FundraiserContextProvider: FunctionalComponent<{
3232
}
3333

3434
try {
35-
const response = await getFundraiser(nonprofitSlug, fundraiserSlug);
35+
const response = await getFundraiser(
36+
nonprofitSlug,
37+
fundraiserSlug,
38+
staging
39+
);
3640
setFundraiser(response);
3741
} catch {
3842
setFundraiser(FundraiserFetchError);
3943
}
40-
}, [nonprofitSlug, fundraiserSlug]);
44+
}, [nonprofitSlug, fundraiserSlug, staging]);
4145

4246
useEffect(() => {
4347
// eslint-disable-next-line @typescript-eslint/no-floating-promises

packages/donate-button-v4/src/components/widget/context/NonprofitContext.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export const NonprofitContext = createContext<NonprofitContextData>({
3030
export const NonprofitContextProvider: FunctionalComponent<{
3131
nonprofitSlug?: string;
3232
code?: string;
33-
}> = ({children, nonprofitSlug, code}) => {
33+
staging?: boolean;
34+
}> = ({children, nonprofitSlug, code, staging}) => {
3435
const [nonprofit, setNonprofit] =
3536
useState<NonprofitContextData['nonprofit']>(NonprofitFetching);
3637
const [parentNonprofit, setParentNonprofitNonprofit] =
@@ -45,7 +46,7 @@ export const NonprofitContextProvider: FunctionalComponent<{
4546
throw new Error('No nonprofit slug provided');
4647
}
4748

48-
const response = await getNonprofit(nonprofitSlug);
49+
const response = await getNonprofit(nonprofitSlug, staging);
4950
setNonprofit(response);
5051

5152
const parentNonprofitId =
@@ -55,7 +56,7 @@ export const NonprofitContextProvider: FunctionalComponent<{
5556

5657
if (parentNonprofitId) {
5758
try {
58-
const response = await getNonprofit(parentNonprofitId);
59+
const response = await getNonprofit(parentNonprofitId, staging);
5960
setParentNonprofitNonprofit(response);
6061
} catch {
6162
setParentNonprofitNonprofit(undefined);
@@ -66,7 +67,8 @@ export const NonprofitContextProvider: FunctionalComponent<{
6667
try {
6768
const customizationResponse = await getCustomization(
6869
response.id,
69-
code
70+
code,
71+
staging
7072
);
7173
setCustomization(customizationResponse);
7274
} catch {
@@ -78,7 +80,7 @@ export const NonprofitContextProvider: FunctionalComponent<{
7880
} catch {
7981
setNonprofit(NonprofitFetchError);
8082
}
81-
}, [nonprofitSlug, code]);
83+
}, [nonprofitSlug, code, staging]);
8284

8385
useEffect(() => {
8486
// eslint-disable-next-line @typescript-eslint/no-floating-promises

packages/donate-button-v4/src/components/widget/context/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const ContextProvider: FunctionComponent<{
1212
<NonprofitContextProvider
1313
nonprofitSlug={options.nonprofitSlug}
1414
code={options.code}
15+
staging={options.staging}
1516
>
1617
<FundraiserContextProvider fundraiserSlug={options.fundraiserSlug}>
1718
<ConfigContextProvider options={options}>

packages/donate-button-v4/src/components/widget/hooks/useSubmitDonation.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export const useSubmitDonation = () => {
3939
webhookToken,
4040
redeemGiftCardInFlow,
4141
designation,
42-
requireShareInfo
42+
requireShareInfo,
43+
staging
4344
} = useConfigContext();
4445

4546
const submitDonation = useCallback(
@@ -72,7 +73,8 @@ export const useSubmitDonation = () => {
7273
partnerMetadata: config.partnerMetadata,
7374
designation,
7475
requireShareInfo,
75-
customFieldResponses
76+
customFieldResponses,
77+
staging
7678
};
7779
switch (selectedPaymentMethod) {
7880
case PaymentMethod.CRYPTO:
@@ -156,7 +158,8 @@ export const useSubmitDonation = () => {
156158
designation,
157159
requireShareInfo,
158160
customization,
159-
customFieldValues
161+
customFieldValues,
162+
staging
160163
]
161164
);
162165

packages/donate-button-v4/src/components/widget/types/WidgetConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ export interface WidgetConfig {
5757
previewMode?: boolean;
5858

5959
code?: string;
60+
61+
staging?: boolean;
6062
}

0 commit comments

Comments
 (0)