Skip to content

Instantly share code, notes, and snippets.

@Smnthjm08
Created August 23, 2024 07:52
Show Gist options
  • Save Smnthjm08/581a9fa1cfad6f90a152e517382d672f to your computer and use it in GitHub Desktop.
Save Smnthjm08/581a9fa1cfad6f90a152e517382d672f to your computer and use it in GitHub Desktop.

Certainly. Here are the relevant code segments responsible for filtering in the MetrcTags component:

  1. Filter state and initialization:
const [selectedFilter, setSelectedFilter] = useState('All');
  1. Fetch data with filter:
const fetchData = async (page = 1) => {
  setLoading(true);
  setError(null);
  try {
    let url = `metrc-tag/?limit=${limit}&page=${page}`;
    if (selectedFilter !== 'All') {
      url += `&farm_id=${selectedFilter}`;
    }
    const metrcResponse = await APIService.fetchInstance(url);
    // ... rest of the function
  } catch (err) {
    // ... error handling
  } finally {
    setLoading(false);
  }
};
  1. Handle filter change:
const handleFilterChange = (farmId) => {
  if (farmId === 'All' || farmId === selectedFilter) {
    setSelectedFilter('All');
  } else {
    setSelectedFilter(farmId);
  }
};
  1. Filter chips rendering:
const sChipsProps = (farm) => ({
  key: farm.id,
  label: farm.name,
  variant: selectedFilter === farm.id ? 'filled' : 'outlined',
  onClick: () => handleFilterChange(farm.id),
  clickable: true,
  className: `${roomDetailClasses.chip} ${currDarkThemeClass}`
});

const metrcTagFarmChipsList = (
  <Stack direction='row' spacing={1}>
    <Chip
      key="All"
      label="All"
      variant={selectedFilter === 'All' ? 'filled' : 'outlined'}
      onClick={() => handleFilterChange('All')}
      clickable
      className={`${roomDetailClasses.chip} ${currDarkThemeClass}`}
    />
    {farms.map(farm => (
      <Chip key={farm.id} {...sChipsProps(farm)} />
    ))}
  </Stack>
);
  1. Filter section in the JSX:
<Stack {...metrcTagFilterProps}>
  <Typography {...metrcTagFilterTypoProps}>Show by Farm:</Typography>
  {metrcTagFarmChipsList}
</Stack>

These code segments work together to implement the filtering functionality in the MetrcTags component. The filter allows users to select a specific farm or view all tags, updating the displayed data accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment