Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix Redirect Behaviour #538

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const DirectoryNamedWebpackPlugin = require('directory-named-webpack-plugin');
const WebpackAssetsManifest = require('webpack-assets-manifest');
const makePluginData = require('./src/helpers/plugin-data');
const createRewriteMap = require('./src/helpers/createRewriteMap');
const CheckUniqueName = require('./src/helpers/CheckUniqueName');
const chinaHelper = require('./src/helpers/chinaHelper');
const { SkillSort } = require('./src/helpers/skillSort');
const { getViewDataFromCRM } = require('./src/helpers/CRMApi');
Expand Down Expand Up @@ -394,9 +395,19 @@ exports.createPages = async function ({ actions, graphql }) {
};
});

const personFirstNames = people.map((person) => {
return person.dataCRM?.fullName.split(' ')[0].toLowerCase();
});

people.forEach((person) => {
const pathToUse =
person.nicknamePath &&
CheckUniqueName(personFirstNames, person.dataCRM.nickname.toLowerCase())
? person.nicknamePath
: person.path;

actions.createPage({
path: person.nicknamePath ? person.nicknamePath : person.path,
path: pathToUse,
component: require.resolve('./src/templates/person.js'),
context: {
slug: person.slug,
Expand Down Expand Up @@ -470,28 +481,7 @@ exports.onPostBuild = async ({ store, pathPrefix }) => {
];
});

//Fetch existing URL redirects for previous Nicknames
const existingRewrites = await createRewriteMap.getExistingRewrites();
const additionalRewrites = Array.from(existingRewrites).map((rewrite) => {
let existingRedirect = rewrites.find(
(r) => r.fromPath === pathPrefix + '/' + rewrite.fullName
);
if (existingRedirect) {
return {
fromPath: pathPrefix + '/' + rewrite.nickName,
toPath: existingRedirect.toPath,
};
} else {
return {
fromPath: pathPrefix + '/' + rewrite.nickName,
toPath: pathPrefix + '/' + rewrite.fullName,
};
}
});

const allRewrites = rewrites
.concat(alumniRewrites)
.concat(additionalRewrites);
const allRewrites = rewrites.concat(alumniRewrites);

const allRewritesUnique = [
...new Map(allRewrites.map((item) => [item.fromPath, item])).values(),
Expand Down
4 changes: 3 additions & 1 deletion src/components/profile-box/profile-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ProfileBox = ({
profileImages,
sanitisedNickname,
profileAudio,
isUniqueName,
}) => {
const linkRef = useRef();
const [hover, setHover] = useState(false);
Expand Down Expand Up @@ -96,7 +97,7 @@ const ProfileBox = ({
<Link
ref={linkRef}
to={`/${
profile.nickname
profile.nickname && isUniqueName
? sanitisedNickname.toLowerCase()
: sanitisedName.toLowerCase()
}`}
Expand All @@ -122,6 +123,7 @@ ProfileBox.propTypes = {
}),
sanitisedNickname: PropTypes.string,
profileAudio: PropTypes.string,
isUniqueName: PropTypes.bool,
};

export default ProfileBox;
9 changes: 8 additions & 1 deletion src/components/profile-list/profile-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import ProfileBox from 'components/profile-box';
import Distinct from '../../helpers/arrayHelpers';
import RoleSort from '../../helpers/roleSort';
import CheckUniqueName from '../../helpers/CheckUniqueName';

const ProfileList = ({ filteredPeople }) => {
const distinctRoles = filteredPeople
Expand All @@ -13,7 +14,9 @@ const ProfileList = ({ filteredPeople }) => {
const getPeopleInRole = (role) => {
return filteredPeople.filter((p) => p.role === role);
};

const firstNameList = filteredPeople.map((person) => {
return person.profile.fullName.split(' ')[0].toLowerCase();
});
return (
<>
{distinctRoles.map((role, i) => {
Expand All @@ -32,6 +35,10 @@ const ProfileList = ({ filteredPeople }) => {
profileImages={person.profileImages}
sanitisedNickname={person.sanitisedNickname}
profileAudio={person.profileAudio}
isUniqueName={CheckUniqueName(
firstNameList,
person.profile.nickname.toLowerCase()
)}
/>
);
})}
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/CheckUniqueName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const CheckUniqueName = (arr, target) => {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
count++;
}
}
return count <= 1;
};

module.exports = CheckUniqueName;