set search params without the whole layout reloading

This commit is contained in:
Kalle (Sendou) 2021-01-08 15:26:08 +02:00
parent b53da05627
commit 967ce013bf
5 changed files with 27 additions and 14 deletions

View File

@ -1,7 +1,6 @@
import { Flex, useToast } from "@chakra-ui/react";
import { t } from "@lingui/macro";
import MyContainer from "components/common/MyContainer";
import { AppProps } from "next/app";
import { useState } from "react";
import { SWRConfig } from "swr";
import Banner from "./Banner";
@ -11,7 +10,7 @@ import TopNav from "./TopNav";
const DATE_KEYS = ["createdAt", "updatedAt"];
const Layout = ({ Component, pageProps }: AppProps) => {
const Layout = ({ children }: { children: React.ReactNode }) => {
const [errors, setErrors] = useState(new Set<string>());
const toast = useToast();
@ -47,9 +46,7 @@ const Layout = ({ Component, pageProps }: AppProps) => {
<IconNavBar />
<Banner />
<Flex flexDirection="column" minH="100vh" pt="1rem">
<MyContainer wide>
<Component {...pageProps} />
</MyContainer>
<MyContainer wide>{children}</MyContainer>
<Footer />
</Flex>
</SWRConfig>

View File

@ -1,4 +1,5 @@
import { Playstyle } from "@prisma/client";
import { setSearchParams } from "lib/setSearchParams";
import useUser from "lib/useUser";
import { useRouter } from "next/router";
import { GetAllFreeAgentPostsData } from "prisma/queries/getAllFreeAgentPosts";
@ -23,10 +24,7 @@ export function useFreeAgents() {
(oldState: UseFreeAgentsState, action: Action) => {
switch (action.type) {
case "SET_PLAYSTYLE":
router.replace({
pathname: "/freeagents",
query: { playstyle: action.playstyle },
});
setSearchParams("playstyle", action.playstyle);
return { ...oldState, playstyle: action.playstyle };
default:

18
lib/setSearchParams.ts Normal file
View File

@ -0,0 +1,18 @@
export const setSearchParams = (key: string, value: string | undefined) => {
const url = new URL(window.location.href);
const params = new URLSearchParams(url.search);
if (!value) {
params.delete(key);
} else {
params.set(key, value); // encodeURIComponent(value)
}
history.replaceState(
{},
"",
`${window.location.pathname}${
Array.from(params.entries()).length ? "?" : ""
}${params.toString()}`
);
};

View File

@ -144,7 +144,7 @@ const setDisplayedLanguage = () => {
activateLocale(getUsersLanguage());
};
const MyApp = (props: AppProps) => {
const MyApp = ({ Component, pageProps }: AppProps) => {
useEffect(setDisplayedLanguage, []);
return (
@ -178,10 +178,12 @@ const MyApp = (props: AppProps) => {
}}
/>
<NextAuthProvider session={props.pageProps.session}>
<NextAuthProvider session={pageProps.session}>
<ChakraProvider theme={extendedTheme}>
<I18nProvider i18n={i18n}>
<Layout {...props} />
<Layout>
<Component {...pageProps} />
</Layout>
</I18nProvider>
</ChakraProvider>
</NextAuthProvider>

View File

@ -40,8 +40,6 @@ const FreeAgentsPage = () => {
const { data, usersPost, playstyleCounts, state, dispatch } = useFreeAgents();
const [modalIsOpen, setModalIsOpen] = useState(false);
console.log({ playstyleCounts });
return (
<MyContainer>
{modalIsOpen && (