Skip to content

Commit

Permalink
integrate frontend and backend
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAmi committed Dec 24, 2023
1 parent 61eb92e commit b41f855
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 3 deletions.
9 changes: 9 additions & 0 deletions frontend/src/components/pages/Edit.tsx
@@ -0,0 +1,9 @@
import {Link} from "@tanstack/react-router";

export const Edit = () => {
return (
<>
<Link search={() => {}} to="/">戻る</Link>
</>
)
}
33 changes: 32 additions & 1 deletion frontend/src/components/pages/Index.tsx
@@ -1,5 +1,36 @@
import {useQuery} from "@tanstack/react-query";
import axios from "axios";
import {Link} from "@tanstack/react-router";

type ApiResponse = {
id: number,
name: string,
content: string,
updated_at: string
}

const BASE_URL = 'http://localhost:8000'

export const Index = () => {
const {isLoading, data} = useQuery({
queryKey: ['diaries'],
queryFn: async () => {
const response = await axios.get<ApiResponse[]>(`${BASE_URL}/api/diaries/`)
return response.data
}
})

if (isLoading) return <div>Loading</div>

return (
<p>hello</p>
<>
<h1>一覧</h1>
<ul>
{data.map((d)=> {
// search propsが必須なので、空の関数を渡しておく
return <li key={d.id}><Link search={() => {}} to={`${BASE_URL}/${d.id}`}>{d.name}</Link></li>
})}
</ul>
</>
)
}
16 changes: 14 additions & 2 deletions frontend/src/main.tsx
Expand Up @@ -4,6 +4,8 @@ import 'vite/modulepreload-polyfill'; // required for vite entrypoints
import {Outlet, RootRoute, Route, Router, RouterProvider} from "@tanstack/react-router";
import {TanStackRouterDevtools} from '@tanstack/router-devtools'
import {Index} from "./components/pages/Index";
import {QueryClient, QueryClientProvider} from "@tanstack/react-query";
import {Edit} from "./components/pages/Edit";

const rootRoute = new RootRoute(
{
Expand All @@ -23,11 +25,21 @@ const indexRoot = new Route({
component: Index
})

const routeTree = rootRoute.addChildren([indexRoot])
const diaryRoot = new Route({
getParentRoute: () => rootRoute,
path: '/$diaryId',
component: Edit
})

const routeTree = rootRoute.addChildren([indexRoot.addChildren([diaryRoot])])
const router = new Router({ routeTree })

const queryClient = new QueryClient()

ReactDOM.createRoot(document.getElementById('root')!).render(
<StrictMode>
<RouterProvider router={router} />
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>
</StrictMode>,
)
116 changes: 116 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -10,8 +10,10 @@
"preview": "vite preview"
},
"dependencies": {
"@tanstack/react-query": "^5.14.6",
"@tanstack/react-router": "^1.0.0",
"@tanstack/router-devtools": "^1.0.0",
"axios": "^1.6.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down

0 comments on commit b41f855

Please sign in to comment.