Skip to content

Commit 8bb1c4a

Browse files
committed
fix(common): persistant session across pages
1 parent aadf93d commit 8bb1c4a

File tree

4 files changed

+15
-33
lines changed

4 files changed

+15
-33
lines changed

context/session.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
import { createContext, useContext, useState } from 'react';
2-
import { ContextValues } from '../types';
1+
import { useRouter } from 'next/router';
2+
import { createContext, useContext, useEffect, useState } from 'react';
3+
import { bigCommerceSDK } from '../scripts/bcSdk';
34

4-
const SessionContext = createContext<Partial<ContextValues>>({});
5+
const SessionContext = createContext({ context: '' });
56

67
const SessionProvider = ({ children }) => {
8+
const { query } = useRouter();
79
const [context, setContext] = useState('');
8-
const value = { context, setContext };
10+
11+
useEffect(() => {
12+
if (query.context) {
13+
setContext(query.context.toString());
14+
// Keeps app in sync with BC (e.g. heatbeat, user logout, etc)
15+
bigCommerceSDK(query.context);
16+
}
17+
}, [query.context]);
918

1019
return (
11-
<SessionContext.Provider value={value}>
20+
<SessionContext.Provider value={{ context }}>
1221
{children}
1322
</SessionContext.Provider>
1423
);

pages/_app.tsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
import { Box, GlobalStyles } from '@bigcommerce/big-design';
22
import type { AppProps } from 'next/app';
3-
import { useRouter } from 'next/router';
4-
import { useEffect } from 'react';
53
import Header from '../components/header';
64
import SessionProvider from '../context/session';
7-
import { bigCommerceSDK } from '../scripts/bcSdk';
85

96
const MyApp = ({ Component, pageProps }: AppProps) => {
10-
const router = useRouter();
11-
const { query: { context } } = router;
12-
13-
useEffect(() => {
14-
// Keeps app in sync with BC (e.g. heatbeat, user logout, etc)
15-
if (context) bigCommerceSDK(context);
16-
}, [context]);
17-
187
return (
198
<>
209
<GlobalStyles />

pages/index.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import { Box, Flex, H1, H4, Panel } from '@bigcommerce/big-design';
2-
import { useEffect } from 'react';
32
import styled from 'styled-components';
43
import ErrorMessage from '../components/error';
54
import Loading from '../components/loading';
6-
import { useSession } from '../context/session';
75
import { useProducts } from '../lib/hooks';
86

9-
const Index = ({ context }: { context: string }) => {
7+
const Index = () => {
108
const { error, isLoading, summary } = useProducts();
11-
const { setContext } = useSession();
12-
13-
useEffect(() => {
14-
if (context) setContext(context);
15-
}, [context, setContext]);
169

1710
if (isLoading) return <Loading />;
1811
if (error) return <ErrorMessage error={error} />;
@@ -37,10 +30,6 @@ const Index = ({ context }: { context: string }) => {
3730
);
3831
};
3932

40-
export const getServerSideProps = async ({ query }) => ({
41-
props: { context: query?.context ?? '' }
42-
});
43-
4433
const StyledBox = styled(Box)`
4534
min-width: 10rem;
4635
`;

types/data.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
export interface ContextValues {
2-
context: string;
3-
setContext: (key: string) => void;
4-
}
5-
61
export interface FormData {
72
description: string;
83
isVisible: boolean;

0 commit comments

Comments
 (0)