@soroush.design/cli 0.2.0-alpha.4 → 0.2.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin.mjs +2 -15
- package/dist/index.mjs +1 -1
- package/dist/{io-C_8_JerZ.mjs → io-CqI2BTaP.mjs} +81 -12
- package/package.json +3 -2
- package/templates/react-router/js/base/README.md +67 -0
- package/templates/react-router/js/base/_gitignore +4 -0
- package/templates/{vike/js/spa/src/pages/+onRenderHtml.jsx → react-router/js/base/index.html} +3 -7
- package/templates/react-router/js/base/src/components/RouterLink.jsx +12 -0
- package/templates/react-router/js/base/src/data/api.js +76 -0
- package/templates/react-router/js/base/src/main.jsx +38 -0
- package/templates/react-router/js/base/src/pages/ErrorPage.jsx +14 -0
- package/templates/react-router/js/base/src/pages/Layout.jsx +141 -0
- package/templates/react-router/js/base/src/pages/index/Counter.jsx +18 -0
- package/templates/react-router/js/base/src/pages/index/Greeting.jsx +48 -0
- package/templates/react-router/js/base/src/pages/index/Page.jsx +29 -0
- package/templates/react-router/js/base/src/pages/index/Page.test.jsx +61 -0
- package/templates/react-router/js/base/src/routes.jsx +46 -0
- package/templates/react-router/js/base/vite.config.js +13 -0
- package/templates/react-router/js/base/vitest.config.js +13 -0
- package/templates/react-router/js/sample-data/src/data/items.js +55 -0
- package/templates/react-router/js/sample-data/src/pages/item/Page.jsx +49 -0
- package/templates/react-router/js/sample-data/src/pages/item/loader.js +14 -0
- package/templates/react-router/js/sample-data/src/pages/items/Page.jsx +48 -0
- package/templates/react-router/js/sample-data/src/pages/items/StatusChip.jsx +8 -0
- package/templates/react-router/js/sample-data/src/pages/items/loader.js +11 -0
- package/templates/react-router/js/with/jotai/src/pages/index/Counter.jsx +19 -0
- package/templates/react-router/js/with/jotai/src/state/counter.js +4 -0
- package/templates/react-router/js/with/jotai/src/state/scheme.js +4 -0
- package/templates/react-router/js/with/msw/_env +3 -0
- package/templates/react-router/js/with/msw/src/mocks/browser.js +5 -0
- package/templates/react-router/js/with/msw/src/mocks/handlers.js +24 -0
- package/templates/react-router/js/with/msw/src/mocks/server.js +5 -0
- package/templates/react-router/js/with/msw/src/mocks/setupTests.js +7 -0
- package/templates/react-router/js/with/msw/src/mocks/start.js +10 -0
- package/templates/react-router/js/with/react-hook-form/src/pages/form/Page.jsx +294 -0
- package/templates/react-router/js/with/redux-toolkit/src/pages/index/Counter.jsx +20 -0
- package/templates/react-router/js/with/redux-toolkit/src/state/store.js +27 -0
- package/templates/react-router/js/with/zustand/src/pages/index/Counter.jsx +17 -0
- package/templates/react-router/js/with/zustand/src/state/counter.js +14 -0
- package/templates/react-router/js/with/zustand/src/state/scheme.js +14 -0
- package/templates/react-router/ts/base/README.md +67 -0
- package/templates/react-router/ts/base/_gitignore +4 -0
- package/templates/react-router/ts/base/index.html +12 -0
- package/templates/react-router/ts/base/src/components/RouterLink.tsx +12 -0
- package/templates/react-router/ts/base/src/data/api.ts +84 -0
- package/templates/react-router/ts/base/src/main.tsx +38 -0
- package/templates/react-router/ts/base/src/pages/ErrorPage.tsx +14 -0
- package/templates/react-router/ts/base/src/pages/Layout.tsx +141 -0
- package/templates/react-router/ts/base/src/pages/index/Counter.tsx +18 -0
- package/templates/react-router/ts/base/src/pages/index/Greeting.tsx +49 -0
- package/templates/react-router/ts/base/src/pages/index/Page.test.tsx +61 -0
- package/templates/react-router/ts/base/src/pages/index/Page.tsx +29 -0
- package/templates/react-router/ts/base/src/routes.tsx +47 -0
- package/templates/react-router/ts/base/tsconfig.json +18 -0
- package/templates/react-router/ts/base/vite.config.ts +13 -0
- package/templates/react-router/ts/base/vitest.config.ts +13 -0
- package/templates/react-router/ts/sample-data/src/data/items.ts +61 -0
- package/templates/react-router/ts/sample-data/src/pages/item/Page.tsx +50 -0
- package/templates/react-router/ts/sample-data/src/pages/item/loader.ts +14 -0
- package/templates/react-router/ts/sample-data/src/pages/items/Page.tsx +49 -0
- package/templates/react-router/ts/sample-data/src/pages/items/StatusChip.tsx +9 -0
- package/templates/react-router/ts/sample-data/src/pages/items/loader.ts +11 -0
- package/templates/react-router/ts/with/jotai/src/pages/index/Counter.tsx +19 -0
- package/templates/react-router/ts/with/jotai/src/state/counter.ts +4 -0
- package/templates/react-router/ts/with/jotai/src/state/scheme.ts +4 -0
- package/templates/react-router/ts/with/msw/_env +3 -0
- package/templates/react-router/ts/with/msw/src/mocks/browser.ts +5 -0
- package/templates/react-router/ts/with/msw/src/mocks/handlers.ts +24 -0
- package/templates/react-router/ts/with/msw/src/mocks/server.ts +5 -0
- package/templates/react-router/ts/with/msw/src/mocks/setupTests.ts +7 -0
- package/templates/react-router/ts/with/msw/src/mocks/start.ts +10 -0
- package/templates/react-router/ts/with/react-hook-form/src/pages/form/Page.tsx +298 -0
- package/templates/react-router/ts/with/redux-toolkit/src/pages/index/Counter.tsx +20 -0
- package/templates/react-router/ts/with/redux-toolkit/src/state/store.ts +29 -0
- package/templates/react-router/ts/with/zustand/src/pages/index/Counter.tsx +17 -0
- package/templates/react-router/ts/with/zustand/src/state/counter.ts +19 -0
- package/templates/react-router/ts/with/zustand/src/state/scheme.ts +19 -0
- package/templates/templates.md +2 -1
- package/templates/vike/js/base/README.md +0 -4
- package/templates/vike/ts/base/README.md +0 -4
- package/templates/vike/js/spa/src/pages/+config.js +0 -12
- package/templates/vike/js/spa/src/pages/+onRenderClient.jsx +0 -25
- package/templates/vike/ts/spa/src/pages/+config.ts +0 -14
- package/templates/vike/ts/spa/src/pages/+onRenderClient.tsx +0 -27
- package/templates/vike/ts/spa/src/pages/+onRenderHtml.tsx +0 -17
- /package/templates/{vike/js/spa/src/pages/about/+Page.jsx → react-router/js/base/src/pages/about/Page.jsx} +0 -0
- /package/templates/{vike/ts/spa/src/pages/about/+Page.tsx → react-router/ts/base/src/pages/about/Page.tsx} +0 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// @if tanstack-query || swr || msw
|
|
2
|
+
// @if tanstack-query
|
|
3
|
+
import { useQuery } from '@tanstack/react-query'
|
|
4
|
+
// @elif swr
|
|
5
|
+
import useSWR from 'swr'
|
|
6
|
+
// @else
|
|
7
|
+
import { useEffect, useState } from 'react'
|
|
8
|
+
// @endif
|
|
9
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
10
|
+
// @if !tanstack-query && !swr
|
|
11
|
+
// @endif
|
|
12
|
+
import { fetchGreeting } from '../../data/api'
|
|
13
|
+
|
|
14
|
+
// @if tanstack-query
|
|
15
|
+
/** What the API says, read with a query of TanStack Query. */
|
|
16
|
+
export function Greeting() {
|
|
17
|
+
const { data } = useQuery({ queryKey: ['greeting'], queryFn: fetchGreeting })
|
|
18
|
+
return (
|
|
19
|
+
<Typography color="secondary">
|
|
20
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
21
|
+
</Typography>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
// @elif swr
|
|
25
|
+
/** What the API says, read with SWR. */
|
|
26
|
+
export function Greeting() {
|
|
27
|
+
const { data } = useSWR('greeting', fetchGreeting)
|
|
28
|
+
return (
|
|
29
|
+
<Typography color="secondary">
|
|
30
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
31
|
+
</Typography>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
// @else
|
|
35
|
+
/** What the API says, read with fetch, as the page loads. */
|
|
36
|
+
export function Greeting() {
|
|
37
|
+
const [data, setData] = useState()
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
fetchGreeting().then(setData, () => setData({ message: 'The API did not answer.' }))
|
|
40
|
+
}, [])
|
|
41
|
+
return (
|
|
42
|
+
<Typography color="secondary">
|
|
43
|
+
{data ? data.message : 'Asking the API for a greeting...'}
|
|
44
|
+
</Typography>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
// @endif
|
|
48
|
+
// @endif
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
2
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
import { Counter } from './Counter'
|
|
5
|
+
// @if tanstack-query || swr || msw
|
|
6
|
+
import { Greeting } from './Greeting'
|
|
7
|
+
// @endif
|
|
8
|
+
|
|
9
|
+
export default function Page() {
|
|
10
|
+
return (
|
|
11
|
+
<Flex gap={2} alignItems="flex-start">
|
|
12
|
+
<Typography variant="h2">__TITLE__</Typography>
|
|
13
|
+
<Typography color="secondary">
|
|
14
|
+
A React Router app on the design system. Edit src/pages/index/Page.jsx, and make the themes
|
|
15
|
+
your own in src/theme/themes.js.
|
|
16
|
+
</Typography>
|
|
17
|
+
{/* @if tanstack-query || swr || msw */}
|
|
18
|
+
<Greeting />
|
|
19
|
+
{/* @endif */}
|
|
20
|
+
<Counter />
|
|
21
|
+
<Flex flexDirection="row" gap={1}>
|
|
22
|
+
<Button href="https://docs.soroush.tech">Components</Button>
|
|
23
|
+
<Button href="https://reactrouter.com" variant="outlined">
|
|
24
|
+
React Router
|
|
25
|
+
</Button>
|
|
26
|
+
</Flex>
|
|
27
|
+
</Flex>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
|
|
2
|
+
// @if tanstack-query
|
|
3
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
4
|
+
// @endif
|
|
5
|
+
// @if redux-toolkit
|
|
6
|
+
import { Provider as StoreProvider } from 'react-redux'
|
|
7
|
+
// @endif
|
|
8
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
9
|
+
// @if redux-toolkit
|
|
10
|
+
import { store } from '../../state/store'
|
|
11
|
+
// @endif
|
|
12
|
+
import { Providers } from '../../theme/Providers'
|
|
13
|
+
import Page from './Page'
|
|
14
|
+
|
|
15
|
+
/** The home page, inside the providers the app gives it. */
|
|
16
|
+
function renderPage() {
|
|
17
|
+
render(
|
|
18
|
+
<Providers>
|
|
19
|
+
{/* @if tanstack-query && redux-toolkit */}
|
|
20
|
+
<QueryClientProvider client={new QueryClient()}>
|
|
21
|
+
<StoreProvider store={store}>
|
|
22
|
+
<Page />
|
|
23
|
+
</StoreProvider>
|
|
24
|
+
</QueryClientProvider>
|
|
25
|
+
{/* @elif tanstack-query */}
|
|
26
|
+
<QueryClientProvider client={new QueryClient()}>
|
|
27
|
+
<Page />
|
|
28
|
+
</QueryClientProvider>
|
|
29
|
+
{/* @elif redux-toolkit */}
|
|
30
|
+
<StoreProvider store={store}>
|
|
31
|
+
<Page />
|
|
32
|
+
</StoreProvider>
|
|
33
|
+
{/* @else */}
|
|
34
|
+
<Page />
|
|
35
|
+
{/* @endif */}
|
|
36
|
+
</Providers>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('the home page', () => {
|
|
41
|
+
// Vitest has no globals here, so Testing Library cannot clean up on its own.
|
|
42
|
+
afterEach(cleanup)
|
|
43
|
+
|
|
44
|
+
it('shows the app title', () => {
|
|
45
|
+
renderPage()
|
|
46
|
+
expect(screen.getByRole('heading', { name: '__TITLE_STRING__' })).toBeTruthy()
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('counts each press of its button', () => {
|
|
50
|
+
renderPage()
|
|
51
|
+
fireEvent.click(screen.getByRole('button', { name: 'Count' }))
|
|
52
|
+
expect(screen.getByText('Count: 1')).toBeTruthy()
|
|
53
|
+
})
|
|
54
|
+
// @if msw
|
|
55
|
+
|
|
56
|
+
it('shows the greeting the mock API answers', async () => {
|
|
57
|
+
renderPage()
|
|
58
|
+
expect(await screen.findByText('Hello from the mock API')).toBeTruthy()
|
|
59
|
+
})
|
|
60
|
+
// @endif
|
|
61
|
+
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ErrorPage } from './pages/ErrorPage'
|
|
2
|
+
import { Layout } from './pages/Layout'
|
|
3
|
+
import AboutPage from './pages/about/Page'
|
|
4
|
+
// @if react-hook-form
|
|
5
|
+
import FormPage from './pages/form/Page'
|
|
6
|
+
// @endif
|
|
7
|
+
import HomePage from './pages/index/Page'
|
|
8
|
+
// @if sample-data
|
|
9
|
+
import ItemPage from './pages/item/Page'
|
|
10
|
+
import ItemsPage from './pages/items/Page'
|
|
11
|
+
// @endif
|
|
12
|
+
// @if sample-data && !tanstack-query && !swr
|
|
13
|
+
import { loader as itemLoader } from './pages/item/loader'
|
|
14
|
+
import { loader as itemsLoader } from './pages/items/loader'
|
|
15
|
+
// @endif
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The app's pages, by path, each in the Layout. A path with no page, and a page that fails, show
|
|
19
|
+
* the error page in its place. https://reactrouter.com/start/data/routing
|
|
20
|
+
*/
|
|
21
|
+
export const routes = [
|
|
22
|
+
{
|
|
23
|
+
path: '/',
|
|
24
|
+
element: <Layout />,
|
|
25
|
+
children: [
|
|
26
|
+
{
|
|
27
|
+
errorElement: <ErrorPage />,
|
|
28
|
+
children: [
|
|
29
|
+
{ index: true, element: <HomePage /> },
|
|
30
|
+
{ path: 'about', element: <AboutPage /> },
|
|
31
|
+
// @if sample-data && (tanstack-query || swr)
|
|
32
|
+
{ path: 'items', element: <ItemsPage /> },
|
|
33
|
+
{ path: 'items/:id', element: <ItemPage /> },
|
|
34
|
+
// @elif sample-data
|
|
35
|
+
{ path: 'items', loader: itemsLoader, element: <ItemsPage /> },
|
|
36
|
+
{ path: 'items/:id', loader: itemLoader, element: <ItemPage /> },
|
|
37
|
+
// @endif
|
|
38
|
+
// @if react-hook-form
|
|
39
|
+
{ path: 'form', element: <FormPage /> },
|
|
40
|
+
// @endif
|
|
41
|
+
{ path: '*', element: <ErrorPage /> },
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import react from '@vitejs/plugin-react'
|
|
2
|
+
// @if msw
|
|
3
|
+
import { msw } from 'msw/vite'
|
|
4
|
+
// @endif
|
|
5
|
+
import { defineConfig } from 'vite'
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
// @if msw
|
|
9
|
+
plugins: [react(), msw({ mode: 'worker-only' })],
|
|
10
|
+
// @else
|
|
11
|
+
plugins: [react()],
|
|
12
|
+
// @endif
|
|
13
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import react from '@vitejs/plugin-react'
|
|
2
|
+
import { defineConfig } from 'vitest/config'
|
|
3
|
+
|
|
4
|
+
// The tests render components in jsdom, with no router: a page is a component like any other.
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
plugins: [react()],
|
|
7
|
+
// @if msw
|
|
8
|
+
// The mock API answers the tests' requests, from Node, as it answers the app's in the browser.
|
|
9
|
+
test: { environment: 'jsdom', setupFiles: ['./src/mocks/setupTests.js'] },
|
|
10
|
+
// @else
|
|
11
|
+
test: { environment: 'jsdom' },
|
|
12
|
+
// @endif
|
|
13
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// @if ts
|
|
2
|
+
/** One sample item. */
|
|
3
|
+
|
|
4
|
+
// @endif
|
|
5
|
+
/**
|
|
6
|
+
// @if tanstack-query || swr
|
|
7
|
+
* Sample data, so the items pages have something to show: src/data/api.js answers with it. Make
|
|
8
|
+
* src/data/api.js fetch from your API instead, and remove this file.
|
|
9
|
+
// @else
|
|
10
|
+
* Sample data, so the items pages have something to show: their loaders read it. Make the loaders
|
|
11
|
+
* fetch your own data instead, and remove this file.
|
|
12
|
+
// @endif
|
|
13
|
+
*/
|
|
14
|
+
export const items = [
|
|
15
|
+
{
|
|
16
|
+
id: 'theme',
|
|
17
|
+
title: 'Make the theme your own',
|
|
18
|
+
summary: 'Change the colors and fonts of both themes in src/theme/themes.js.',
|
|
19
|
+
status: 'doing',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: 'fonts',
|
|
23
|
+
title: 'Load the fonts',
|
|
24
|
+
summary: 'The theme names Space Grotesk and JetBrains Mono, and does not load them.',
|
|
25
|
+
status: 'todo',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: 'pages',
|
|
29
|
+
title: 'Add a page',
|
|
30
|
+
summary: 'A component in src/pages, and a route for it in src/routes.jsx, is a new page.',
|
|
31
|
+
status: 'todo',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'data',
|
|
35
|
+
title: 'Read your own data',
|
|
36
|
+
// @if tanstack-query || swr
|
|
37
|
+
summary: 'src/data/api.js answers with this file. Make it fetch from your API.',
|
|
38
|
+
// @else
|
|
39
|
+
summary: 'Each loader here reads this file. Make it fetch from your API.',
|
|
40
|
+
// @endif
|
|
41
|
+
status: 'todo',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: 'build',
|
|
45
|
+
title: 'Build it',
|
|
46
|
+
summary: 'The build writes the app to dist: one HTML page, and the scripts that render it.',
|
|
47
|
+
status: 'done',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: 'deploy',
|
|
51
|
+
title: 'Deploy it',
|
|
52
|
+
summary: 'Any static host serves dist, once it answers every path with index.html.',
|
|
53
|
+
status: 'todo',
|
|
54
|
+
},
|
|
55
|
+
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
3
|
+
// @if tanstack-query
|
|
4
|
+
import { useQuery } from '@tanstack/react-query'
|
|
5
|
+
import { useParams } from 'react-router'
|
|
6
|
+
// @elif swr
|
|
7
|
+
import { useParams } from 'react-router'
|
|
8
|
+
import useSWR from 'swr'
|
|
9
|
+
// @else
|
|
10
|
+
import { useLoaderData } from 'react-router'
|
|
11
|
+
// @endif
|
|
12
|
+
import { RouterLink } from '../../components/RouterLink'
|
|
13
|
+
// @if tanstack-query || swr
|
|
14
|
+
import { fetchItem } from '../../data/api'
|
|
15
|
+
// @endif
|
|
16
|
+
import { StatusChip } from '../items/StatusChip'
|
|
17
|
+
// @if !tanstack-query && !swr
|
|
18
|
+
// @endif
|
|
19
|
+
|
|
20
|
+
export default function Page() {
|
|
21
|
+
// @if tanstack-query
|
|
22
|
+
const id = useParams().id
|
|
23
|
+
const { data: item, error } = useQuery({
|
|
24
|
+
queryKey: ['items', id],
|
|
25
|
+
queryFn: () => fetchItem(id),
|
|
26
|
+
// An id with no item has none on a second try either: the error page shows at once.
|
|
27
|
+
retry: false,
|
|
28
|
+
})
|
|
29
|
+
// An id with no item: the error page shows in this page's place.
|
|
30
|
+
if (error) throw error
|
|
31
|
+
if (!item) return <Typography color="secondary">Asking the API for the item...</Typography>
|
|
32
|
+
// @elif swr
|
|
33
|
+
const id = useParams().id
|
|
34
|
+
const { data: item, error } = useSWR(['items', id], () => fetchItem(id))
|
|
35
|
+
// An id with no item: the error page shows in this page's place.
|
|
36
|
+
if (error) throw error
|
|
37
|
+
if (!item) return <Typography color="secondary">Asking the API for the item...</Typography>
|
|
38
|
+
// @else
|
|
39
|
+
const item = useLoaderData()
|
|
40
|
+
// @endif
|
|
41
|
+
return (
|
|
42
|
+
<Flex gap={2} alignItems="flex-start">
|
|
43
|
+
<Typography variant="h2">{item.title}</Typography>
|
|
44
|
+
<StatusChip status={item.status} />
|
|
45
|
+
<Typography color="secondary">{item.summary}</Typography>
|
|
46
|
+
<RouterLink to="/items">All items</RouterLink>
|
|
47
|
+
</Flex>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// @if !tanstack-query && !swr
|
|
2
|
+
import { data } from 'react-router'
|
|
3
|
+
import { items } from '../../data/items'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* One item's data, by the id in the URL, or the error page for an id with no item. The router
|
|
7
|
+
* runs it before it shows the page. https://reactrouter.com/start/data/data-loading
|
|
8
|
+
*/
|
|
9
|
+
export function loader({ params }) {
|
|
10
|
+
const item = items.find(({ id }) => id === params.id)
|
|
11
|
+
if (!item) throw data(`There is no item ${params.id}.`, { status: 404 })
|
|
12
|
+
return item
|
|
13
|
+
}
|
|
14
|
+
// @endif
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
2
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
3
|
+
// @if tanstack-query
|
|
4
|
+
import { useQuery } from '@tanstack/react-query'
|
|
5
|
+
// @elif swr
|
|
6
|
+
import useSWR from 'swr'
|
|
7
|
+
// @else
|
|
8
|
+
import { useLoaderData } from 'react-router'
|
|
9
|
+
// @endif
|
|
10
|
+
import { RouterLink } from '../../components/RouterLink'
|
|
11
|
+
// @if tanstack-query || swr
|
|
12
|
+
import { fetchItems } from '../../data/api'
|
|
13
|
+
// @else
|
|
14
|
+
// @endif
|
|
15
|
+
import { StatusChip } from './StatusChip'
|
|
16
|
+
|
|
17
|
+
export default function Page() {
|
|
18
|
+
// @if tanstack-query
|
|
19
|
+
const { data: items = [] } = useQuery({ queryKey: ['items'], queryFn: fetchItems })
|
|
20
|
+
// @elif swr
|
|
21
|
+
const { data: items = [] } = useSWR('items', fetchItems)
|
|
22
|
+
// @else
|
|
23
|
+
const items = useLoaderData()
|
|
24
|
+
// @endif
|
|
25
|
+
return (
|
|
26
|
+
<Flex gap={2}>
|
|
27
|
+
<Typography variant="h2">Items</Typography>
|
|
28
|
+
<Typography color="secondary">
|
|
29
|
+
{/* @if tanstack-query */}
|
|
30
|
+
Sample data from src/data/items.js, which this page reads through src/data/api.js with a
|
|
31
|
+
query of TanStack Query.
|
|
32
|
+
{/* @elif swr */}
|
|
33
|
+
Sample data from src/data/items.js, which this page reads through src/data/api.js with SWR.
|
|
34
|
+
{/* @else */}
|
|
35
|
+
Sample data from src/data/items.js, which this page's loader reads before it is shown.
|
|
36
|
+
{/* @endif */}
|
|
37
|
+
</Typography>
|
|
38
|
+
<Flex gap={1}>
|
|
39
|
+
{items.map((item) => (
|
|
40
|
+
<Flex key={item.id} flexDirection="row" alignItems="center" gap={2}>
|
|
41
|
+
<RouterLink to={`/items/${item.id}`}>{item.title}</RouterLink>
|
|
42
|
+
<StatusChip status={item.status} />
|
|
43
|
+
</Flex>
|
|
44
|
+
))}
|
|
45
|
+
</Flex>
|
|
46
|
+
</Flex>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Chip } from '@soroush.tech/design-system/Chip'
|
|
2
|
+
|
|
3
|
+
const COLORS = { todo: 'default', doing: 'primary', done: 'success' }
|
|
4
|
+
|
|
5
|
+
/** An item's status, in its palette color. */
|
|
6
|
+
export function StatusChip({ status }) {
|
|
7
|
+
return <Chip label={status} size="sm" variant="outlined" color={COLORS[status]} />
|
|
8
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// @if !tanstack-query && !swr
|
|
2
|
+
import { items } from '../../data/items'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The list page's data: what it shows of each item. The router runs it before it shows the page.
|
|
6
|
+
* https://reactrouter.com/start/data/data-loading
|
|
7
|
+
*/
|
|
8
|
+
export function loader() {
|
|
9
|
+
return items.map(({ id, title, status }) => ({ id, title, status }))
|
|
10
|
+
}
|
|
11
|
+
// @endif
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Button } from '@soroush.tech/design-system/Button'
|
|
2
|
+
import { Flex } from '@soroush.tech/design-system/Flex'
|
|
3
|
+
import { Typography } from '@soroush.tech/design-system/Typography'
|
|
4
|
+
import { useAtom } from 'jotai'
|
|
5
|
+
import { countAtom } from '../../state/counter'
|
|
6
|
+
|
|
7
|
+
/** A count, held in a Jotai atom, src/state/counter, and the button that adds to it. */
|
|
8
|
+
export function Counter() {
|
|
9
|
+
const [count, setCount] = useAtom(countAtom)
|
|
10
|
+
const increment = () => setCount(count + 1)
|
|
11
|
+
return (
|
|
12
|
+
<Flex flexDirection="row" alignItems="center" gap={2}>
|
|
13
|
+
<Button size="sm" variant="outlined" onClick={increment}>
|
|
14
|
+
Count
|
|
15
|
+
</Button>
|
|
16
|
+
<Typography>Count: {count}</Typography>
|
|
17
|
+
</Flex>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { http, HttpResponse } from 'msw'
|
|
2
|
+
// @if sample-data && (tanstack-query || swr)
|
|
3
|
+
import { items } from '../data/items'
|
|
4
|
+
// @endif
|
|
5
|
+
|
|
6
|
+
/** The mock API: what it answers, by method and path. Add a handler for each call the app makes. */
|
|
7
|
+
export const handlers = [
|
|
8
|
+
http.get('/api/greeting', () => HttpResponse.json({ message: 'Hello from the mock API' })),
|
|
9
|
+
// @if react-hook-form
|
|
10
|
+
http.post('/api/messages', async ({ request }) => {
|
|
11
|
+
const { name } = await request.json()
|
|
12
|
+
return HttpResponse.json({ message: `Thanks, ${name}: the mock API took your message.` })
|
|
13
|
+
}),
|
|
14
|
+
// @endif
|
|
15
|
+
// @if sample-data && (tanstack-query || swr)
|
|
16
|
+
http.get('/api/items', () =>
|
|
17
|
+
HttpResponse.json(items.map(({ id, title, status }) => ({ id, title, status })))
|
|
18
|
+
),
|
|
19
|
+
http.get('/api/items/:id', ({ params }) => {
|
|
20
|
+
const item = items.find(({ id }) => id === params.id)
|
|
21
|
+
return item ? HttpResponse.json(item) : new HttpResponse(null, { status: 404 })
|
|
22
|
+
}),
|
|
23
|
+
// @endif
|
|
24
|
+
]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll } from 'vitest'
|
|
2
|
+
import { server } from './server'
|
|
3
|
+
|
|
4
|
+
// The mock API answers every request a test makes; one it has no handler for fails the test.
|
|
5
|
+
beforeAll(() => server.listen({ onUnhandledFrame: 'error' }))
|
|
6
|
+
afterEach(() => server.resetHandlers())
|
|
7
|
+
afterAll(() => server.close())
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts the mock API in the browser while VITE_MSW, in .env, is true. Set it to false once the
|
|
3
|
+
* app's own API answers.
|
|
4
|
+
*/
|
|
5
|
+
export async function startMocks() {
|
|
6
|
+
if (import.meta.env.VITE_MSW !== 'true') return
|
|
7
|
+
const { worker } = await import('./browser')
|
|
8
|
+
// A request the mock API has no handler for - a module, an image - goes to the network.
|
|
9
|
+
await worker.start({ onUnhandledFrame: 'bypass' })
|
|
10
|
+
}
|