Skip to content

Commit

Permalink
Introduce a base currency override param to primary sales
Browse files Browse the repository at this point in the history
  • Loading branch information
luads committed May 17, 2024
1 parent 35f22b2 commit 19bde81
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export type SaleWidgetParams = {
language?: WidgetLanguage;
/** The disabled payment types */
excludePaymentTypes?: SalePaymentTypes[];
/** Preferred currency, replacing the backend's base currency */
baseCurrencyOverride?: string;
};

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/checkout/widgets-lib/src/widgets/sale/SaleWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ export default function SaleWidget(props: SaleWidgetProps) {
environmentId,
collectionName,
excludePaymentTypes,
baseCurrencyOverride,
waitFulfillmentSettlements = true,
} = props;

const { connectLoaderState } = useContext(ConnectLoaderContext);
const { checkout, provider } = connectLoaderState;
const chainId = useRef<ChainId>();
Expand Down Expand Up @@ -124,6 +126,7 @@ export default function SaleWidget(props: SaleWidgetProps) {
passport: checkout?.passport,
collectionName,
excludePaymentTypes,
baseCurrencyOverride,
waitFulfillmentSettlements,
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class Sale extends Base<WidgetType.SALE> {
environmentId={this.parameters.environmentId!}
collectionName={this.parameters.collectionName!}
excludePaymentTypes={this.parameters.excludePaymentTypes!}
baseCurrencyOverride={this.parameters.baseCurrencyOverride!}
language="en"
waitFulfillmentSettlements={this.properties?.config?.waitFulfillmentSettlements ?? true}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type SaleContextProps = {
checkout: ConnectLoaderState['checkout'];
passport?: Passport;
excludePaymentTypes: SalePaymentTypes[];
baseCurrencyOverride?: string;
waitFulfillmentSettlements: boolean;
};

Expand Down Expand Up @@ -115,6 +116,7 @@ const SaleContext = createContext<SaleContextValues>({
orderQuote: defaultOrderQuote,
signTokenIds: [],
excludePaymentTypes: [],
baseCurrencyOverride: undefined,
selectedCurrency: undefined,
waitFulfillmentSettlements: true,
});
Expand All @@ -140,6 +142,7 @@ export function SaleContextProvider(props: {
passport,
collectionName,
excludePaymentTypes,
baseCurrencyOverride,
waitFulfillmentSettlements,
},
} = props;
Expand Down Expand Up @@ -182,6 +185,7 @@ export function SaleContextProvider(props: {
provider,
environmentId,
environment: config.environment,
baseCurrencyOverride,
});

const fromTokenAddress = selectedCurrency?.address || '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type UseQuoteOrderParams = {
environmentId: string;
environment: Environment;
provider: Web3Provider | undefined;
baseCurrencyOverride?: string;
};

export const defaultOrderQuote: OrderQuote = {
Expand All @@ -33,6 +34,7 @@ export const useQuoteOrder = ({
environment,
environmentId,
provider,
baseCurrencyOverride,
}: UseQuoteOrderParams) => {
const [selectedCurrency, setSelectedCurrency] = useState<
OrderQuoteCurrency | undefined
Expand Down Expand Up @@ -108,7 +110,12 @@ export const useQuoteOrder = ({
// Set default currency
if (orderQuote.currencies.length === 0) return;

const defaultSelectedCurrency = orderQuote.currencies.find((c) => c.base)
const preferredCurrency = baseCurrencyOverride
? orderQuote.currencies.find((c) => c.name === baseCurrencyOverride)
: undefined;

const defaultSelectedCurrency = preferredCurrency
|| orderQuote.currencies.find((c) => c.base)
|| orderQuote.currencies?.[0];

setSelectedCurrency(defaultSelectedCurrency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ const useParams = () => {

const multicurrency = urlParams.get("multicurrency") === "true";

const baseCurrencyOverride = urlParams.get("baseCurrencyOverride") as string ?? undefined;

return {
login,
environmentId,
collectionName,
excludePaymentTypes,
multicurrency,
baseCurrencyOverride,
};
};

Expand Down Expand Up @@ -125,6 +128,7 @@ export function SaleUI() {
collectionName,
excludePaymentTypes,
multicurrency,
baseCurrencyOverride,
} = params;
const [passportConfig, setPassportConfig] = useState(
JSON.stringify(defaultPassportConfig, null, 2)
Expand Down Expand Up @@ -177,11 +181,11 @@ export function SaleUI() {
// mount sale widget and subscribe to close event
useEffect(() => {
saleWidget.mount("sale", {

environmentId,
collectionName,
items: defaultItems,
excludePaymentTypes,
baseCurrencyOverride,
});
saleWidget.addListener(SaleEventType.CLOSE_WIDGET, () => {
saleWidget.unmount();
Expand Down Expand Up @@ -283,6 +287,7 @@ export function SaleUI() {
collectionName,
items: defaultItems,
excludePaymentTypes,
baseCurrencyOverride,
})
}
>
Expand Down

0 comments on commit 19bde81

Please sign in to comment.