Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #347 from newrelic/clark/add-nerdgraph-call
Browse files Browse the repository at this point in the history
Clark/add nerdgraph call
  • Loading branch information
LizBaker committed Dec 20, 2022
2 parents bb13688 + acd6eb3 commit a226a02
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions src/components/InstallButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,32 @@ const checkUtmParameters = (parameters) => {
};

/**
* Method which returns `false` if current user is 'new'. Returns `true` if user is a returning user.
* @returns {Boolean}
* Makes a call to nerdgraph to see if the user is logged in via the NR cookie hitting Service Gateway
* @returns {Promise<Boolean>}
*/
const checkIfReturningUser = () => {
return Boolean(Cookies.get('ajs_user_id'));
};
const checkIfUserLoggedIn = () =>
fetch('https://nerd-graph.service.newrelic.com/graphql', {
method: 'POST',
credentials: 'include',
redirect: 'error',
headers: {
'Content-Type': 'application/json',
'NewRelic-Requesting-Services': 'io-website',
},
body: JSON.stringify({
query: `{
actor {
user {
name
}
}
}`,
}),
})
.then((res) => {
return res.ok;
})
.catch(() => false);

/**
* @param {String} id
Expand All @@ -61,14 +81,14 @@ const createInstallLink = (
nerdletId,
hasGuidedInstall,
hasUtmParameters,
isReturningUser,
isLoggedIn,
parameters
) => {
const platformUrl = hasGuidedInstall
? getGuidedInstallStackedNr1Url(nerdletId)
: getPackNr1Url(id, nerdletId);

const installUrl = new URL(isReturningUser ? platformUrl : SIGNUP_LINK);
const installUrl = new URL(isLoggedIn ? platformUrl : SIGNUP_LINK);
if (parameters) {
parameters.forEach((value, key) => {
installUrl.searchParams.set(key, value);
Expand Down Expand Up @@ -103,24 +123,24 @@ const InstallButton = ({

const tessen = useTessen();

const [parameters, setParameters] = useState();
useEffect(() => {
if (location.search) {
setParameters(new URLSearchParams(location.search));
}
}, [location.search, setParameters]);
const parameters = new URLSearchParams(location.search);

const hasGuidedInstall =
hasInstallableComponent &&
quickstart.installPlans.length === 1 &&
quickstart.installPlans[0].id.includes('guided-install');

const [installUrl, setInstallUrl] = useState(SIGNUP_LINK);
const [isLoggedIn, setLoggedIn] = useState();
useEffect(() => {
setInstallUrl(url);

// IIFE - used to make a call to nerdgraph to set state for is a user is logged in.
// We're using an IIFE here because the callback passed
// to `useEffect` can't be async.
(async () => {
const loggedIn = await checkIfUserLoggedIn();
setLoggedIn(loggedIn);
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [checkIfUserLoggedIn]);

// If there is nothing to install AND no documentation, don't show this button.
if (!hasInstallableComponent && !hasComponent(quickstart, 'documentation')) {
Expand All @@ -137,13 +157,13 @@ const InstallButton = ({
const hasUtmParameters = checkUtmParameters(parameters);
// If we have an install-able component, generate a URL. Otherwise, link to the
// first documentation supplied.
const url = hasInstallableComponent
const installUrl = hasInstallableComponent
? createInstallLink(
quickstart.id,
nerdletId,
hasGuidedInstall,
hasUtmParameters,
checkIfReturningUser(),
isLoggedIn,
parameters
)
: quickstart.documentation[0].url;
Expand Down Expand Up @@ -175,7 +195,7 @@ const InstallButton = ({
quickstartId: quickstart.id,
quickstartUrl: quickstart.packUrl,
super_tiles_treatment: treatment,
urlParameters: Object.entries([...parameters]),
urlParameters: parameters ? Object.entries([...parameters]) : null,
partner: isNRPartner(quickstart.keywords),
quickstartButtonText: hasInstallableComponent
? 'Install quickstart'
Expand Down

0 comments on commit a226a02

Please sign in to comment.