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

Refactored removeModalIdFromUrl to use common URL functions #1143

Merged
merged 1 commit into from
May 7, 2024
Merged
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
14 changes: 7 additions & 7 deletions src/core/modal.slice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createSlice } from "@reduxjs/toolkit";
import {
appendQueryParametersToUrl,
getCurrentLocation
getCurrentLocation,
removeQueryParametersFromUrl,
setQueryParametersInUrl
} from "./utils/helpers/url";

export type ModalId = string;
Expand Down Expand Up @@ -34,16 +36,14 @@ const returnFocusElement = () => {
// Removes the 'modal' parameter from the browser's address bar.
// If state.modalIds is not empty, adds the last modal ID as the 'modal' parameter.
const removeModalIdFromUrl = (state: StateProps) => {
const currentUrl = new URL(window.location.href);

if (state.modalIds && state.modalIds.length > 0) {
const lastModalId = state.modalIds[state.modalIds.length - 1];
currentUrl.searchParams.set("modal", lastModalId);
setQueryParametersInUrl({
modal: lastModalId
});
} else {
currentUrl.searchParams.delete("modal");
removeQueryParametersFromUrl("modal");
}

window.history.pushState({}, "", currentUrl);
};

const modalSlice = createSlice({
Expand Down
Loading