Skip to content

Commit

Permalink
Merge pull request #235 from Chia-Network/develop
Browse files Browse the repository at this point in the history
release: 1.1.5 - server override fixes
  • Loading branch information
TheLastCicada authored Sep 20, 2023
2 parents 0921122 + 67cddd2 commit 14caada
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "climate-tokenization-engine-ui",
"version": "1.1.4",
"version": "1.1.5",
"private": true,
"author": "Chia Network Inc. <[email protected]>",
"description": "User Interface for the Climate Tokenization Engine",
Expand Down
6 changes: 3 additions & 3 deletions src/components/blocks/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const PagesContainer = styled(ControlsContainer)`
line-height: 150%;
${props => {
if (props.isActive) {
return `border: 1px solid ${props.theme.colors.default.primaryDark};
color: ${props.theme.colors.default.primaryDark};`;
return `border: 1px solid ${props.theme.colors.default.primary};
color: ${props.theme.colors.default.primary};`;
} else {
return `border: 1px solid ${props.theme.colors.default.gray4};
color: ${props.theme.colors.default.secondary};`;
Expand All @@ -72,7 +72,7 @@ const Pagination = withTheme(
({ pages, current, showLast = false, callback }) => {
// if current page number higher or equal than number of pages, first page is displayed
const currentPageNumber =
current && current > 0 && current < pages ? current + 1 : 1;
current && current > 0 && current < pages ? current : 1;
const numberOfPages = pages && pages !== 0 ? pages : 1;
const changeCurrentPageTo = value => callback(value - 1);
const backButtonIsDisabled = currentPageNumber === 1;
Expand Down
34 changes: 22 additions & 12 deletions src/store/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,32 @@ export const confirmDetokanization = data => {
};
};

const maybeServerOverrideFetch = async (url, payload) => {
const maybeServerOverrideFetch = async (originalUrl, payload) => {
const apiKey = localStorage.getItem(
'climateTokenizationEngineRemoteServerApiKey',
);

const serverAddress = localStorage.getItem(
'climateTokenizationEngineRemoteServerAddress',
);
const doesSignInDataExist = apiKey != null && serverAddress != null;

if (doesSignInDataExist) {
// If serverAddress is valid, replace the domain of the original URL.
if (serverAddress && typeof serverAddress === 'string') {
const serverUrl = new URL(serverAddress);
originalUrl = new URL(originalUrl);

// Remove trailing slash from serverUrl.pathname if it exists
let serverPathname = serverUrl.pathname;
serverPathname = serverPathname.replace(/\/$/, '');

// Replace the domain of the original URL with the server URL domain.
// Also, append the server URL's pathname to the original URL's pathname.
// And, maintain the query parameters from the original URL.
const newUrl = new URL(
serverPathname + originalUrl.pathname + originalUrl.search,
serverUrl,
);

const payloadWithApiKey = { ...payload };
if (payloadWithApiKey?.headers) {
payloadWithApiKey.headers = {
Expand All @@ -572,17 +588,11 @@ const maybeServerOverrideFetch = async (url, payload) => {
payloadWithApiKey.headers = { 'x-api-key': apiKey };
}

const serverAddressUrl =
serverAddress[serverAddress.length - 1] !== '/'
? `${serverAddress}/`
: serverAddressUrl;

const newUrl = url.replace(serverAddressUrl);

return fetch(newUrl, payloadWithApiKey);
return fetch(newUrl.toString(), payloadWithApiKey);
}

return fetch(url, payload);
// If serverAddress is not valid, return the original URL.
return fetch(originalUrl, payload);
};

// encapsulates error handling, network failure, loader toggling and on success or failed handlers
Expand Down

0 comments on commit 14caada

Please sign in to comment.