Skip to content

Commit

Permalink
chore(examples): add suspense & network status checking
Browse files Browse the repository at this point in the history
  • Loading branch information
afiiif committed Sep 27, 2023
1 parent b678f00 commit 7e3d765
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/react/query-network-status/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

# Lock files
package-lock.json
pnpm-lock.yaml
yarn.lock
6 changes: 6 additions & 0 deletions examples/react/query-network-status/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Example

To run this example:

- `npm install`
- `npm run dev`
13 changes: 13 additions & 0 deletions examples/react/query-network-status/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Floppy Disk - Query & Network Status</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/index.jsx"></script>
</body>
</html>
84 changes: 84 additions & 0 deletions examples/react/query-network-status/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';

import { createQuery } from 'floppy-disk';

const usePsyduckQuery = createQuery(
async () => {
const res = await fetch('https://pokeapi.co/api/v2/pokemon/psyduck');
if (res.ok) return res.json();
throw res;
},
{
defaultDeps: (state) => [state.data, state.error, state.isWaiting],
onBeforeFetch: (cancel) => {
if (!navigator.onLine) cancel();
},
},
);

const useEeveeQuery = createQuery(
async () => {
const res = await fetch('https://pokeapi.co/api/v2/pokemon/eevee-x'); // Expected wrong URL, to simulate error
if (res.ok) return res.json();
throw res;
},
{
defaultDeps: (state) => [state.data, state.error, state.isWaiting],
retry: () => {
if (navigator.onLine) return 2;
return 0;
},
},
);

function App() {
const [showQuery1, setShowQuery1] = useState(false);
const [showQuery2, setShowQuery2] = useState(false);

return (
<main>
<h1>💾 Floppy Disk - Query & Network Status</h1>

<hr />
<h2>Example 1: Disable fetch when offline</h2>
<button onClick={() => setShowQuery1((p) => !p)}>Toggle query 1</button>
{showQuery1 && <Query1 />}

<hr />
<h2>Example 2: Disable retries when offline</h2>
<button onClick={() => setShowQuery2((p) => !p)}>Toggle query 2</button>
{showQuery2 && <Query2 />}
</main>
);
}

function Query1() {
const { data = {}, isWaiting } = usePsyduckQuery();
return (
<section>
<ul>
<li>ID: {data.id}</li>
<li>Name: {data.name}</li>
<li>Height: {data.height}</li>
</ul>
{isWaiting && <div>Fetching data... ⏳</div>}
</section>
);
}

function Query2() {
const { data = {}, isWaiting } = useEeveeQuery();
return (
<section>
<ul>
<li>ID: {data.id}</li>
<li>Name: {data.name}</li>
<li>Height: {data.height}</li>
</ul>
{isWaiting && <div>Fetching data... ⏳</div>}
</section>
);
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
22 changes: 22 additions & 0 deletions examples/react/query-network-status/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "floppy-disk-example",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"floppy-disk": "^2.6.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"vite": "^4.4.5"
}
}
35 changes: 35 additions & 0 deletions examples/react/suspense/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
36 changes: 36 additions & 0 deletions examples/react/suspense/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
11 changes: 11 additions & 0 deletions examples/react/suspense/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const metadata = {
title: 'Dependent Queries | Floppy Disk',
};

export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
63 changes: 63 additions & 0 deletions examples/react/suspense/app/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use client';

import { Suspense, useEffect, useState } from 'react';

import { createQuery } from 'floppy-disk';

const usePokemonQuery = createQuery(
async ({ pokemonName }) => {
const res = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonName}`);
if (res.ok) return res.json();
throw res;
},
{
enabled: ({ pokemonName }) => !!pokemonName,
onBeforeFetch: (_, state) => {
console.info(state);
},
},
);

export default function App() {
const [pokemonName, setPokemonName] = useState();

return (
<main>
<h1>💾 Floppy Disk - Suspense</h1>

<Suspense fallback={<div>Loading... ⏳</div>}>
<PokemonDetail pokemonName="pikachu" />
</Suspense>

<hr />

<div style={{ display: 'flex', gap: 8, margin: '1.75rem 0' }}>
<button onClick={() => setPokemonName('bulbasaur')}>bulbasaur</button>
<button onClick={() => setPokemonName('charmander')}>charmander</button>
<button onClick={() => setPokemonName('squirtle')}>squirtle</button>
</div>

{pokemonName ? (
<Suspense fallback={<div>Loading... ⏳</div>}>
<PokemonDetail pokemonName={pokemonName} />
</Suspense>
) : (
<h2>Select a pokemon...</h2>
)}
</main>
);
}

function PokemonDetail({ pokemonName }) {
const { data } = usePokemonQuery.suspend({ pokemonName });
return (
<>
<h2>{data.name}</h2>
<ul>
<li>Order: {data.order}</li>
<li>Height: {data.height}</li>
<li>Weight: {data.weight}</li>
</ul>
</>
);
}
4 changes: 4 additions & 0 deletions examples/react/suspense/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}

module.exports = nextConfig
17 changes: 17 additions & 0 deletions examples/react/suspense/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "floppy-disk-example",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"floppy-disk": "^2.6.0",
"next": "13.5.2",
"react": "18.2.0",
"react-dom": "18.2.0"
}
}

1 comment on commit 7e3d765

@vercel
Copy link

@vercel vercel bot commented on 7e3d765 Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

demo-floppy-disk – ./comparison/nextjs/with-floppy-disk

demo-floppy-disk.vercel.app
demo-floppy-disk-git-main-afiiif.vercel.app
demo-floppy-disk-afiiif.vercel.app

Please sign in to comment.