Skip to content

Commit

Permalink
🗑️ Removed secret content code (#1636)
Browse files Browse the repository at this point in the history
* removed secret content calls

* clean up
  • Loading branch information
babakamyljanovssw authored Nov 11, 2024
1 parent a8b4faf commit 08c3e40
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 183 deletions.
36 changes: 2 additions & 34 deletions src/components/signin/signin.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,11 @@
import { useAuth0 } from '@auth0/auth0-react';
/* eslint-disable no-console */
import React, { useEffect } from 'react';
import React from 'react';
import DropdownIcon from '../dropdown-icon/dropdown-icon';
import {
GetGithubOrganisations,
setUserOrganisation,
} from '../../services/apiService';
import { useAuthService } from '../../services/authService';
import useAppInsights from '../../hooks/useAppInsights';

const SignIn = () => {
const { isAuthenticated, loginWithRedirect, user } = useAuth0();
const { fetchIdToken } = useAuthService();
const { trackException } = useAppInsights();
const { isAuthenticated, loginWithRedirect } = useAuth0();

useEffect(() => {
isAuthenticated ? setUserOrg() : null;
}, []);

const setUserOrg = async () => {
isAuthenticated
? await GetGithubOrganisations(user.nickname)
.then(async (success) => {
const jwt = await fetchIdToken();
success.forEach(async (org) => {
const data = {
OrganisationId: org.id.toString(),
UserId: user.sub,
};
await setUserOrganisation(data, jwt).catch((err) => {
console.error('error: ' + err);
});
});
})
.catch((err) => {
trackException(err, 3);
})
: null;
};
const currentPage =
typeof window !== 'undefined'
? window.location.pathname.split('/').pop()
Expand Down
64 changes: 0 additions & 64 deletions src/services/apiService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const API_URL = process.env.API_BASE_URL + '/api';
const GITHUB_API_PAT = process.env.GITHUB_API_PAT;

/* Bookmarks */

Expand Down Expand Up @@ -46,66 +45,3 @@ export async function RemoveBookmark(data, token) {
});
return response.json();
}

/* Secret Content */

export async function GetGithubOrganisations(username) {
const response = await fetch(
`https://api.github.com/users/${username}/orgs`,
{
method: 'GET',
headers: {
Authorization: `token ${GITHUB_API_PAT}`,
},
}
);
return response.json();
}

export async function GetGithubOrganisationName(orgID) {
const response = await fetch(
`https://api.github.com/organizations/${orgID}`,
{
method: 'GET',
headers: {
Authorization: `token ${GITHUB_API_PAT}`,
},
}
);
return response.json();
}

export async function setUserOrganisation(data, token) {
if (!data || Object.values(data).some((x) => !x)) {
return {
error: true,
message: 'Data is empty or in the wrong format',
};
}
const response = await fetch(`${API_URL}/AddUserOrganisationFunction`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(data),
});
return response.json();
}

export async function GetOrganisations(userId) {
const response = await fetch(
`${API_URL}/GetOrganisationsFunction?user_id=${userId}`
);
return response.json();
}

export async function GetSecretContent(Id, token) {
const response = await fetch(`${API_URL}/GetSecretContentFunction?id=${Id}`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
return response.json();
}
87 changes: 2 additions & 85 deletions src/templates/rule.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {
GetGithubOrganisationName,
GetOrganisations,
GetSecretContent,
} from '../services/apiService';
import { graphql } from 'gatsby';
/* eslint-disable jsx-a11y/anchor-has-content */
import React, { useLayoutEffect, useState } from 'react';
import React from 'react';
import {
faExclamationTriangle,
faPencilAlt,
Expand All @@ -17,12 +12,9 @@ import Breadcrumb from '../components/breadcrumb/breadcrumb';
import Comments from '../components/comments/comments';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import PropTypes from 'prop-types';
import ReactDOMServer from 'react-dom/server';
import RuleSideBar from '../components/rule-side-bar/rule-side-bar';
import formatDistance from 'date-fns/formatDistance';
import { format } from 'date-fns';
import { useAuth0 } from '@auth0/auth0-react';
import { useAuthService } from '../services/authService';
import { faGithub } from '@fortawesome/free-brands-svg-icons';
import useAppInsights from '../hooks/useAppInsights.js';
import Discussion from '../components/discussion/discussion.js';
Expand All @@ -33,82 +25,7 @@ const Rule = ({ data, location }) => {
};
const rule = data.markdownRemark;
const categories = data.categories.nodes;
const { user, isAuthenticated } = useAuth0();
const { fetchIdToken } = useAuthService();
const [hiddenCount, setHiddenCount] = useState(0);
const { trackException, trackEvent } = useAppInsights();

const loadSecretContent = async (userOrgId) => {
const hidden = document.getElementsByClassName('hidden');
if (hidden.length != 0) {
const token = await fetchIdToken();
for (var hiddenBlock of hidden) {
const contentID = hiddenBlock.textContent || hiddenBlock.innerText;
const guid = contentID.substring(0, 36);
const orgID = contentID.substring(37);
if (parseInt(orgID) == parseInt(userOrgId)) {
isAuthenticated && guid
? await GetSecretContent(guid, token)
.then((success) => {
GetGithubOrganisationName(orgID)
.then((nameSuccess) => {
hiddenBlock.innerHTML =
ReactDOMServer.renderToStaticMarkup(
<SecretContent
content={success.content.content}
orgName={nameSuccess?.login ?? 'Your Organisation'}
/>
);
hiddenBlock.className = 'secret-content';
})
.catch((err) => {
trackException(err, 3);
});
})
.catch((err) => {
trackException(err, 3);
})
: null;
}
setHiddenCount(document.getElementsByClassName('hidden').length);
}
}
};

const SecretContent = (props) => {
return (
<>
<div className="secret-content-heading">
<h4>{props.orgName + ' Only: \n'}</h4>
</div>
<div
style={{
wordWrap: 'break-word',
width: 'auto',
}}
dangerouslySetInnerHTML={{ __html: props.content }} //Is this a good idea? JS injection ect
/>
</>
);
};
SecretContent.propTypes = {
content: PropTypes.string,
orgName: PropTypes.string,
};

useLayoutEffect(() => {
isAuthenticated
? GetOrganisations(user.sub)
.then((success) => {
success.organisations.forEach((org) =>
loadSecretContent(org.organisationId)
);
})
.catch((err) => {
trackException(err, 3);
})
: null;
}, [user, isAuthenticated, hiddenCount]);
const { trackEvent } = useAppInsights();

return (
<div>
Expand Down

0 comments on commit 08c3e40

Please sign in to comment.