Skip to content

Commit

Permalink
Merge branch 'canary' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
archx3 committed Jun 6, 2024
2 parents 9d729c1 + 5709eb0 commit d5d63af
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
33 changes: 19 additions & 14 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import React, { useState, useEffect } from "react";
import { Switch, Route } from "react-router-dom";
import * as Sentry from "@sentry/react";


import "./assets/css/style.css";
import AppRouter from "./AppRouter";
import { connect, useDispatch, useSelector } from "react-redux";
import { apiCall } from "./api/functions";
import LoadingCircle from "./components/Shared/LoadingCircle";
import { getIsSandboxFromURL } from "./components/Utils";
import {
changeToProperURL,
domainsAreTheSame,
getIsSandboxFromURL,
getSubdomainFromURL,
} from "./components/Utils";
import {
LOAD_COMMUNITY_INFORMATION,
SET_IS_CUSTOM_SITE,
Expand Down Expand Up @@ -39,24 +43,20 @@ function App() {
payload: is_sandbox,
});

// Update the document title using the browser API
if (!community) {
const hostname = window.location.hostname;
const pathname = window.location.pathname;
const slash = pathname.indexOf("/", 1);
const subdomain =
slash > 0 ? pathname.substring(1, slash) : pathname.substring(1);

const subdomain = getSubdomainFromURL(window.location.href);
let body = {};
if (URLS.NONE_CUSTOM_WEBSITE_LIST.has(hostname)) {
const isAMassenergizeDomain = URLS.NONE_CUSTOM_WEBSITE_LIST.has(hostname);
if (isAMassenergizeDomain) {
// if no subdomain found, redirect to all communities page (NB: The all communities page does not exist on this side of the application. It is a page on the backend)
const thereIsNoSubdomain = [undefined, "", "/"].indexOf(subdomain) > -1;
if (thereIsNoSubdomain) {
window.location.href = IS_DEV ? DEV_URL : URLS.COMMUNITIES;
return;
}

body = subdomain ? { subdomain: subdomain } : {};
body = subdomain ? { subdomain } : {};
dispatch({
type: SET_IS_CUSTOM_SITE,
payload: false,
Expand All @@ -70,20 +70,25 @@ function App() {
type: LOAD_COMMUNITY_INFORMATION,
payload: json.data,
});
if (json.data.website) {
const customDomain = json?.data?.website;
if (customDomain) {
dispatch({
type: SET_IS_CUSTOM_SITE,
payload: true,
});
const newURL = 'https://' + json.data.website + pathname.substring(slash);
window.location.href = newURL;
const userIsNotAlreadyOnCustomDomain = !domainsAreTheSame(
customDomain,
window.location.href
);
// Only redirect to custom domain if a community has one, and the user is not already on the custom domain
if (userIsNotAlreadyOnCustomDomain && !IS_LOCAL)
window.location.href = changeToProperURL(customDomain);
}
} else {
setError(json.error);
}
})
.catch((err) => setError(err.message));

}
}, [community, dispatch]);

Expand Down
26 changes: 25 additions & 1 deletion src/components/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ import { STATUS, ACTIONS } from "react-joyride";
import { NONE } from "./Pages/Widgets/MELightDropDown";
import { COPYRIGHT_OPTIONS } from "./Constants";

export function getSubdomainFromURL(url) {
const urlObj = new URL(changeToProperURL(url));
const pathname = urlObj.pathname;
const slash = pathname.indexOf("/", 1);
const subdomain =
slash > 0 ? pathname.substring(1, slash) : pathname.substring(1);
return subdomain;
}

export function domainsAreTheSame(url1, url2) {
try {
let parsedUrl1 = new URL(changeToProperURL(url1));
let parsedUrl2 = new URL(changeToProperURL(url2));

// Remove 'www.' prefix if present
let domain1 = parsedUrl1.hostname.replace(/^www\./, "");
let domain2 = parsedUrl2.hostname.replace(/^www\./, "");

return domain1 === domain2;
} catch (e) {
console.log(e, url1, url2);
return false;
}
}
export const makeStringFromArrOfObjects = (arr, func, separator = ",") => {
if (!func)
return console.warn(
Expand Down Expand Up @@ -706,6 +730,6 @@ export const fetchCopyrightData = (info) => {
copyright_att: info?.copyright_att || "",
underAge: info?.has_children || false,
guardian_info: info?.guardian_info || "",
permission_key: info?.permission_key || COPYRIGHT_OPTIONS.YES.key
permission_key: info?.permission_key || COPYRIGHT_OPTIONS.YES.key,
};
};

0 comments on commit d5d63af

Please sign in to comment.