Skip to content

Reduce duplicate code in filter cell #1367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/components/events/partials/EventsDateCell.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { useTranslation } from "react-i18next";
import { editFilterValue } from "../../../slices/tableFilterSlice";
import React from "react";
import { loadEventsIntoTable } from "../../../thunks/tableThunks";
import { fetchEvents } from "../../../slices/eventSlice";
import { Event } from "../../../slices/eventSlice";
Expand Down
38 changes: 12 additions & 26 deletions src/components/events/partials/EventsLocationCell.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getFilters } from "../../../selectors/tableFilterSelectors";
import { editFilterValue } from "../../../slices/tableFilterSlice";
import { loadEventsIntoTable } from "../../../thunks/tableThunks";
import { useAppDispatch, useAppSelector } from "../../../store";
import { fetchEvents } from "../../../slices/eventSlice";
import { Event } from "../../../slices/eventSlice";
import ButtonLikeAnchor from "../../shared/ButtonLikeAnchor";
import FilterCell from "../../shared/FilterCell";

/**
* This component renders the location cells of events in the table view
Expand All @@ -14,29 +11,18 @@ const EventsLocationCell = ({
}: {
row: Event
}) => {
const dispatch = useAppDispatch();

const filterMap = useAppSelector(state => getFilters(state, "events"));

// Filter with value of current cell
const addFilter = (location: string) => {
const filter = filterMap.find(({ name }) => name === "location");
if (filter) {
dispatch(editFilterValue({ filterName: filter.name, value: location, resource: "events" }));
dispatch(fetchEvents());
dispatch(loadEventsIntoTable());
}
};

return (
// Link template for location of event
<ButtonLikeAnchor
onClick={() => addFilter(row.location)}
className={"crosslink"}
tooltipText={"EVENTS.EVENTS.TABLE.TOOLTIP.LOCATION"}
>
{row.location}
</ButtonLikeAnchor>
<FilterCell
resource={"events"}
filterName={"location"}
filterItems={[{
filterValue: row.location,
children: row.location,
cellTooltipText: "EVENTS.EVENTS.TABLE.TOOLTIP.LOCATION",
}]}
fetchResource={fetchEvents}
loadResourceIntoTable={loadEventsIntoTable}
/>
);
};

Expand Down
15 changes: 9 additions & 6 deletions src/components/events/partials/EventsPresentersCell.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { loadEventsIntoTable } from "../../../thunks/tableThunks";
import { fetchEvents } from "../../../slices/eventSlice";
import { Event } from "../../../slices/eventSlice";
import MultiValueCell from "../../shared/MultiValueCell";
import FilterCell from "../../shared/FilterCell";

/**
* This component renders the presenters cells of events in the table view
Expand All @@ -12,13 +12,16 @@ const EventsPresentersCell = ({
row: Event
}) => {
return (
<MultiValueCell
resource="events"
values={row.presenters}
filterName="presentersBibliographic"
<FilterCell
resource={"events"}
filterName={"presentersBibliographic"}
filterItems={row.presenters.map(presenter => ({
filterValue: presenter,
children: presenter,
cellTooltipText: "EVENTS.EVENTS.TABLE.TOOLTIP.PRESENTER",
}))}
fetchResource={fetchEvents}
loadResourceIntoTable={loadEventsIntoTable}
tooltipText="EVENTS.EVENTS.TABLE.TOOLTIP.PRESENTER"
/>
);
};
Expand Down
41 changes: 12 additions & 29 deletions src/components/events/partials/EventsSeriesCell.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getFilters } from "../../../selectors/tableFilterSelectors";
import { editFilterValue } from "../../../slices/tableFilterSlice";
import { loadEventsIntoTable } from "../../../thunks/tableThunks";
import { useAppDispatch, useAppSelector } from "../../../store";
import { fetchEvents } from "../../../slices/eventSlice";
import { Event } from "../../../slices/eventSlice";
import ButtonLikeAnchor from "../../shared/ButtonLikeAnchor";
import FilterCell from "../../shared/FilterCell";

/**
* This component renders the series cells of events in the table view
Expand All @@ -14,33 +11,19 @@ const EventsSeriesCell = ({
}: {
row: Event
}) => {
const dispatch = useAppDispatch();

const filterMap = useAppSelector(state => getFilters(state, "events"));

// Filter with value of current cell
const addFilter = async (seriesId: string) => {
const filter = filterMap.find(({ name }) => name === "series");
if (filter) {
dispatch(editFilterValue({ filterName: filter.name, value: seriesId, resource: "events" }));
await dispatch(fetchEvents());
dispatch(loadEventsIntoTable());
}
};

return (
row.series ? (
// Link template for series of event
<ButtonLikeAnchor
onClick={() => row.series
? addFilter(row.series.id)
: console.error("Tried to sort by a series, but the series did not exist.")
}
className={"crosslink"}
tooltipText={"EVENTS.EVENTS.TABLE.TOOLTIP.SERIES"}
>
{row.series.title}
</ButtonLikeAnchor>
<FilterCell
resource={"events"}
filterName={"series"}
filterItems={[{
filterValue: row.series.id,
children: row.series.title,
cellTooltipText: "EVENTS.EVENTS.TABLE.TOOLTIP.SERIES",
}]}
fetchResource={fetchEvents}
loadResourceIntoTable={loadEventsIntoTable}
/>
)
: <></>
);
Expand Down
46 changes: 0 additions & 46 deletions src/components/events/partials/EventsTechnicalDateCell.tsx

This file was deleted.

20 changes: 6 additions & 14 deletions src/components/events/partials/SeriesTitleCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { setSpecificEventFilter } from "../../../slices/tableFilterSlice";
import { useNavigate } from "react-router";
import { useAppDispatch } from "../../../store";
import { Series } from "../../../slices/seriesSlice";
import BaseButton from "../../shared/BaseButton";
import RedirectCell from "../../shared/RedirectCell";

/**
* This component renders the title cells of series in the table view
Expand All @@ -12,23 +12,15 @@ const SeriesTitleCell = ({
}: {
row: Series
}) => {
const dispatch = useAppDispatch();
const navigate = useNavigate();

const redirectToEvents = async (seriesId: string) => {
// set the series filter value of events to series title
await dispatch(setSpecificEventFilter({ filter: "series", filterValue: seriesId }));
navigate("/events/events");
};

return (
<BaseButton
className="button-like-anchor crosslink"
<RedirectCell
path={"/events/events"}
filterName={"series"}
filterValue={row.id}
tooltipText={"EVENTS.SERIES.TABLE.TOOLTIP.SERIES"}
onClick={() => redirectToEvents(row.id)}
>
{row.title}
</BaseButton>
</RedirectCell>
);
};

Expand Down
23 changes: 6 additions & 17 deletions src/components/recordings/partials/RecordingsNameCell.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { useNavigate } from "react-router";
import { setSpecificEventFilter } from "../../../slices/tableFilterSlice";
import { useAppDispatch } from "../../../store";
import { Recording } from "../../../slices/recordingSlice";
import BaseButton from "../../shared/BaseButton";
import RedirectCell from "../../shared/RedirectCell";

/**
* This component renders the name cells of recordings in the table view
Expand All @@ -12,23 +9,15 @@ const RecordingsNameCell = ({
}: {
row: Recording
}) => {
const dispatch = useAppDispatch();
const navigate = useNavigate();

const redirectToEvents = async (locationName: string) => {
// set the location filter value of events to location name
await dispatch(setSpecificEventFilter({ filter: "location", filterValue: locationName }));
navigate("/events/events");
};

return (
<BaseButton
className="button-like-anchor crosslink"
<RedirectCell
path={"/events/events"}
filterName={"location"}
filterValue={row.name}
tooltipText={"RECORDINGS.RECORDINGS.TABLE.TOOLTIP.NAME"}
onClick={() => redirectToEvents(row.name)}
>
{row.name}
</BaseButton>
</RedirectCell>
);
};

Expand Down
61 changes: 61 additions & 0 deletions src/components/shared/FilterCell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { getFilters } from "../../selectors/tableFilterSelectors";
import { editFilterValue } from "../../slices/tableFilterSlice";
import { AppThunk, useAppDispatch, useAppSelector } from "../../store";
import { Resource } from "../../slices/tableSlice";
import { AsyncThunk } from "@reduxjs/toolkit";
import { ParseKeys } from "i18next";
import { ReactNode } from "react";
import BaseButton from "./BaseButton";
import ButtonLikeAnchor from "./ButtonLikeAnchor";

/**
* This component renders a table cell with one or more clickable items
* where clicking the items will set a filter
*/
const FilterCell = <T, >({
resource,
filterName,
filterItems,
fetchResource,
loadResourceIntoTable,
}: {
resource: Resource
filterName: string
filterItems: {
filterValue: string
children: ReactNode
cellTooltipText?: ParseKeys
}[]
fetchResource: AsyncThunk<T, void, any>
loadResourceIntoTable: () => AppThunk,
}) => {
const dispatch = useAppDispatch();

const filterMap = useAppSelector(state => getFilters(state, resource));

// Filter with value of current cell
const addFilter = async (filterValue: string) => {
const filter = filterMap.find(({ name }) => name === filterName);
if (filter) {
dispatch(editFilterValue({ filterName: filter.name, value: filterValue, resource }));
await dispatch(fetchResource());
dispatch(loadResourceIntoTable());
}
};

return (
// Link template for location of event
filterItems.map((item, key) => (
<ButtonLikeAnchor
key={key}
onClick={() => addFilter(item.filterValue)}
className={"crosslink"}
tooltipText={item.cellTooltipText}
>
{item.children}
</ButtonLikeAnchor>
))
);
};

export default FilterCell;
44 changes: 44 additions & 0 deletions src/components/shared/RedirectCell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useNavigate } from "react-router";
import { setSpecificEventFilter } from "../../slices/tableFilterSlice";
import { useAppDispatch } from "../../store";
import ButtonLikeAnchor from "./ButtonLikeAnchor";
import { ParseKeys } from "i18next";
import { ReactNode } from "react";

/**
* This component renders the name cells of recordings in the table view
*/
const RedirectCell = ({
path,
filterName,
filterValue,
tooltipText,
children,
}: {
path: string
filterName: string
filterValue: string
tooltipText?: ParseKeys
children: ReactNode
}) => {
const dispatch = useAppDispatch();
const navigate = useNavigate();

const redirectToResource = async (filterValue: string) => {
// Set filter before redirecting
await dispatch(setSpecificEventFilter({ filter: filterName, filterValue }));
navigate(path);
};

return (
<ButtonLikeAnchor
className="crosslink"
tooltipText={tooltipText}
onClick={() => redirectToResource(filterValue)}
>
{children}
</ButtonLikeAnchor>
);
};

export default RedirectCell;
2 changes: 0 additions & 2 deletions src/configs/tableConfigs/eventsTableMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import EventsDateCell from "../../components/events/partials/EventsDateCell";
import EventsPresentersCell from "../../components/events/partials/EventsPresentersCell";
import EventsSeriesCell from "../../components/events/partials/EventsSeriesCell";
import EventsStatusCell from "../../components/events/partials/EventsStatusCell";
import EventsTechnicalDateCell from "../../components/events/partials/EventsTechnicalDateCell";
import PublishedCell from "../../components/events/partials/PublishedCell";
import EventsLocationCell from "../../components/events/partials/EventsLocationCell";
import EventsEndCell from "../../components/events/partials/EventsEndCell";
Expand All @@ -23,7 +22,6 @@ export const eventsTemplateMap = {
EventsPresentersCell: EventsPresentersCell,
EventsSeriesCell: EventsSeriesCell,
EventsStatusCell: EventsStatusCell,
EventsTechnicalDateCell: EventsTechnicalDateCell,
PublishedCell: PublishedCell,
EventsNotesCell: EventsNotesCell,
};