Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
124 changes: 117 additions & 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"preview": "vite preview"
},
"dependencies": {
"@tanstack/react-query": "4.28",
"@tanstack/react-query-devtools": "4.28",
"axios": "^1.3.4",
"bootstrap": "^5.2.3",
"react": "^18.2.0",
Expand Down
12 changes: 10 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import './App.css';
import "./App.css";
import PostList from "./react-query/PostList";
import TodoForm from "./react-query/TodoForm";
import TodoList from "./react-query/TodoList";

function App() {
return <h1>React Starter Project</h1>;
return (
<>
<TodoForm />
<TodoList />
</>
);
}

export default App;
31 changes: 22 additions & 9 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import 'bootstrap/dist/css/bootstrap.css';
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';
import "bootstrap/dist/css/bootstrap.css";
import React from "react";
import ReactDOM from "react-dom/client";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import App from "./App";
import "./index.css";

ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
).render(
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: 3,
cacheTime: 300_000, //5 min
staleTime: 10 * 1000, //10S
},
},
});

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<App />
<QueryClientProvider client={queryClient}>
<App />
<ReactQueryDevtools />
</QueryClientProvider>
</React.StrictMode>
);
59 changes: 34 additions & 25 deletions src/react-query/PostList.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
import axios from 'axios';
import { useEffect, useState } from 'react';

interface Post {
id: number;
title: string;
body: string;
userId: number;
}
import axios from "axios";
import { useEffect, useState } from "react";
import usePosts from "./hooks/usePosts";
import React from "react";

const PostList = () => {
const [posts, setPosts] = useState<Post[]>([]);
const [error, setError] = useState('');
const pageSize = 10;

useEffect(() => {
axios
.get('https://jsonplaceholder.typicode.com/posts')
.then((res) => setPosts(res.data))
.catch((error) => setError(error));
}, []);
const {
data: posts,
error,
isLoading,
fetchNextPage,
isFetchingNextPage,
} = usePosts({ pageSize });

if (error) return <p>{error}</p>;
if (error) return <p>{error.message}</p>;

return (
<ul className="list-group">
{posts.map((post) => (
<li key={post.id} className="list-group-item">
{post.title}
</li>
))}
</ul>
<>
<ul className="list-group">
{posts?.pages.map((page, index) => (
<React.Fragment key={index}>
{page?.map((post) => (
<li key={post.id} className="list-group-item">
{post.title}
</li>
))}
</React.Fragment>
))}
</ul>

<button
disabled={isFetchingNextPage}
onClick={() => fetchNextPage()}
className="btn btn-primary my-3 ms-1"
>
{isFetchingNextPage ? "Loading.." : "Load More"}
</button>
</>
);
};

Expand Down
40 changes: 31 additions & 9 deletions src/react-query/TodoForm.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import { useRef } from 'react';
import { useRef } from "react";
import useAddTodo from "./hooks/useAddTodo";

const TodoForm = () => {
const ref = useRef<HTMLInputElement>(null);
const addTodo = useAddTodo(() => {
if (ref.current) ref.current.value = "";
});

return (
<form className="row mb-3">
<div className="col">
<input ref={ref} type="text" className="form-control" />
</div>
<div className="col">
<button className="btn btn-primary">Add</button>
</div>
</form>
<>
{addTodo.error && (
<div className="alert alert-danger">{addTodo.error.message}</div>
)}
<form
className="row mb-3"
onSubmit={(event) => {
event.preventDefault();

if (ref.current && ref.current.value)
addTodo.mutate({
id: 0,
title: ref.current?.value,
completed: false,
userId: 1,
});
}}
>
<div className="col">
<input ref={ref} type="text" className="form-control" />
</div>
<div className="col">
<button className="btn btn-primary">Add</button>
</div>
</form>
</>
);
};

Expand Down
19 changes: 5 additions & 14 deletions src/react-query/TodoList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import axios from 'axios';
import React, { useEffect, useState } from 'react';

import useTodos from "./hooks/useTodos";
interface Todo {
id: number;
title: string;
Expand All @@ -9,21 +7,14 @@ interface Todo {
}

const TodoList = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [error, setError] = useState('');

useEffect(() => {
axios
.get('https://jsonplaceholder.typicode.com/todos')
.then((res) => setTodos(res.data))
.catch((error) => setError(error));
}, []);
const { data: todos, error, isLoading } = useTodos();

if (error) return <p>{error}</p>;
if (isLoading) return <p>Loding...</p>;
if (error) return <p>{error.message}</p>;

return (
<ul className="list-group">
{todos.map((todo) => (
{todos?.map((todo) => (
<li key={todo.id} className="list-group-item">
{todo.title}
</li>
Expand Down
1 change: 1 addition & 0 deletions src/react-query/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CACHE_KEY_TODO = ["todos"];
Loading