-
Notifications
You must be signed in to change notification settings - Fork 26k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade with-redux example to app router (#49994)
### What? Update **with-redux** example. ### Why? **with-redux** example have: - outdated packages - outdated approaches and relies on **pages** directory Since **app router** is stable now and is recommended to use, I've updated this example. ### How? An update includes: - move example to **app router** - update **package.json** deps to the latest versions - modernize jest by switching from **ts-node** to **@swc/jest** - fix and overhaul **tests** - modernize **redux** infra - overhaul example **source code quality** Co-authored-by: Balázs Orbán <18369201+balazsorban44@users.noreply.github.com>
- Loading branch information
1 parent
cbb69b2
commit 04cd1fd
Showing
38 changed files
with
478 additions
and
489 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* Core */ | ||
import { NextResponse } from 'next/server' | ||
|
||
export async function POST(req: Request, res: Response) { | ||
const body = await req.json() | ||
const { amount = 1 } = body | ||
|
||
// simulate IO latency | ||
await new Promise((r) => setTimeout(r, 500)) | ||
|
||
return NextResponse.json({ data: amount }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use client' | ||
|
||
/* Core */ | ||
import Link from 'next/link' | ||
import { usePathname } from 'next/navigation' | ||
|
||
/* Instruments */ | ||
import styles from '../styles/layout.module.css' | ||
|
||
export const Nav = () => { | ||
const pathname = usePathname() | ||
|
||
return ( | ||
<nav className={styles.nav}> | ||
<Link | ||
className={`${styles.link} ${pathname === '/' ? styles.active : ''}`} | ||
href="/" | ||
> | ||
Home | ||
</Link> | ||
<Link | ||
className={`${styles.link} ${ | ||
pathname === '/verify' ? styles.active : '' | ||
}`} | ||
href="/verify" | ||
> | ||
Verify | ||
</Link> | ||
</nav> | ||
) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* Components */ | ||
import { Providers } from '@/lib/providers' | ||
import { Nav } from './components/Nav' | ||
|
||
/* Instruments */ | ||
import styles from './styles/layout.module.css' | ||
import './styles/globals.css' | ||
|
||
export default function RootLayout(props: React.PropsWithChildren) { | ||
return ( | ||
<Providers> | ||
<html lang="en"> | ||
<body> | ||
<section className={styles.container}> | ||
<Nav /> | ||
|
||
<header className={styles.header}> | ||
<img src="/logo.svg" className={styles.logo} alt="logo" /> | ||
</header> | ||
|
||
<main className={styles.main}>{props.children}</main> | ||
|
||
<footer className={styles.footer}> | ||
<span>Learn </span> | ||
<a | ||
className={styles.link} | ||
href="https://reactjs.org/" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
React | ||
</a> | ||
<span>, </span> | ||
<a | ||
className={styles.link} | ||
href="https://redux.js.org/" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Redux | ||
</a> | ||
<span>, </span> | ||
<a | ||
className={styles.link} | ||
href="https://redux-toolkit.js.org/" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Redux Toolkit | ||
</a> | ||
,<span> and </span> | ||
<a | ||
className={styles.link} | ||
href="https://react-redux.js.org/" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
React Redux | ||
</a> | ||
</footer> | ||
</section> | ||
</body> | ||
</html> | ||
</Providers> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* Components */ | ||
import { Counter } from './components/Counter/Counter' | ||
|
||
export default function IndexPage() { | ||
return <Counter /> | ||
} | ||
|
||
export const metadata = { | ||
title: 'Redux Toolkit', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
html, | ||
body { | ||
min-height: 100vh; | ||
padding: 0; | ||
margin: 0; | ||
font-family: system-ui, sans-serif; | ||
} | ||
|
||
a { | ||
color: inherit; | ||
text-decoration: none; | ||
} | ||
|
||
* { | ||
box-sizing: border-box; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
.container { | ||
display: grid; | ||
grid-template-areas: | ||
'nav' | ||
'header' | ||
'main' | ||
'footer'; | ||
grid-template-rows: auto auto 1fr 36px; | ||
align-items: center; | ||
min-height: 100vh; | ||
} | ||
|
||
.logo { | ||
height: 40vmin; | ||
pointer-events: none; | ||
} | ||
|
||
.header { | ||
grid-area: header; | ||
} | ||
|
||
.main { | ||
grid-area: main; | ||
} | ||
|
||
.header, | ||
.main { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.footer { | ||
grid-area: footer; | ||
justify-self: center; | ||
} | ||
|
||
.nav { | ||
grid-area: nav; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
gap: 8px; | ||
padding: 8px; | ||
font-size: calc(10px + 2vmin); | ||
} | ||
|
||
.link:hover { | ||
text-decoration: underline; | ||
} | ||
|
||
.link { | ||
color: #704cb6; | ||
} | ||
|
||
.link.active { | ||
text-decoration: underline; | ||
} | ||
|
||
@media (prefers-reduced-motion: no-preference) { | ||
.logo { | ||
animation: logo-float infinite 3s ease-in-out; | ||
} | ||
} | ||
|
||
@keyframes logo-float { | ||
0% { | ||
transform: translateY(0); | ||
} | ||
50% { | ||
transform: translateY(10px); | ||
} | ||
100% { | ||
transform: translateY(0px); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default function VerifyPage() { | ||
return ( | ||
<> | ||
<h1>Verify page</h1> | ||
<p> | ||
This page is intended to verify that Redux state is persisted across | ||
page navigations. | ||
</p> | ||
</> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use client' | ||
|
||
/* Core */ | ||
import { Provider } from 'react-redux' | ||
|
||
/* Instruments */ | ||
import { reduxStore } from '@/lib/redux' | ||
|
||
export const Providers = (props: React.PropsWithChildren) => { | ||
return <Provider store={reduxStore}>{props.children}</Provider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* Core */ | ||
import { createAsyncThunk } from '@reduxjs/toolkit' | ||
|
||
/* Instruments */ | ||
import type { ReduxState, ReduxDispatch } from './store' | ||
|
||
/** | ||
* ? A utility function to create a typed Async Thnuk Actions. | ||
*/ | ||
export const createAppAsyncThunk = createAsyncThunk.withTypes<{ | ||
state: ReduxState | ||
dispatch: ReduxDispatch | ||
rejectValue: string | ||
}>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './store' | ||
export * from './slices' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* Core */ | ||
import { createLogger } from 'redux-logger' | ||
|
||
const middleware = [ | ||
createLogger({ | ||
duration: true, | ||
timestamp: false, | ||
collapsed: true, | ||
colors: { | ||
title: () => '#139BFE', | ||
prevState: () => '#1C5FAF', | ||
action: () => '#149945', | ||
nextState: () => '#A47104', | ||
error: () => '#ff0005', | ||
}, | ||
predicate: () => typeof window !== 'undefined', | ||
}), | ||
] | ||
|
||
export { middleware } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* Instruments */ | ||
import { counterSlice } from './slices' | ||
|
||
export const reducer = { | ||
counter: counterSlice.reducer, | ||
} |
Oops, something went wrong.