Skip to content

Commit f6abe99

Browse files
committed
refactor: State management
1 parent 2ed400f commit f6abe99

File tree

17 files changed

+259
-83
lines changed

17 files changed

+259
-83
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ A JAMstack website based on _NextJS_ and _Sanity_ consisting of a professional p
3333

3434
- **Languages**: HTML, CSS, SASS, JavaScript, Typescript, GraphQL, YAML, Bash
3535
- **Environments**: DOM
36-
- **Libraries**: Headless UI, GSAP, TypedJS, Testing Library
36+
- **Libraries**: Headless UI, GSAP, TypedJS, Testing Library, C64JS
3737
- **Frameworks**: React, Next.js, TailwindCSS, Sanity, Jest, Cypress
3838
- **Pre/Post-Processors**: PostCSS, Sass
3939
- **Linters/Plugins**: stylelint, ESLint, Prettier

frontend/src/components/Animation/AnimationIntro.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import AnimationIntroLogo from './AnimationIntroLogo';
77
import AnimationIntroImage from './AnimationIntroLogoImage';
88
import AnimationIntroTagline from './AnimationIntroTagline';
99
import handleState from '@/state/actions';
10-
import { useDispatchContext, useTitleContext } from '@/hooks/State';
10+
import { useDispatchContextTitle, useTitleContext } from '@/hooks/State';
1111
import useStorage from '@/hooks/Storage';
1212
import { AnimationIntroType } from '@/types/components/AnimationIntroImage';
1313
import { CookiesProvider } from '../StateProvider';
@@ -25,7 +25,7 @@ const AnimationIntro = (): React.ReactNode => {
2525
const router = useRouter();
2626
const { getStorage } = useStorage();
2727
const title = useTitleContext();
28-
const dispatch = useDispatchContext();
28+
const dispatch = useDispatchContextTitle();
2929
const titleStorage = getStorage(TITLE) ?? false;
3030

3131
useEffect(() => {
@@ -42,7 +42,10 @@ const AnimationIntro = (): React.ReactNode => {
4242
const handleTitle = (): void => {
4343
// Storage + state check
4444
if (titleStorage || title) {
45-
handleState({ type: TITLE, element: titleStorage }, dispatch);
45+
handleState(
46+
{ type: TITLE, element: titleStorage },
47+
dispatch as keyof typeof dispatch,
48+
);
4649

4750
router.push(ROUTE.HOME.PATH);
4851
}

frontend/src/components/Animation/AnimationIntroLogoImage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useRouter } from 'next/navigation';
55
import gsap from 'gsap';
66
import { useGSAP } from '@gsap/react';
77
import handleState from '@/state/actions';
8-
import { useCookiesContext, useDispatchContext } from '@/hooks/State';
8+
import { useCookiesContext, useDispatchContextCookies } from '@/hooks/State';
99
import useStorage from '@/hooks/Storage';
1010
import { checkCookies } from '@/utilities/utils';
1111
import { ROUTE, ACTION, ANIMATION_TIMELINE, TEST } from '@/utilities/constants';
@@ -37,7 +37,7 @@ const AnimationIntroImage = ({
3737
const { getStorage, setStorage } = useStorage();
3838
const cookiesStorage = getStorage(COOKIES);
3939
const cookies = useCookiesContext();
40-
const dispatch = useDispatchContext();
40+
const dispatch = useDispatchContextCookies();
4141

4242
gsap.registerPlugin(useGSAP);
4343

@@ -63,7 +63,7 @@ const AnimationIntroImage = ({
6363
type: TITLE,
6464
element: true,
6565
},
66-
dispatch,
66+
dispatch as keyof typeof dispatch,
6767
);
6868
};
6969

frontend/src/components/Animation/AnimationPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useEffect, useRef, Ref } from 'react';
44
import {
5-
useDispatchContext,
5+
useDispatchContextLoading,
66
useLoadingContext,
77
useThemeContext,
88
} from '@/hooks/State';
@@ -21,7 +21,7 @@ const AnimationPage = (): React.ReactNode => {
2121
const bars = useRef<Array<HTMLDivElement>>([]);
2222
const theme = useThemeContext();
2323
const loading = useLoadingContext();
24-
const dispatch = useDispatchContext();
24+
const dispatchLoading = useDispatchContextLoading();
2525

2626
useEffect(() => {
2727
// Theme check
@@ -36,7 +36,7 @@ const AnimationPage = (): React.ReactNode => {
3636
type: ACTION.LOADING,
3737
element: false,
3838
},
39-
dispatch,
39+
dispatchLoading as keyof typeof dispatchLoading,
4040
);
4141
clearTimeout(timer);
4242
}, 500);

frontend/src/components/Cookies/CookiesBanner.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { useEffect } from 'react';
44
import Action from '../Layout/Action';
55
import {
66
useCookiesContext,
7-
useDispatchContext,
7+
useDispatchContextCookies,
88
useThemeContext,
99
} from '@/hooks/State';
1010
import useStorage from '@/hooks/Storage';
1111
import handleState from '@/state/actions';
1212
import { isLightTheme } from '@/utilities/utils';
1313
import { ACTIONS, ACTION, COOKIES_STATE, TEST } from '@/utilities/constants';
1414
import { METADATA } from '@/data/content';
15-
import { Cookie, Status } from '@/types/state/State';
15+
import { Cookie, Status, TStateCookies } from '@/types/state/State';
1616

1717
/**
1818
* @description Cookies banner component
@@ -28,7 +28,7 @@ const CookiesBanner = (): React.ReactNode => {
2828
const theme = useThemeContext();
2929
const cookies = useCookiesContext();
3030
const { open } = cookies;
31-
const dispatch = useDispatchContext();
31+
const dispatch = useDispatchContextCookies();
3232

3333
useEffect(() => {
3434
initCookies();
@@ -59,7 +59,7 @@ const CookiesBanner = (): React.ReactNode => {
5959
active: cookiesSaved.active ?? cookies.active,
6060
},
6161
},
62-
dispatch,
62+
dispatch as keyof typeof dispatch,
6363
);
6464
};
6565

@@ -111,9 +111,9 @@ const CookiesBanner = (): React.ReactNode => {
111111
handleState(
112112
{
113113
type: ACTION.COOKIES,
114-
element: selection,
114+
element: selection as TStateCookies,
115115
},
116-
dispatch,
116+
dispatch as keyof typeof dispatch,
117117
);
118118
};
119119

frontend/src/components/Cookies/CookiesConsentForm.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import Form from 'next/form';
44
import handleState from '@/state/actions';
5-
import { Cookie, Status } from '@/types/state/State';
6-
import { useCookiesContext, useDispatchContext } from '@/hooks/State';
5+
import { Cookie, Status, TStateCookies } from '@/types/state/State';
6+
import { useCookiesContext, useDispatchContextCookies } from '@/hooks/State';
77
import useStorage from '@/hooks/Storage';
88
import { isCookieActive } from '@/utilities/utils';
99
import { ACTION, COOKIE_LEVEL, COOKIES, TEST } from '@/utilities/constants';
@@ -27,7 +27,7 @@ const CookiesConsentForm = ({
2727
const { setStorage, deleteStorages } = useStorage();
2828
const cookies = useCookiesContext();
2929
const { active } = cookies;
30-
const dispatch = useDispatchContext();
30+
const dispatch = useDispatchContextCookies();
3131

3232
// Helpers
3333
/**
@@ -142,9 +142,9 @@ const CookiesConsentForm = ({
142142
handleState(
143143
{
144144
type: ACTION.COOKIES,
145-
element: selection,
145+
element: selection as TStateCookies,
146146
},
147-
dispatch,
147+
dispatch as keyof typeof dispatch,
148148
);
149149
};
150150

frontend/src/components/Layout/Footer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Modal from './Modal';
44
import CookiesConsentForm from '../Cookies/CookiesConsentForm';
55
import {
66
useCookiesContext,
7-
useDispatchContext,
7+
useDispatchContextModal,
88
useModalContext,
99
useThemeContext,
1010
} from '@/hooks/State';
@@ -40,7 +40,7 @@ const Footer = (): React.ReactNode => {
4040
const theme = useThemeContext();
4141
const cookies = useCookiesContext();
4242
const modal = useModalContext();
43-
const dispatch = useDispatchContext();
43+
const dispatch = useDispatchContextModal();
4444
const { navigate } = useAnimation();
4545

4646
useShortcut();
@@ -70,7 +70,7 @@ const Footer = (): React.ReactNode => {
7070
[type]: !modal[type as keyof typeof modal],
7171
},
7272
},
73-
dispatch,
73+
dispatch as keyof typeof dispatch,
7474
);
7575
};
7676

frontend/src/components/Layout/ThemeSwitch.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useEffect } from 'react';
44
import { Switch } from '@headlessui/react';
5-
import { useDispatchContext, useThemeContext } from '@/hooks/State';
5+
import { useDispatchContextTheme, useThemeContext } from '@/hooks/State';
66
import useStorage from '@/hooks/Storage';
77
import handleState from '@/state/actions';
88
import { isLightTheme, checkCookies } from '@/utilities/utils';
@@ -19,7 +19,7 @@ const ThemeSwitch = (): React.ReactNode => {
1919
const themeSaved = getStorage(LABEL) ?? '';
2020
const cookiesStorage = getStorage(ACTION.COOKIES);
2121
const theme = useThemeContext();
22-
const dispatch = useDispatchContext();
22+
const dispatch = useDispatchContextTheme();
2323

2424
useEffect(() => {
2525
// User preference + system-aware detection
@@ -28,7 +28,7 @@ const ThemeSwitch = (): React.ReactNode => {
2828

2929
handleState(
3030
{ type: ACTION.THEME, element: isDark ? DARK : LIGHT },
31-
dispatch,
31+
dispatch as keyof typeof dispatch,
3232
);
3333
enableTheme(isDark);
3434
}, []);
@@ -61,18 +61,12 @@ const ThemeSwitch = (): React.ReactNode => {
6161
const handleTheme = (value: boolean): void => {
6262
enableTheme(value);
6363

64-
// Storage + state check
65-
if (
66-
themeSaved !== '' ||
67-
(cookiesStorage &&
68-
checkCookies(JSON.parse(cookiesStorage), Cookie.Essentials))
69-
) {
70-
setStorage(LABEL, value ? LIGHT : DARK);
71-
}
64+
const selected = value ? DARK : LIGHT;
7265

66+
setStorage(LABEL, selected);
7367
handleState(
74-
{ type: ACTION.THEME, element: value ? DARK : LIGHT },
75-
dispatch,
68+
{ type: ACTION.THEME, element: selected },
69+
dispatch as keyof typeof dispatch,
7670
);
7771
};
7872

frontend/src/components/StateProvider.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import { useMemo, useCallback, useReducer } from 'react';
44
import {
5-
DispatchContext,
65
CookiesContext,
76
LoadingContext,
87
ModalContext,
98
ThemeContext,
109
TitleContext,
10+
CookiesDispatchContext,
11+
LoadingDispatchContext,
12+
ModalDispatchContext,
13+
ThemeDispatchContext,
14+
TitleDispatchContext,
1115
} from '@/state/contexts';
1216
import {
1317
reducerCookies,
@@ -48,7 +52,9 @@ const CookiesProvider = ({
4852

4953
return (
5054
<CookiesContext value={cookies}>
51-
<DispatchContext value={setCookies}>{children}</DispatchContext>
55+
<CookiesDispatchContext value={setCookies}>
56+
{children}
57+
</CookiesDispatchContext>
5258
</CookiesContext>
5359
);
5460
};
@@ -78,7 +84,9 @@ const LoadingProvider = ({
7884

7985
return (
8086
<LoadingContext value={loading}>
81-
<DispatchContext value={setLoading}>{children}</DispatchContext>
87+
<LoadingDispatchContext value={setLoading}>
88+
{children}
89+
</LoadingDispatchContext>
8290
</LoadingContext>
8391
);
8492
};
@@ -108,7 +116,7 @@ const ModalProvider = ({
108116

109117
return (
110118
<ModalContext value={modal}>
111-
<DispatchContext value={setModal}>{children}</DispatchContext>
119+
<ModalDispatchContext value={setModal}>{children}</ModalDispatchContext>
112120
</ModalContext>
113121
);
114122
};
@@ -138,7 +146,7 @@ const ThemeProvider = ({
138146

139147
return (
140148
<ThemeContext value={theme}>
141-
<DispatchContext value={setTheme}>{children}</DispatchContext>
149+
<ThemeDispatchContext value={setTheme}>{children}</ThemeDispatchContext>
142150
</ThemeContext>
143151
);
144152
};
@@ -156,7 +164,7 @@ const TitleProvider = ({
156164

157165
return (
158166
<TitleContext value={title}>
159-
<DispatchContext value={setTitle}>{children}</DispatchContext>
167+
<TitleDispatchContext value={setTitle}>{children}</TitleDispatchContext>
160168
</TitleContext>
161169
);
162170
};

frontend/src/hooks/Animation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MouseEvent } from 'react';
22
import { useTransitionRouter } from 'next-view-transitions';
3-
import { useDispatchContext } from './State';
3+
import { useDispatchContextLoading } from './State';
44
import handleState from '@/state/actions';
55
import { ACTION } from '@/utilities/constants';
66
import { TUseAnimation } from '@/types/hooks/Animation';
@@ -13,7 +13,7 @@ import { TUseAnimation } from '@/types/hooks/Animation';
1313
*/
1414
const useAnimation = (): TUseAnimation => {
1515
// Hooks
16-
const dispatch = useDispatchContext();
16+
const dispatch = useDispatchContextLoading();
1717
const router = useTransitionRouter();
1818

1919
// Helpers
@@ -29,7 +29,7 @@ const useAnimation = (): TUseAnimation => {
2929
type: ACTION.LOADING,
3030
element: true,
3131
},
32-
dispatch,
32+
dispatch as keyof typeof dispatch,
3333
);
3434
};
3535

0 commit comments

Comments
 (0)