Skip to content

Commit

Permalink
examples: history-sync-transit init
Browse files Browse the repository at this point in the history
  • Loading branch information
AkifumiSato committed Jul 24, 2022
1 parent 237532a commit f5cb63d
Show file tree
Hide file tree
Showing 20 changed files with 427 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/history-sync-transit/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": "next/core-web-vitals"
}
38 changes: 38 additions & 0 deletions examples/history-sync-transit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo

# yarn
yarn.lock
8 changes: 8 additions & 0 deletions examples/history-sync-transit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## history-sync-json

Example of `RecoilHistorySyncJSONNext`.

```bash
# run example application
yarn dev
```
5 changes: 5 additions & 0 deletions examples/history-sync-transit/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
11 changes: 11 additions & 0 deletions examples/history-sync-transit/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
experimental: {
scrollRestoration: true,
newNextLinkBehavior: true,
},
}

module.exports = nextConfig
19 changes: 19 additions & 0 deletions examples/history-sync-transit/package-copy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

echo "[recoil-sync-next] copy to current directory..."

CURRENT=`pwd`
cd ../../
yarn clean-build
yarn pack --filename recoil-sync-next-local.tgz

cd $CURRENT/node_modules
rm -Rf recoil-sync-next/
tar zxf ../../../recoil-sync-next-local.tgz
mv package/ recoil-sync-next/

cd ..
rm -Rf .next/

echo "[recoil-sync-next] copy success!"
exit $?
34 changes: 34 additions & 0 deletions examples/history-sync-transit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "history-sync-json",
"version": "0.1.0",
"private": true,
"scripts": {
"inst": "yarn install",
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint:eslint": "next lint",
"lint:tsc": "tsc --noEmit -p tsconfig.json",
"lint-fix:eslint": "next lint --fix",
"lint-fix:prettier": "prettier \"{pages,src,styles}/**/*.{ts,tsx,css}\" --write",
"local-build": "yarn run local-copy && yarn run build",
"local-dev": "yarn run local-copy && yarn run dev",
"local-copy": "./package-copy.sh"
},
"dependencies": {
"next": "^12.2.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"recoil": "^0.7.4",
"recoil-sync": "^0.1.0",
"recoil-sync-next": "latest"
},
"devDependencies": {
"@types/node": "^18.0.3",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"eslint": "^8.19.0",
"eslint-config-next": "^12.2.2",
"typescript": "^4.7.4"
}
}
17 changes: 17 additions & 0 deletions examples/history-sync-transit/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import '../styles/globals.css'
import { RecoilRoot } from 'recoil'
import { RecoilHistorySyncJSONNext } from 'recoil-sync-next'

import type { AppProps } from 'next/app'

function MyApp({ Component, pageProps }: AppProps) {
return (
<RecoilRoot>
<RecoilHistorySyncJSONNext>
<Component {...pageProps} />
</RecoilHistorySyncJSONNext>
</RecoilRoot>
)
}

export default MyApp
31 changes: 31 additions & 0 deletions examples/history-sync-transit/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Head from 'next/head'
import Link from 'next/link'

import { Counter } from '../src/components/Counter'
import { Links } from '../src/components/Links'
import { Textfield } from '../src/components/Textfield'
import styles from '../styles/Home.module.css'

import type { NextPage } from 'next'

const Home: NextPage = () => {
return (
<div className={styles.container}>
<Head>
<title>Top - Example of RecoilHistorySyncJSONNext</title>
<link rel="icon" href="/favicon.ico" />
</Head>

<main className={styles.main}>
<h1 className={styles.title}>Top page (Static)</h1>
<Textfield />
<Counter name="foo" initialValue={0} />
<Counter name="bar" initialValue={10} />
<Counter name="baz" initialValue={100} />
<Links />
</main>
</div>
)
}

export default Home
56 changes: 56 additions & 0 deletions examples/history-sync-transit/pages/ssg/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Head from 'next/head'

import { Counter } from '../../src/components/Counter'
import { Textfield } from '../../src/components/Textfield'
import { Links } from '../../src/components/Links'
import styles from '../../styles/Home.module.css'

import type { GetStaticPaths, GetStaticProps, NextPage } from 'next'

type Props = {
id: string
}

const SSGPage: NextPage<Props> = ({ id }) => {
return (
<div className={styles.container}>
<Head>
<title>SSG - Example of RecoilHistorySyncJSONNext</title>
</Head>

<main className={styles.main}>
<h1 className={styles.title}>SSG Page {id}</h1>
<Textfield />
<Counter name="foo" initialValue={0} />
<Counter name="bar" initialValue={10} />
<Counter name="baz" initialValue={100} />
<Links />
</main>
</div>
)
}

export default SSGPage

export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: [
{ params: { id: '1' } },
{ params: { id: '2' } },
{ params: { id: '3' } },
],
fallback: false,
}
}

export const getStaticProps: GetStaticProps<Props, { id: string }> = async ({
params,
}) => {
if (params?.id === undefined) throw new Error('`params.id` is undefined.')

return {
props: {
id: params?.id,
},
}
}
46 changes: 46 additions & 0 deletions examples/history-sync-transit/pages/ssr/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Head from 'next/head'

import { Counter } from '../../src/components/Counter'
import { Links } from '../../src/components/Links'
import { Textfield } from '../../src/components/Textfield'
import styles from '../../styles/Home.module.css'

import type { GetServerSideProps, NextPage } from 'next'

type Props = {
id: string
}

const SSRPage: NextPage<Props> = ({ id }) => {
return (
<div className={styles.container}>
<Head>
<title>SSR - Example of RecoilHistorySyncJSONNext</title>
</Head>

<main className={styles.main}>
<h1 className={styles.title}>SSR Page {id}</h1>
<Textfield />
<Counter name="foo" initialValue={0} />
<Counter name="bar" initialValue={10} />
<Counter name="baz" initialValue={100} />
<Links />
</main>
</div>
)
}

export default SSRPage

export const getServerSideProps: GetServerSideProps<
Props,
{ id: string }
> = async ({ params }) => {
if (params?.id === undefined) throw new Error('`params.id` is undefined.')

return {
props: {
id: params?.id,
},
}
}
Binary file added examples/history-sync-transit/public/favicon.ico
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.wrapper {
display: flex;
justify-content: space-between;
width: 200px;
}
32 changes: 32 additions & 0 deletions examples/history-sync-transit/src/components/Counter/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { number } from '@recoiljs/refine'
import { useRecoilState } from 'recoil'
import { syncEffect } from 'recoil-sync'
import { atomFamilyWithInitialValue } from 'recoil-sync-next'

import styles from './index.module.css'

export const counter = atomFamilyWithInitialValue<number, string>({
key: 'counterState',
effects: [syncEffect({ refine: number() })],
})

export type Props = {
name: string
initialValue: number
}

export const Counter: React.FC<Props> = ({ name, initialValue }) => {
const [count, setCount] = useRecoilState(counter(name, initialValue))

return (
<div key={name} className={styles.wrapper}>
<div>
count[{name}]: {count}
</div>
<div>
<button onClick={() => setCount((prev) => prev + 1)}>+</button>
<button onClick={() => setCount((prev) => prev - 1)}>-</button>
</div>
</div>
)
}
44 changes: 44 additions & 0 deletions examples/history-sync-transit/src/components/Links/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Link from 'next/link'

export const Links: React.FC = () => {
return (
<dl>
<dt>Top</dt>
<dd>
<ul>
<li>
<Link href="/">Top</Link>
</li>
</ul>
</dd>
<dt>SSR</dt>
<dd>
<ul>
<li>
<Link href="/ssr/1">SSR 1</Link>
</li>
<li>
<Link href="/ssr/2">SSR 2</Link>
</li>
<li>
<Link href="/ssr/3">SSR 3</Link>
</li>
</ul>
</dd>
<dt>SSG</dt>
<dd>
<ul>
<li>
<Link href="/ssg/1">SSG 1</Link>
</li>
<li>
<Link href="/ssg/2">SSG 2</Link>
</li>
<li>
<Link href="/ssg/3">SSG 3</Link>
</li>
</ul>
</dd>
</dl>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.wrapper {
width: 200px;
}
21 changes: 21 additions & 0 deletions examples/history-sync-transit/src/components/Textfield/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { string } from '@recoiljs/refine'
import { atom, useRecoilState } from 'recoil'
import { syncEffect } from 'recoil-sync'

import styles from './index.module.css'

export const textState = atom<string>({
key: 'textState',
default: '',
effects: [syncEffect({ refine: string() })],
})

export const Textfield: React.FC = () => {
const [text, setText] = useRecoilState(textState)

return (
<div className={styles.wrapper}>
<input value={text} onChange={(e) => setText(e.currentTarget.value)} />
</div>
)
}
Loading

0 comments on commit f5cb63d

Please sign in to comment.