Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add react router examples #166

Merged
merged 15 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/content/docs/examples/react-router-6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: React Router 6 Example
description: A simple example of using React Router 6 with OpenAPI React Query Codegen.
---

Example of using React Router 6 can be found in the [`examples/react-router-6-app`](https://github.com/7nohe/openapi-react-query-codegen/tree/main/examples/react-router-6-app) directory of the repository.
6 changes: 6 additions & 0 deletions docs/src/content/docs/examples/react-router-7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: React Router 7 Example
description: A simple example of using React Router 7 with OpenAPI React Query Codegen.
---

Example of using React Router 7 can be found in the [`examples/react-router-7-app`](https://github.com/7nohe/openapi-react-query-codegen/tree/main/examples/react-router-7-app) directory of the repository.
24 changes: 24 additions & 0 deletions examples/react-router-6-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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?
13 changes: 13 additions & 0 deletions examples/react-router-6-app/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>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions examples/react-router-6-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@7nohe/react-router-6-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "run-p dev:mock dev:client",
"dev:client": "vite --clearScreen=false",
"dev:mock": "prism mock ../petstore.yaml --dynamic",
"build": "tsc && vite build",
"preview": "vite preview",
"generate:api": "rimraf ./openapi && node ../../dist/cli.mjs -i ../petstore.yaml --format=biome --lint=biome",
"test:generated": "tsc -p ./tsconfig.json --noEmit"
},
"dependencies": {
"@hey-api/client-axios": "^0.2.7",
"@tanstack/react-query": "^5.59.13",
"@tanstack/react-query-devtools": "^5.32.1",
"axios": "^1.7.7",
"form-data": "~4.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.27.0"
},
"devDependencies": {
"@biomejs/biome": "^1.7.2",
"@stoplight/prism-cli": "^5.5.2",
"@types/react": "^18.3.1",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react": "^4.2.1",
"npm-run-all": "^4.1.5",
"typescript": "^5.4.5",
"vite": "^5.0.12"
}
}
1 change: 1 addition & 0 deletions examples/react-router-6-app/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions examples/react-router-6-app/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}

@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
94 changes: 94 additions & 0 deletions examples/react-router-6-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import "./App.css";
import type { QueryClient } from "@tanstack/react-query";
import { useState } from "react";
import { type LoaderFunctionArgs, useLoaderData } from "react-router-dom";
import { UseFindPetsKeyFn, useAddPet } from "../openapi/queries";
import { ensureUseFindPetsData } from "../openapi/queries/ensureQueryData";
import { useFindPetsSuspense } from "../openapi/queries/suspense";
import { queryClient } from "./queryClient";

export const loader =
(queryClient: QueryClient) => async (_: LoaderFunctionArgs) => {
const queryParameters = {
query: { tags: [], limit: 10 },
};

await ensureUseFindPetsData(queryClient, {
query: { tags: [], limit: 10 },
});
return queryParameters;
};

export function Compoment() {
const queryParameters = useLoaderData() as Awaited<
ReturnType<ReturnType<typeof loader>>
>;

const { data, error, refetch } = useFindPetsSuspense(queryParameters);

const { mutate: addPet, isError } = useAddPet();

const [text, setText] = useState<string>("");
const [errorText, setErrorText] = useState<string>();

if (error)
return (
<div>
<p>Failed to fetch pets</p>
<button type="button" onClick={() => refetch()}>
Retry
</button>
</div>
);

return (
<div className="App">
<h1>Pet List</h1>
<input
type="text"
value={text}
placeholder="Type pet name"
onChange={(e) => setText(e.target.value)}
/>
<button
type="button"
onClick={() => {
addPet(
{
body: { name: text },
},
{
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: UseFindPetsKeyFn(queryParameters),
});
console.log("success");
},
onError: (error) => {
console.log(error.message);
setErrorText(`Error: ${error.message}`);
},
},
);
}}
>
Create a pet
</button>
{isError && (
<p
style={{
color: "red",
}}
>
{errorText}
</p>
)}
<ul>
{Array.isArray(data) &&
data?.map((pet, index) => (
<li key={`${pet.id}-${index}`}>{pet.name}</li>
))}
</ul>
</div>
);
}
1 change: 1 addition & 0 deletions examples/react-router-6-app/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions examples/react-router-6-app/src/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { client } from "../openapi/requests/services.gen";

client.setConfig({
baseUrl: "http://localhost:4010",
throwOnError: true,
});
87 changes: 87 additions & 0 deletions examples/react-router-6-app/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
:root {
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
font-size: 16px;
line-height: 24px;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

input {
border-radius: 8px;
border: 1px solid #ccc;
padding: 0.5em;
font-size: 1em;
font-family: inherit;
background-color: #fff;
color: #000;
transition: border-color 0.25s;
margin: 1em;
}

input:focus {
border-color: #646cff;
outline: none;
}
26 changes: 26 additions & 0 deletions examples/react-router-6-app/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { Compoment, loader } from "./App";
import "./index.css";
import { QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { queryClient } from "./queryClient";
import "./axios";
import { RouterProvider, createBrowserRouter } from "react-router-dom";

const router = createBrowserRouter([
{
path: "/",
element: <Compoment />,
loader: loader(queryClient),
},
]);

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
<ReactQueryDevtools buttonPosition="bottom-left" />
</QueryClientProvider>
</React.StrictMode>,
);
3 changes: 3 additions & 0 deletions examples/react-router-6-app/src/queryClient.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { QueryClient } from "@tanstack/react-query";

export const queryClient = new QueryClient();
1 change: 1 addition & 0 deletions examples/react-router-6-app/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
25 changes: 25 additions & 0 deletions examples/react-router-6-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [
{
"path": "./tsconfig.node.json"
}
]
}
9 changes: 9 additions & 0 deletions examples/react-router-6-app/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}
Loading
Loading