Skip to content

Commit

Permalink
clear url param
Browse files Browse the repository at this point in the history
  • Loading branch information
wadehammes committed Jul 14, 2022
1 parent 32fd082 commit 2e5ee0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/context/collection.context.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Router from "next/router";
import { createContext, useContext, useReducer, FC, useCallback } from "react";
import { PropsWithChildrenOnly } from "src/@types/react";
import { LOAD_RELEASES_TEXT, USERNAME_STORAGE_PARAM } from "src/constants";
Expand Down Expand Up @@ -374,9 +375,20 @@ export const CollectionContextProvider: FC<PropsWithChildrenOnly> = ({
);

const dispatchResetState = useCallback(() => {
const {
location: { href },
localStorage,
} = window;

const url = new URL(href);

url.searchParams.delete("username");

Router.replace(url);

dispatch({ type: CollectionActionTypes.ResetState, payload: initialState });

window.localStorage.removeItem(USERNAME_STORAGE_PARAM);
localStorage.removeItem(USERNAME_STORAGE_PARAM);
}, [dispatch]);

return children ? (
Expand Down
11 changes: 11 additions & 0 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useUrlParam } from "src/hooks/useUrlParam.hook";
import { ReleasesLoading } from "src/components/ReleasesLoading/ReleasesLoading.component";
import styled from "styled-components";
import { trackEvent } from "src/analytics/analytics";
import Router from "next/router";

const ClearButton = styled.button`
background: white;
Expand Down Expand Up @@ -96,6 +97,16 @@ const FilterMyDiscogs: FC = () => {
}

if (username) {
const {
location: { href },
} = window;

const url = new URL(href);

url.searchParams.set("username", username);

Router.replace(url);

if (usernameRef?.current) {
usernameRef.current.value = username;
}
Expand Down

1 comment on commit 2e5ee0e

@vercel
Copy link

@vercel vercel bot commented on 2e5ee0e Jul 14, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.