generated from lucien/api-template
add: added homepage
This commit is contained in:
parent
1f24907c80
commit
4ea8c9f2b3
11 changed files with 239 additions and 68 deletions
|
@ -1,37 +1,22 @@
|
|||
import { useParams } from "react-router-dom";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Channel_type, Messages } from "../types";
|
||||
import axios from "axios";
|
||||
|
||||
type Channel ={
|
||||
id: number,
|
||||
name: string
|
||||
description: string
|
||||
}
|
||||
|
||||
type Message = {
|
||||
id: number,
|
||||
user_id: number,
|
||||
username: string,
|
||||
content: string,
|
||||
date: number
|
||||
}
|
||||
|
||||
type Messages = Message[]
|
||||
|
||||
export default function Channel() {
|
||||
const { id } = useParams();
|
||||
const [channel, setChannel] = useState<Channel>();
|
||||
const { name } = useParams();
|
||||
const [channel, setChannel] = useState<Channel_type>();
|
||||
const [messages, setMessages] = useState<Messages>();
|
||||
|
||||
useEffect(() => {
|
||||
axios.get(`/api/channels/${id}`).then((res) => {
|
||||
axios.get(`/api/channels/${name}`).then((res) => {
|
||||
setChannel(res.data);
|
||||
});
|
||||
|
||||
axios.get(`/api/channels/${id}/messages`).then((res) => {
|
||||
axios.get(`/api/channels/${name}/messages`).then((res) => {
|
||||
setMessages(res.data);
|
||||
});
|
||||
}, [id]);
|
||||
}, [name]);
|
||||
|
||||
if (!channel || !messages) {
|
||||
return <div>Loading...</div>;
|
||||
|
|
|
@ -1,7 +1,56 @@
|
|||
import { useState, useEffect } from "react"
|
||||
import { Link } from "react-router-dom"
|
||||
import { Channels, User } from "../types"
|
||||
import axios from "axios"
|
||||
|
||||
export default function Home() {
|
||||
const [user, setUser] = useState<User>();
|
||||
const [channels, setChannels] = useState<Channels>();
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem("token")
|
||||
if (token) {
|
||||
axios.post("/api/auth/me", {
|
||||
token: token
|
||||
}).then((res) => {
|
||||
setUser(res.data)
|
||||
}).catch((err) => {
|
||||
console.error(err)
|
||||
})
|
||||
}
|
||||
|
||||
axios.get("/api/channels").then((res) => {
|
||||
setChannels(res.data)
|
||||
}).catch((err) => {
|
||||
console.error(err)
|
||||
})
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Home</h1>
|
||||
{user ? (
|
||||
<div>
|
||||
<p>Welcome {user.username}</p>
|
||||
<button onClick={() => {
|
||||
localStorage.removeItem("token")
|
||||
window.location.reload()
|
||||
}}>Logout</button>
|
||||
</div>
|
||||
) : (
|
||||
<div>
|
||||
<Link to="/login">Login</Link>
|
||||
<Link to="/register">Register</Link>
|
||||
</div>
|
||||
)}
|
||||
<h2>Channels</h2>
|
||||
<ul>
|
||||
{channels?.map((channel) => (
|
||||
<li key={channel.id}>
|
||||
<Link to={`/channels/${channel.name}`}>{channel.name}</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)
|
||||
}
|
45
front/src/pages/Login.tsx
Normal file
45
front/src/pages/Login.tsx
Normal file
|
@ -0,0 +1,45 @@
|
|||
import axios from "axios";
|
||||
import { useState } from "react";
|
||||
import { useNavigate, Link } from "react-router-dom";
|
||||
|
||||
export default function Login() {
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const navigate = useNavigate();
|
||||
|
||||
function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
axios
|
||||
.post("/api/auth/login", { username, password })
|
||||
.then((res) => {
|
||||
localStorage.setItem("token", res.data.token);
|
||||
navigate("/");
|
||||
})
|
||||
.catch((err) => {
|
||||
alert(err.response.data.message);
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Link to="/">Home</Link>
|
||||
<h1>Login</h1>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Username"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
/>
|
||||
<input
|
||||
type="password"
|
||||
placeholder="Password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
/>
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
<Link to="/register">Register</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
47
front/src/pages/Register.tsx
Normal file
47
front/src/pages/Register.tsx
Normal file
|
@ -0,0 +1,47 @@
|
|||
import axios from "axios";
|
||||
import { useState } from "react";
|
||||
import { useNavigate, Link } from "react-router-dom";
|
||||
|
||||
export default function Register () {
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const navigate = useNavigate();
|
||||
|
||||
function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
axios.post("/api/auth/register", { username, password });
|
||||
axios
|
||||
.post("/api/auth/login", { username, password })
|
||||
.then((res) => {
|
||||
localStorage.setItem("token", res.data.token);
|
||||
navigate("/");
|
||||
})
|
||||
.catch((err) => {
|
||||
alert(err.response.data.message);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Link to="/">Home</Link>
|
||||
<h1>Register</h1>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Username"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
/>
|
||||
<input
|
||||
type="password"
|
||||
placeholder="Password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
/>
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
<Link to="/login">Login</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue