Skip to content

Commit 68186e2

Browse files
authored
Merge pull request #1420 from Northeastern-Electric-Racing/536-cr-creation-sort-the-wbs-dropdown
536 cr creation sort the wbs dropdown
2 parents 6c3d45c + d478005 commit 68186e2

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/frontend/src/pages/CreateChangeRequestPage/CreateChangeRequestView.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import NERFailButton from '../../components/NERFailButton';
3636
import NERSuccessButton from '../../components/NERSuccessButton';
3737
import { wbsNamePipe } from '../../utils/pipes';
3838
import PageLayout from '../../components/PageLayout';
39+
import { wbsNumComparator } from 'shared/src/validate-wbs';
3940

4041
interface CreateChangeRequestViewProps {
4142
wbsNum: string;
@@ -103,6 +104,7 @@ const CreateChangeRequestsView: React.FC<CreateChangeRequestViewProps> = ({
103104
if (isError) return <ErrorPage message={error?.message} />;
104105

105106
const projectOptions: { label: string; id: string }[] = [];
107+
106108
const wbsDropdownOptions: { label: string; id: string }[] = [];
107109

108110
projects.forEach((project: Project) => {
@@ -122,6 +124,8 @@ const CreateChangeRequestsView: React.FC<CreateChangeRequestViewProps> = ({
122124
});
123125
});
124126

127+
wbsDropdownOptions.sort((wbsNum1, wbsNum2) => wbsNumComparator(wbsNum1.id, wbsNum2.id));
128+
125129
const wbsAutocompleteOnChange = (
126130
_event: React.SyntheticEvent<Element, Event>,
127131
value: { label: string; id: string } | null
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { wbsNumComparator } from 'shared';
2+
import '@testing-library/jest-dom/extend-expect';
3+
4+
describe('wbsNumComparator', () => {
5+
it('should correctly compare two WBS Numbers', () => {
6+
expect(wbsNumComparator('1.1.3', '1.3.4')).toBe(-1);
7+
expect(wbsNumComparator('1.2.1', '1.2.1')).toBe(0);
8+
expect(wbsNumComparator('1.3.4', '1.1.3')).toBe(1);
9+
expect(wbsNumComparator('2.3.4', '1.1.3')).toBe(1);
10+
expect(wbsNumComparator('1.3.4', '2.1.3')).toBe(-1);
11+
});
12+
});

src/shared/src/validate-wbs.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55

66
import { WbsNumber } from './types/project-types';
77

8+
/**
9+
* Compares two wbs numbers in ascending order
10+
*
11+
* @param wbsNum1 WBS number to compare
12+
* @param wbsNum2 WBS number to compare
13+
*/
14+
export const wbsNumComparator = (wbsNum1: string, wbsNum2: string) => {
15+
const wbsParts1 = wbsNum1.split('.').map(Number);
16+
const wbsParts2 = wbsNum2.split('.').map(Number);
17+
18+
for (let i = 0; i < wbsParts1.length; i++) {
19+
if (wbsParts1[i] < wbsParts2[i]) {
20+
return -1;
21+
} else if (wbsParts1[i] > wbsParts2[i]) {
22+
return 1;
23+
}
24+
}
25+
26+
return 0;
27+
};
28+
829
/**
930
* Ensure the provided wbsNum is a valid Work Breakdown Structure Number
1031
*

0 commit comments

Comments
 (0)