Skip to content
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

feat: filter url parameters #996

Closed
Closed
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
1 change: 1 addition & 0 deletions src/components/UI/FilterMenu/ChannelFilterMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const ChannelFilterMenu: React.FC = () => {
menuOptions={channelFilterMenuOptions}
onChange={setChannelFilterMenuOption}
translationPath="filter.channels"
parameterName="channel"
/>
);
};
12 changes: 12 additions & 0 deletions src/components/UI/FilterMenu/CommonFilterMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState, MouseEvent, ReactElement } from "react";
import { Menu, MenuItem } from "@mui/material";
import FilterMenuButton from "./FilterMenuButton";
import { useTranslation } from "next-i18next";
import { addParameterToURL, getParameterValueFromURL } from "@/utils/helpers";

export type FilterMenuOption<ValueType> = {
label: string;
Expand All @@ -13,13 +14,15 @@ export type FilterMenuProps<ValueType> = {
initialValue: ValueType;
menuOptions: FilterMenuOption<ValueType>[];
translationPath: string;
parameterName: string;
};

function CommonFilterMenu<ValueType>({
onChange,
initialValue,
menuOptions,
translationPath,
parameterName,
}: FilterMenuProps<ValueType>): ReactElement {
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null);
const [selectedValue, setSelectedValue] = useState<ValueType>(initialValue);
Expand All @@ -37,9 +40,18 @@ function CommonFilterMenu<ValueType>({
const handleMenuItemClick = (event: MouseEvent<HTMLLIElement>) => {
const value = (event.currentTarget.dataset.value ?? null) as ValueType;
setSelectedValue(value);
addParameterToURL(
parameterName,
(event.currentTarget.dataset.value ?? "") as string
);
handleClose();
};

useEffect(() => {
const value = getParameterValueFromURL(parameterName);
setSelectedValue(value as ValueType);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

useEffect(() => {
onChange(selectedValue);
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
9 changes: 9 additions & 0 deletions src/components/UI/FilterMenu/FilterTimeMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from "react";
import { Menu, MenuItem } from "@mui/material";
import FilterMenuButton from "./FilterMenuButton";
import { useTranslation } from "next-i18next";
import { addParameterToURL, getParameterValueFromURL } from "@/utils/helpers";

type TimeOption =
| "last30Minutes"
Expand Down Expand Up @@ -81,6 +82,7 @@ const FilterTimeMenu: React.FC<FilterTimeMenuProps> = ({
const [selectedValue, setSelectedValue] =
useState<TimeOption>("last30Minutes");
const open = Boolean(anchorEl);
const parameterName = "lastTime";

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
Expand All @@ -90,10 +92,17 @@ const FilterTimeMenu: React.FC<FilterTimeMenuProps> = ({
setAnchorEl(null);
};

useEffect(() => {
const value = getParameterValueFromURL(parameterName);
if (!value) return;
setSelectedValue(value as TimeOption);
}, []);

const handleMenuItemClick = (event: React.MouseEvent<HTMLLIElement>) => {
const value = event.currentTarget.dataset.value as TimeOption;

setSelectedValue(value);
addParameterToURL(parameterName, value);

handleClose();
};
Expand Down
10 changes: 10 additions & 0 deletions src/components/UI/FilterMenu/ReasonFilterMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as React from "react";
import { useTranslation } from "next-i18next";
import Checkbox from "@mui/material/Checkbox";
import ListItemText from "@mui/material/ListItemText";
import { addParameterToURL, getParameterValueFromURL } from "@/utils/helpers";

const reasonFilterMenuOptions: string[] = [
"barinma",
Expand All @@ -29,6 +30,7 @@ export const ReasonFilterMenu: React.FC = () => {
const [filterValues, setValues] = React.useState<string[]>(
reasonFilterMenuOptions
);
const parameterName = "reasons";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here as above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't understand :( Is there a problem related to line 33?


const handleChange = (event: SelectChangeEvent<any>) => {
const {
Expand All @@ -38,8 +40,16 @@ export const ReasonFilterMenu: React.FC = () => {
typeof value === "string" ? value.split(",") : value;

setValues(selectedReasons);
addParameterToURL(parameterName, value);
};

React.useEffect(() => {
const value = getParameterValueFromURL(parameterName);
if (value) {
setValues(value.split(","));
}
}, []);

React.useEffect(() => {
setReasoningFilterMenuOption(filterValues.join(","));
}, [filterValues, setReasoningFilterMenuOption]);
Expand Down
10 changes: 5 additions & 5 deletions src/components/UI/Map/LeafletMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ const MapEvents = () => {
const _lng = localCoordinates.getCenter().lng;
const _zoomLevel = zoomLevel;

const locationWithZoomLevel = new URLSearchParams();
locationWithZoomLevel.append("lat", _lat.toString());
locationWithZoomLevel.append("lng", _lng.toString());
locationWithZoomLevel.append("zoom", _zoomLevel.toString());
const locationWithZoomLevel = new URLSearchParams(window.location.search);
locationWithZoomLevel.set("lat", _lat.toString());
locationWithZoomLevel.set("lng", _lng.toString());
locationWithZoomLevel.set("zoom", _zoomLevel.toString());
if (id) {
locationWithZoomLevel.append("id", id.toString());
locationWithZoomLevel.set("id", id.toString());
}
const query = locationWithZoomLevel.toString();
safeSetLocalStorage(localStorageKeys.coordinatesURL, query);
Expand Down
13 changes: 12 additions & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,15 @@ const capitalize = (str: string) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};

export { capitalize, isNaN };
function addParameterToURL(param: string, value: string) {
let url = new URL(window.location.href);
url.searchParams.set(param, value);
window.history.pushState({}, "", url);
}

function getParameterValueFromURL(param: string) {
let url = new URL(window.location.href);
return url.searchParams.get(param);
}

export { capitalize, isNaN, addParameterToURL, getParameterValueFromURL };