Skip to content

Commit

Permalink
Sort contributions by election. A new approach to #435
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeubell committed Dec 2, 2023
1 parent 334d280 commit 06fe538
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions src/components/contributions-table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ContributionsTable extends React.Component {
order: 1,
column: 'amount',
},
iec: iec
iec,
};
}

Expand All @@ -47,7 +47,7 @@ class ContributionsTable extends React.Component {
attributes.contributions.value : []);
const iec = attributes.iec.value === 'true';
return {
contributions, iec
contributions, iec,
};
}

Expand Down Expand Up @@ -122,50 +122,55 @@ class ContributionsTable extends React.Component {
};

function parseElectionName(input) {
const [name, ...rest] = input.split("-");
const [ename, ...rest] = input.split('-');

if (rest.length === 1) {
// If there is only one element after the split, assume it's the year
return { name, month: "november", year: rest[0] };
return { ename, month: 'november', year: rest[0] };
} else if (rest.length >= 2) {
// If there are two or more elements, assume the last is the year and the second-to-last is the month
return { name, month: rest[rest.length - 2], year: rest[rest.length - 1] };
} else {
// Handle the case where there are no elements after the split
return { name, month: "november", year: undefined };
// If there are two or more elements, assume the last is the year
// and the second-to-last is the month
return { ename, month: rest[rest.length - 2], year: rest[rest.length - 1] };
}
// Handle the case where there are no elements after the split
return { ename, month: 'november', year: undefined };
}

const electionCompare = (x, y) => {
const election1 = parseElectionName(x);
const election2 = parseElectionName(y);

if (election1.name > election2.name)
if (election1.name > election2.name) {
return 1;
if (election1.name < election2.name)
}
if (election1.name < election2.name) {
return -1;
}

if (election1.year > election2.year)
if (election1.year > election2.year) {
return 1;
if (election1.year < election2.year)
}
if (election1.year < election2.year) {
return -1;
}

const monthOrder = {
january: 0,
february: 1,
march: 2,
april: 3,
may: 4,
june: 5,
july: 6,
august: 7,
september: 8,
october: 9,
november: 10,
december: 11,
};

return monthOrder[election1.month] - monthOrder[election2.month];
}
january: 0,
february: 1,
march: 2,
april: 3,
may: 4,
june: 5,
july: 6,
august: 7,
september: 8,
october: 9,
november: 10,
december: 11,
};

return monthOrder[election1.month] - monthOrder[election2.month];
};

const difference = (a, b) => {
if (sortOnElectionName && a.election_name !== b.election_name) {
Expand Down Expand Up @@ -264,12 +269,13 @@ class ContributionsTable extends React.Component {
<div>
<input className="filter" value={this.state.filterField} onChange={updateFilter} type="text" placeholder="Type to filter contributions" />
{ iec &&
<label>
<label htmlFor="ename">
<input
id="ename"
type="checkbox"
checked={this.state.sortOnElectionName}
onChange={() =>
this.setState((prevState) => ({
this.setState(prevState => ({
sortOnElectionName: !prevState.sortOnElectionName,
}))
}
Expand Down

0 comments on commit 06fe538

Please sign in to comment.