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

refactor: use URLSearchParams to manage params #38

Merged
merged 1 commit into from
Aug 16, 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
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
export const COURSE = 'course';
export const BOOT_CAMP = 'boot_camp';
export const EXECUTIVE_EDUCATION = 'executive_education';
export const DEGREE = '2U_degrees';
export const DEGREE = '2U_degree';
export const PROGRAM = 'program';

export const COURSE_PARAM = 'course';
export const BOOT_CAMP_PARAM = 'boot_camp';
export const EXECUTIVE_EDUCATION_PARAM = 'executive_education';
export const DEGREE_PARAM = '2U_degrees';
export const PROGRAM_PARAM = 'program';

// This array is used to determine the validity of product types as they are passed through the query string
export const productTypeNames = [
DEGREE,
Expand All @@ -12,3 +18,11 @@ export const productTypeNames = [
PROGRAM,
COURSE,
];

export const productTypeParams = {};

productTypeParams[COURSE_PARAM] = COURSE;
productTypeParams[BOOT_CAMP_PARAM] = BOOT_CAMP;
productTypeParams[EXECUTIVE_EDUCATION_PARAM] = EXECUTIVE_EDUCATION;
productTypeParams[DEGREE_PARAM] = DEGREE;
productTypeParams[PROGRAM_PARAM] = PROGRAM;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useLocation } from 'react-router-dom';
import { productTypeNames as acceptedProductTypes, COURSE } from './constants';
import { productTypeParams, COURSE } from './constants';

const defaultSetting = [COURSE];

Expand All @@ -13,27 +13,18 @@ const defaultSetting = [COURSE];
export const useProductTypes = () => {
const { search } = useLocation();
const checkedTypes = [];
const searchParams = new URLSearchParams(search);
const productTypes = searchParams.get('product_types');

if (search) {
// remove the "?" and split the query string at "="
const splitString = search.slice(1).split('&')[0].split('=');

// if the key is not "product_types", use a default setting
if (splitString[0] !== 'product_types') {
return defaultSetting;
}

// split productTypes string into an array at ","
const queryProductTypes = splitString[1]?.split(',');

if (productTypes) {
const productTypesArray = productTypes.split(',');
// compare each product type from the query string with a list of accepted product types
queryProductTypes.forEach(productType => {
if (acceptedProductTypes.includes(productType)) {
checkedTypes.push(productType);
productTypesArray.forEach(productParam => {
if (productParam in productTypeParams) {
checkedTypes.push(productTypeParams[productParam]);
}
});
}

// if no types were set, use default setting
return checkedTypes.length > 0 ? checkedTypes : defaultSetting;
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('view-results', () => {

describe('user interface', () => {
beforeAll(() => {
useProductTypes.mockImplementation(() => (['2U_degrees', 'boot_camp', 'executive_education', 'program', 'course']));
useProductTypes.mockImplementation(() => (['2U_degree', 'boot_camp', 'executive_education', 'program', 'course']));
});

beforeEach(async () => {
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('view-results', () => {
page: 'skills_builder',
label: 'MONS101',
position: 0,
product_line: '2U_degrees',
product_line: '2U_degree',
selected_recommendations: {
job_id: 0,
job_name: 'Prospector',
Expand Down
2 changes: 1 addition & 1 deletion src/skills-builder/test/__mocks__/jobSkills.mockData.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const mockData = {
{ title: 'Mining with the Mons', label: 'MONS101', position: 0 },
{ title: 'The Art of Warren Upkeep', label: 'WAR101', position: 1 },
],
'2U_degrees': [
'2U_degree': [
{ title: 'Mining with the Mons', label: 'MONS101', position: 0 },
{ title: 'The Art of Warren Upkeep', label: 'WAR101', position: 1 },
],
Expand Down