Skip to content

Commit 11aec6c

Browse files
committed
chore(fe/git): bug type shit
1 parent 11b626f commit 11aec6c

File tree

8 files changed

+42
-119
lines changed

8 files changed

+42
-119
lines changed

spez-frontend/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/package-lock.json
99
# testing
1010
/coverage
11-
11+
.env
1212
# next.js
1313
/.next/
1414
/out/
+17-68
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,36 @@
11
"use client"
2-
<<<<<<< HEAD
3-
import svPost from "../../../utils/svPost"
4-
=======
52
import svPost from "@/services/svPost";
63
import svCmt from "@/services/svCmt";
7-
>>>>>>> 5a06110 (chore(fe): split service)
84
import Link from "next/link";
95
import Comment from "@/components/Comment";
106
import Parser from 'html-react-parser'
11-
import Image from "next/image";
12-
import EagleEmoji from '../../../../public/eagle_emoji.png'
137
import CmtForm from "@/components/CmtForm";
148
import { getRelativeTime } from "@/utils/getRelativeTime";
15-
import axios from "axios";
169
import { GoTrash } from "react-icons/go";
17-
<<<<<<< HEAD
18-
import { useEffect } from "react";
19-
=======
2010
import { IPost, IComment } from "@/types";
2111
import LikeButton from "@/components/LikeButton";
2212
import svLike from "@/services/svLike";
23-
>>>>>>> 5a06110 (chore(fe): split service)
2413
type Props = {
2514
params: { id: string };
2615
};
16+
// interface getILike{
17+
// like: number
18+
// }
2719
export default async function Post({ params }: Props) {
2820
const id: string = params.id;
29-
<<<<<<< HEAD
30-
const { getPost, getLike, getComment, delPost } = await svPost();
31-
useEffect(aysnc () => {
32-
const info: any = await getPost(id);
33-
const likes: any = await getLike(id);
34-
const comment: any = await getComment(id);
35-
const author_url = "/user/" + info.author.id;
36-
37-
},[])
38-
const handleLikeButton = async (e: React.FormEvent) => {
39-
e.preventDefault();
40-
try {
41-
// Send a POST request to your FastAPI backend
42-
let token: any
43-
if (typeof window !== 'undefined')
44-
token = localStorage.getItem('jwt')
45-
await axios.post(`http://spezbe.hungnq.online/likes/?post_id=${id}`,
46-
{
47-
headers: {
48-
'accept': 'application/json',
49-
'Authorization': `Bearer ${token}`
50-
}
51-
}
52-
);
53-
54-
// Redirect or give feedback upon success
55-
// alert('Post created successfully!');
56-
} catch (error) {
57-
console.error('Error posting data:', error);
58-
alert('Failed to create like.');
59-
}
60-
};
61-
const handleDelPost = async (e: React.FormEvent) => {
62-
let token: any
63-
=======
6421
const { getPost, delPost } = svPost();
65-
const {getLike} = svLike();
66-
const {getComment} = svCmt()
22+
const {getComment} = svCmt();
23+
const {getLike} = svLike()
6724
const info: IPost = await getPost(id);
6825
const likes: number = await getLike(id);
6926
console.log(likes)
7027
const comment: IComment[] = await getComment(id);
7128
const author_url = "/user/" + info.author.id;
7229
const handleDelPost = async () => {
7330
let token: string | null = null
74-
>>>>>>> 5a06110 (chore(fe): split service)
7531
if (typeof window !== 'undefined')
7632
token = localStorage.getItem('jwt')
77-
const deletePost:any = await delPost(id, token);
78-
console.log(deletePost)
33+
await delPost(id, token);
7934

8035
}
8136
return (
@@ -93,32 +48,26 @@ export default async function Post({ params }: Props) {
9348
<div className="pl-2 text-2xl font-bold">{info.title}</div>
9449
<div className="p-2">{Parser(info.content)}</div>
9550
<div className="flex flex-row">
96-
<button onClick={handleLikeButton} className="w-auto flex flex-row border-2 border-black rounded-full px-2">
97-
<p className="pt-[0.35rem]"> Ưng</p>
98-
<Image
99-
src={EagleEmoji}
100-
alt = "eagle_img"
101-
width={30}
102-
height={50} />
103-
<p className="pt-[0.35rem]">: {likes} </p>
104-
</button>
51+
<LikeButton initialLikes={likes} post_id={id} />
10552
<div className="w-auto mt-[0.35rem] ml-5 border-2 rounded-full px-2 border-black">Phản hồi: {comment.length}</div>
106-
</div>
53+
</div>
10754
<CmtForm postId={id} />
10855
</div>
10956
<div className="border-2 border-slate-950 p-2 text-black rounded shadow h-full w-1/2 space-y-5">
11057
<a className="font-bold text-2xl">Bình Loạn</a>
111-
{comment.map((e: any) => (
58+
{comment.map((element: IComment) => (
11259
<Comment
113-
// author_id = {comment.author}
114-
id = {e.id}
115-
author = {e.author}
116-
content= {e.content}
117-
date = {e.created_at}
60+
key={element.id}
61+
id = {element.id}
62+
author = {element.author}
63+
content= {element.content}
64+
created_at = {element.created_at}
65+
updated_at= {element.updated_at}
66+
post_id= {element.post_id}
11867
/>
11968
))}
12069
</div>
12170
</div>
12271
</>
12372
);
124-
}
73+
}

spez-frontend/src/components/CmtForm.tsx

-16
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,7 @@ export default function CmtForm({ postId }: Props) {
2727
if (typeof window !== "undefined") token = localStorage.getItem("jwt");
2828
if (token === undefined) router.push("/");
2929
// console.log(token)
30-
<<<<<<< HEAD
31-
await axios.post(
32-
`https://spezbe.hungnq.online/posts/cmt/${postId}`,
33-
{
34-
content: content,
35-
},
36-
{
37-
headers: {
38-
accept: "application/json",
39-
Authorization: `Bearer ${token}`,
40-
},
41-
}
42-
);
43-
44-
=======
4530
await createComment(postId, token, content)
46-
>>>>>>> 5a06110 (chore(fe): split service)
4731
// Redirect or give feedback upon success
4832
alert("Post created successfully!");
4933
} catch (error) {

spez-frontend/src/components/LoginForm.tsx

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"use client";
22
import { useState, FormEvent } from "react";
33
import LoadingSingle from "@/components/LoadingSingle";
4-
// import { useRouter } from "next/navigation";
5-
4+
import svAuth from "@/services/svAuth";
65
export default function LoginForm() {
7-
// const router = useRouter();
86
const [username, setUsername] = useState<string>("");
97
const [password, setPassword] = useState<string>("");
108
const [showPassword, setShowPassword] = useState<boolean | null>(false);
119
const [isLoading, setIsLoading] = useState<boolean | undefined>(false);
1210
const [error, setError] = useState("");
11+
const {authenticate} = svAuth();
1312
const togglePasswordVisibility = () => {
1413
setShowPassword((prev: boolean | null) => !prev);
1514
};
@@ -24,13 +23,7 @@ export default function LoginForm() {
2423
// console.log(body)
2524
// API call to authenticate using x-www-form-urlencoded
2625
try {
27-
const res = await fetch('https://spezbe.hungnq.online/token', {
28-
method: 'POST',
29-
headers: {
30-
'Content-Type': 'application/x-www-form-urlencoded',
31-
},
32-
body: body.toString(),
33-
});
26+
const res = await authenticate(body.toString())
3427

3528
if (!res.ok) {
3629
const errorData = await res.json();

spez-frontend/src/components/PostForm.tsx

-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import React, { useState } from 'react';
33
// import dynamic from 'next/dynamic';
44
import SunEditor from 'suneditor-react';
55
import 'suneditor/dist/css/suneditor.min.css';
6-
import axios from 'axios';
76
import { useRouter } from 'next/navigation';
87
import svPost from '@/services/svPost';
98
// Dynamically import SunEditor to avoid SSR issues
@@ -26,24 +25,7 @@ export default function PostForm () {
2625
token = localStorage.getItem('jwt')
2726
if(token === undefined)
2827
router.push('/')
29-
<<<<<<< HEAD
30-
await axios.post('https://spezbe.hungnq.online/posts/',
31-
{
32-
33-
title: title,
34-
content: content,
35-
},
36-
{
37-
headers: {
38-
'accept': 'application/json',
39-
'Authorization': `Bearer ${token}`
40-
}
41-
}
42-
);
43-
44-
=======
4528
await createPost(title, content, token)
46-
>>>>>>> 5a06110 (chore(fe): split service)
4729
// Redirect or give feedback upon success
4830
alert('Post created successfully!');
4931
router.push('/'); // Redirect to the homepage or a posts page
@@ -92,4 +74,3 @@ export default function PostForm () {
9274
</div>
9375
);
9476
};
95-

spez-frontend/src/services/svAuth.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// import axios from "axios";
2+
import { BE_URI } from "@/utils/constants";
3+
4+
export default function svAuth() {
5+
const API = {
6+
token: BE_URI + "/token",
7+
};
8+
async function authenticate(body: string) {
9+
const data = await fetch(API.token,{
10+
method: "POST",
11+
headers: {
12+
"Content-type": "application/x-www-form-urlencoded",
13+
},
14+
body: body
15+
})
16+
return data
17+
}
18+
return {
19+
authenticate
20+
}
21+
}

spez-frontend/src/services/svPost.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { BE_URI } from "@/utils/constants";
22
import axios from "axios";
3-
import { headers } from "next/headers";
43
export default function svPost() {
54
const API = {
65
all: BE_URI + "/posts",

spez-frontend/src/utils/constants.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
<<<<<<< HEAD
2-
export const BE_URI = 'https://spezbe.hungnq.online'
3-
=======
41
export const BE_URI = process.env.NEXT_PUBLIC_SERVER_URL
5-
>>>>>>> 5a06110 (chore(fe): split service)

0 commit comments

Comments
 (0)