Skip to content

Commit 11221f3

Browse files
chore: backup the changes for future reference
1 parent 5824c4b commit 11221f3

File tree

7 files changed

+61
-53
lines changed

7 files changed

+61
-53
lines changed

src/frontend/components/Cart/CartDetail.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,18 @@ import { useCallback, useEffect, useState } from 'react';
66
import CartItems from '../CartItems';
77
import CheckoutForm from '../CheckoutForm';
88
import { IFormData } from '../CheckoutForm/CheckoutForm';
9-
import SessionGateway from '../../gateways/Session.gateway';
109
import { useCart } from '../../providers/Cart.provider';
1110
import { useCurrency } from '../../providers/Currency.provider';
1211
import * as S from '../../styles/Cart.styled';
1312

14-
const CartDetail = ({ userId: initId }: { userId: string }) => {
13+
const CartDetail = ({ userId }: { userId: string }) => {
1514
const {
1615
cart: { items },
1716
emptyCart,
1817
placeOrder,
1918
} = useCart();
2019
const { selectedCurrency } = useCurrency();
2120
const { push } = useRouter();
22-
const [userId, setUserId] = useState(initId);
23-
24-
useEffect(() => {
25-
setUserId(SessionGateway.getSession().userId);
26-
}, []);
2721

2822
const onPlaceOrder = useCallback(
2923
async ({

src/frontend/components/Footer/Footer.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { useEffect, useState } from 'react';
54
import * as S from './Footer.styled';
6-
import SessionGateway from '../../gateways/Session.gateway';
75
import { CypressFields } from '../../utils/enums/CypressFields';
8-
import PlatformFlag from '../PlatformFlag';
96

107
const currentYear = new Date().getFullYear();
118

129
const Footer = ({ userId }: { userId: string }) => {
13-
const [sessionId, setSessionId] = useState(userId);
14-
15-
useEffect(() => {
16-
setSessionId(SessionGateway.getSession().userId);
17-
}, []);
18-
1910
return (
2011
<S.Footer>
2112
<div>
2213
<p>This website is hosted for demo purpose only. It is not an actual shop.</p>
2314
<p>
24-
<span data-cy={CypressFields.SessionId}>session-id: {sessionId}</span>
15+
<span data-cy={CypressFields.SessionId}>session-id: {userId}</span>
2516
</p>
2617
</div>
2718
<p>

src/frontend/gateways/Session.gateway.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { getCookie, setCookie } from 'cookies-next';
45
import { v4 } from 'uuid';
5-
import { getCookie } from "cookies-next";
66

77
interface ISession {
88
userId: string;
99
currencyCode: string;
1010
}
1111

12-
const sessionKey = 'session';
13-
const defaultSession: ISession = {
14-
userId: getCookie('USERID') as string || v4(),
15-
currencyCode: 'USD',
16-
};
12+
const defaultUUID = v4();
13+
const defaultCurrencyCode = 'USD';
1714

18-
const SessionGateway = () => ({
19-
getSession(): ISession {
20-
if (typeof window === 'undefined') return defaultSession;
21-
const sessionString = localStorage.getItem(sessionKey);
22-
23-
if (!sessionString) localStorage.setItem(sessionKey, JSON.stringify(defaultSession));
15+
function getDefaultSession(userId?: string): ISession {
16+
return {
17+
userId: userId ?? defaultUUID,
18+
currencyCode: defaultCurrencyCode,
19+
};
20+
}
2421

25-
return JSON.parse(sessionString || JSON.stringify(defaultSession)) as ISession;
22+
const SessionGateway = () => ({
23+
getSession(userId?: string): ISession {
24+
if (typeof window === 'undefined') {
25+
return getDefaultSession(userId);
26+
}
27+
28+
const cookieUserId = getCookie('USERID') as string;
29+
const currencyCode = getCookie('currencyCode') as string;
30+
31+
return {
32+
userId: cookieUserId || userId || defaultUUID,
33+
currencyCode: currencyCode || defaultCurrencyCode,
34+
};
2635
},
2736
setSessionValue<K extends keyof ISession>(key: K, value: ISession[K]) {
28-
const session = this.getSession();
29-
30-
localStorage.setItem(sessionKey, JSON.stringify({ ...session, [key]: value }));
37+
setCookie(key, value);
3138
},
3239
});
3340

src/frontend/pages/cart/checkout/[orderId]/index.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { InferGetServerSidePropsType, NextPage } from 'next';
4+
import { GetServerSidePropsContext, InferGetServerSidePropsType, NextPage } from 'next';
55
import Head from 'next/head';
66
import Link from 'next/link';
77
import { useRouter } from 'next/router';
@@ -14,11 +14,15 @@ import Recommendations from '../../../../components/Recommendations';
1414
import AdProvider from '../../../../providers/Ad.provider';
1515
import * as S from '../../../../styles/Checkout.styled';
1616
import { IProductCheckout } from '../../../../types/Cart';
17-
import { getCookie } from 'cookies-next';
18-
19-
export async function getServerSideProps() {
20-
const userId = getCookie('USERID') as string;
17+
import { getCookie, setCookie } from 'cookies-next';
18+
import { v4 } from 'uuid';
2119

20+
export async function getServerSideProps({ req, res }: GetServerSidePropsContext) {
21+
let userId = (await getCookie('USERID', { req, res })) as string;
22+
if (userId === undefined || userId === '') {
23+
userId = v4();
24+
setCookie(userId, 'USERID', { req, res });
25+
}
2226
return {
2327
props: { userId },
2428
};

src/frontend/pages/cart/index.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { InferGetServerSidePropsType, NextPage } from 'next';
4+
import { GetServerSidePropsContext, InferGetServerSidePropsType, NextPage } from 'next';
55
import Head from 'next/head';
66
import Footer from '../../components/Footer';
77
import Layout from '../../components/Layout';
@@ -11,11 +11,15 @@ import CartDetail from '../../components/Cart/CartDetail';
1111
import EmptyCart from '../../components/Cart/EmptyCart';
1212
import { useCart } from '../../providers/Cart.provider';
1313
import AdProvider from '../../providers/Ad.provider';
14-
import { getCookie } from 'cookies-next';
15-
16-
export async function getServerSideProps() {
17-
const userId = getCookie('USERID') as string;
14+
import { getCookie, setCookie } from 'cookies-next';
15+
import { v4 } from 'uuid';
1816

17+
export async function getServerSideProps({ req, res }: GetServerSidePropsContext) {
18+
let userId = (await getCookie('USERID', { req, res })) as string;
19+
if (userId === undefined || userId === '') {
20+
userId = v4();
21+
setCookie(userId, 'USERID', { req, res });
22+
}
1923
return {
2024
props: { userId },
2125
};

src/frontend/pages/index.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { InferGetServerSidePropsType, NextPage } from 'next';
4+
import { GetServerSidePropsContext, InferGetServerSidePropsType, NextPage } from 'next';
55
import Head from 'next/head';
66
import Footer from '../components/Footer';
77
import Layout from '../components/Layout';
@@ -12,11 +12,15 @@ import ApiGateway from '../gateways/Api.gateway';
1212
import Banner from '../components/Banner';
1313
import { CypressFields } from '../utils/enums/CypressFields';
1414
import { useCurrency } from '../providers/Currency.provider';
15-
import { getCookie } from 'cookies-next';
16-
17-
export async function getServerSideProps() {
18-
const userId = getCookie('USERID') as string;
15+
import { getCookie, setCookie } from 'cookies-next';
16+
import { v4 } from 'uuid';
1917

18+
export async function getServerSideProps({ req, res }: GetServerSidePropsContext) {
19+
let userId = (await getCookie('USERID', { req, res })) as string;
20+
if (userId === undefined || userId === '') {
21+
userId = v4();
22+
setCookie(userId, 'USERID', { req, res });
23+
}
2024
return {
2125
props: { userId },
2226
};

src/frontend/pages/product/[productId]/index.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { InferGetServerSidePropsType, NextPage } from 'next';
4+
import { GetServerSidePropsContext, InferGetServerSidePropsType, NextPage } from 'next';
55
import Head from 'next/head';
66
import Image from 'next/image';
77
import { useRouter } from 'next/router';
@@ -20,11 +20,15 @@ import AdProvider from '../../../providers/Ad.provider';
2020
import { useCart } from '../../../providers/Cart.provider';
2121
import * as S from '../../../styles/ProductDetail.styled';
2222
import { useCurrency } from '../../../providers/Currency.provider';
23-
import { getCookie } from 'cookies-next';
24-
25-
export async function getServerSideProps() {
26-
const userId = getCookie('USERID') as string;
23+
import { getCookie, setCookie } from 'cookies-next';
24+
import { v4 } from 'uuid';
2725

26+
export async function getServerSideProps({ req, res }: GetServerSidePropsContext) {
27+
let userId = (await getCookie('USERID', { req, res })) as string;
28+
if (userId === undefined || userId === '') {
29+
userId = v4();
30+
setCookie(userId, 'USERID', { req, res });
31+
}
2832
return {
2933
props: { userId },
3034
};

0 commit comments

Comments
 (0)