Skip to content

Commit

Permalink
Refactor reducers into a common reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
CaitBarnard committed Nov 26, 2024
1 parent 00c05a1 commit d83550c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 37 deletions.
48 changes: 12 additions & 36 deletions front_end/src/Apps/Payroll.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export default function Payroll() {
}
}

function handleTogglePayPeriods(employeeNo, index, enabled) {
dispatch({ type: "updatePayPeriods", employeeNo, index, enabled });
function handleTogglePayPeriods(id, index, enabled) {
dispatch({ type: "updatePayPeriods", id, index, enabled });
}

function handleToggleVacancyPayPeriods(id, index, enabled) {
Expand Down Expand Up @@ -117,54 +117,30 @@ export default function Payroll() {
);
}

function payrollReducer(payroll, action) {
const positionReducer = (data, action) => {
switch (action.type) {
case "fetched": {
return action.data;
}
case "updatePayPeriods": {
return payroll.map((employeeRow) => {
if (employeeRow.employee_no == action.employeeNo) {
const updatedPayPeriods = employeeRow.pay_periods.map(
(period, index) => {
if (index + 1 >= action.index + 1) {
return !action.enabled;
}
return period;
}
);
return {
...employeeRow,
pay_periods: updatedPayPeriods,
};
}
return employeeRow;
});
}
}
}

function vacanciesReducer(vacancies, action) {
switch (action.type) {
case "fetched": {
return action.data;
}
case "updatePayPeriods": {
return vacancies.map((vacancy) => {
if (vacancy.id == action.id) {
const updatedPayPeriods = vacancy.pay_periods.map((period, index) => {
return data.map((row) => {
if (row.id == action.id) {
const updatedPayPeriods = row.pay_periods.map((period, index) => {
if (index + 1 >= action.index + 1) {
return !action.enabled;
}
return period;
});
return {
...vacancy,
...row,
pay_periods: updatedPayPeriods,
};
}
return vacancy;
return row;
});
}
}
}
};

const payrollReducer = (data, action) => positionReducer(data, action);
const vacanciesReducer = (data, action) => positionReducer(data, action);
2 changes: 1 addition & 1 deletion front_end/src/Components/EditPayroll/EmployeeRow/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const EmployeeRow = ({ row, onTogglePayPeriods }) => {
<td className="govuk-table__cell">{row.assignment_status}</td>
<PayPeriods
row={row}
id={row.employee_no}
id={row.id}
onTogglePayPeriods={onTogglePayPeriods}
/>
</tr>
Expand Down
1 change: 1 addition & 0 deletions payroll/services/payroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def get_payroll_data(
)
for obj in qs:
yield EmployeePayroll(
id=obj.pk,
name=obj.get_full_name(),
grade=obj.grade.pk,
employee_no=obj.employee_no,
Expand Down

0 comments on commit d83550c

Please sign in to comment.