Skip to content

Commit

Permalink
feat: add preselected filters
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinan029 committed Jan 23, 2025
1 parent d7e8a8d commit b3ee288
Showing 1 changed file with 78 additions and 3 deletions.
81 changes: 78 additions & 3 deletions src/components/learner-credit-management/MultipleBudgetsPicker.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,94 @@
import React, { useMemo } from 'react';
import React, { useMemo, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import {
DataTable,
CardView,
TextFilter,
CheckboxFilter,
Row,
Col,
Form,
Stack,
Badge,
FormLabel

Check failure on line 12 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

Missing trailing comma
} from '@openedx/paragon';
import groupBy from 'lodash/groupBy';

import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import BudgetCard from './BudgetCard';
import { getBudgetStatus, getTranslatedBudgetStatus, orderBudgets } from './data/utils';

let lastId = 0;

const newId = (prefix = 'id') => {
lastId += 1;
return `${prefix}${lastId}`;
};

function useFirstRender() {
const ref = useRef(true);
const firstRender = ref.current;
ref.current = false;
return firstRender;
}

function BudgetCheckboxFilter({

Check failure on line 34 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

Function component is not an arrow function
column: {

Check failure on line 35 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

'column' is missing in props validation
filterValue, setFilter, Header, filterChoices, getHeaderProps,

Check failure on line 36 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

'column.filterValue' is missing in props validation

Check failure on line 36 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

'column.setFilter' is missing in props validation

Check failure on line 36 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

'column.Header' is missing in props validation

Check failure on line 36 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

'column.filterChoices' is missing in props validation

Check failure on line 36 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

'column.getHeaderProps' is missing in props validation
},
}) {

Check failure on line 38 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

Block must not be padded by blank lines

let checkedBoxes = filterValue || [];

const isFirstRender = useFirstRender();

useEffect(() => {
console.log(checkedBoxes);

Check warning on line 45 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
if (isFirstRender) {
checkedBoxes = ["Active"];

Check failure on line 47 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

Assignments to the 'checkedBoxes' variable from inside React Hook useEffect will be lost after each render. To preserve the value over time, store it in a useRef Hook and keep the mutable value in the '.current' property. Otherwise, you can move this variable directly inside useEffect
} else {
checkedBoxes = filterValue;
}
setFilter(checkedBoxes);
}, [filterValue])

const ariaLabel = useRef(newId(`checkbox-filter-label-${getHeaderProps().key}-`));

const changeCheckbox = (value) => {
if (checkedBoxes.includes(value)) {
const newCheckedBoxes = checkedBoxes.filter((val) => val !== value);
return setFilter(newCheckedBoxes);
}
checkedBoxes.push(value);
return setFilter(checkedBoxes);
};
const headerBasedId = useMemo(() => `checkbox-filter-check-${getHeaderProps().key}-`, [getHeaderProps]);

return (
<Form.Group role="group" aria-labelledby={ariaLabel.current}>
<FormLabel id={ariaLabel.current} className="pgn__checkbox-filter-label">{Header}</FormLabel>
<Form.CheckboxSet name={Header} value={checkedBoxes}
>
{filterChoices.map(({ name, number, value }) => {
console.log(name, checkedBoxes.includes(value), number)

Check warning on line 72 in src/components/learner-credit-management/MultipleBudgetsPicker.jsx

View workflow job for this annotation

GitHub Actions / tests

Unexpected console statement
return (
<Form.Checkbox
key={`${headerBasedId}${name}`}
value={name}
checked={checkedBoxes.includes(value)}
onChange={() => changeCheckbox(value)}
aria-label={name}
>
<Stack direction="horizontal" gap={2}>
{name} {number !== undefined && <Badge variant="light">{number}</Badge>}
</Stack>
</Form.Checkbox>
)
})}
</Form.CheckboxSet>
</Form.Group>
);
}

const MultipleBudgetsPicker = ({
budgets,
enterpriseUUID,
Expand Down Expand Up @@ -88,7 +163,7 @@ const MultipleBudgetsPicker = ({
}),
accessor: 'status',
filter: 'includesValue',
Filter: CheckboxFilter,
Filter: BudgetCheckboxFilter,
filterChoices: reducedChoices,
},
]}
Expand Down

0 comments on commit b3ee288

Please sign in to comment.