mirror of
https://github.com/LukeHagar/website.git
synced 2025-12-10 04:22:18 +00:00
107 lines
2.8 KiB
Plaintext
107 lines
2.8 KiB
Plaintext
---
|
|
layout: tutorial
|
|
title: Create ideas page
|
|
description: Add database queries and pagination using Appwrite in your React application.
|
|
step: 7
|
|
---
|
|
|
|
Using the `useIdeas` hook we can now display the ideas on the page. We will also add a form to submit new ideas.
|
|
|
|
Overwrite the contents of `src/pages/Home.jsx` with the following:
|
|
|
|
```client-web
|
|
import { useState } from "react";
|
|
import { useUser } from "../lib/context/user";
|
|
import { useIdeas } from "../lib/context/ideas";
|
|
|
|
export function Home() {
|
|
const user = useUser();
|
|
const ideas = useIdeas();
|
|
|
|
const [title, setTitle] = useState("");
|
|
const [description, setDescription] = useState("");
|
|
|
|
return (
|
|
<>
|
|
{/* Show the submit form to logged in users. */}
|
|
{user.current ? (
|
|
<section>
|
|
<h2>Submit Idea</h2>
|
|
<form>
|
|
<input
|
|
type="text"
|
|
placeholder="Title"
|
|
value={title}
|
|
onChange={(event) => {
|
|
setTitle(event.target.value);
|
|
}}
|
|
/>
|
|
<textarea
|
|
placeholder="Description"
|
|
value={description}
|
|
onChange={(event) => {
|
|
setDescription(event.target.value);
|
|
}}
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={() =>
|
|
ideas.add({ userId: user.current.$id, title, description })
|
|
}
|
|
>
|
|
Submit
|
|
</button>
|
|
</form>
|
|
</section>
|
|
) : (
|
|
<section>
|
|
<p>Please login to submit an idea.</p>
|
|
</section>
|
|
)}
|
|
<section>
|
|
<h2>Latest Ideas</h2>
|
|
<ul>
|
|
{ideas.current.map((idea) => (
|
|
<li key={idea.$id}>
|
|
<strong>{idea.title}</strong>
|
|
<p>{idea.description}</p>
|
|
{/* Show the remove button to idea owner. */}
|
|
{user.current && user.current.$id === idea.userId && (
|
|
<button type="button" onClick={() => ideas.remove(idea.$id)}>
|
|
Remove
|
|
</button>
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</section>
|
|
</>
|
|
);
|
|
}
|
|
```
|
|
|
|
In `src/App.jsx`, wrap the `main` element with the `IdeaProvider` component.
|
|
|
|
```client-web
|
|
import { Login } from "./pages/Login";
|
|
import { Home } from "./pages/Home";
|
|
import { UserProvider } from "./lib/context/user";
|
|
import { IdeaProvider } from "./lib/context/ideas";
|
|
|
|
function App() {
|
|
const isLoginPage = window.location.pathname === "/login";
|
|
|
|
return (
|
|
<div>
|
|
<UserProvider>
|
|
<IdeaProvider>
|
|
<Navbar /> {/* Add the navbar before page content */}
|
|
<main>{isLoginPage ? <Login /> : <Home />}</main>
|
|
</IdeaProvider>
|
|
<UserProvider>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default App;
|
|
``` |