Skip to content

Commit 11b626f

Browse files
committed
chore(fe): split service
1 parent da7fdaf commit 11b626f

16 files changed

+208
-109
lines changed

spez-frontend/.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NEXT_PUBLIC_SERVER_URL=

spez-frontend/src/app/post/[id]/page.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
"use client"
2+
<<<<<<< HEAD
23
import svPost from "../../../utils/svPost"
4+
=======
5+
import svPost from "@/services/svPost";
6+
import svCmt from "@/services/svCmt";
7+
>>>>>>> 5a06110 (chore(fe): split service)
38
import Link from "next/link";
49
import Comment from "@/components/Comment";
510
import Parser from 'html-react-parser'
@@ -9,12 +14,19 @@ import CmtForm from "@/components/CmtForm";
914
import { getRelativeTime } from "@/utils/getRelativeTime";
1015
import axios from "axios";
1116
import { GoTrash } from "react-icons/go";
17+
<<<<<<< HEAD
1218
import { useEffect } from "react";
19+
=======
20+
import { IPost, IComment } from "@/types";
21+
import LikeButton from "@/components/LikeButton";
22+
import svLike from "@/services/svLike";
23+
>>>>>>> 5a06110 (chore(fe): split service)
1324
type Props = {
1425
params: { id: string };
1526
};
1627
export default async function Post({ params }: Props) {
1728
const id: string = params.id;
29+
<<<<<<< HEAD
1830
const { getPost, getLike, getComment, delPost } = await svPost();
1931
useEffect(aysnc () => {
2032
const info: any = await getPost(id);
@@ -48,6 +60,18 @@ export default async function Post({ params }: Props) {
4860
};
4961
const handleDelPost = async (e: React.FormEvent) => {
5062
let token: any
63+
=======
64+
const { getPost, delPost } = svPost();
65+
const {getLike} = svLike();
66+
const {getComment} = svCmt()
67+
const info: IPost = await getPost(id);
68+
const likes: number = await getLike(id);
69+
console.log(likes)
70+
const comment: IComment[] = await getComment(id);
71+
const author_url = "/user/" + info.author.id;
72+
const handleDelPost = async () => {
73+
let token: string | null = null
74+
>>>>>>> 5a06110 (chore(fe): split service)
5175
if (typeof window !== 'undefined')
5276
token = localStorage.getItem('jwt')
5377
const deletePost:any = await delPost(id, token);

spez-frontend/src/app/user/[id]/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import svUser from "@/utils/svUser";
2+
import svUser from "@/services/svUser";
33
import ImageUploader from "@/components/ImgUpload";
44
import ImageFromBase64 from "@/components/DisplayImg";
55
type Props = {

spez-frontend/src/components/CmtForm.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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";
6+
import svCmt from "@/services/svCmt";
77
import { useRouter } from "next/navigation";
88

99
// Dynamically import SunEditor to avoid SSR issues
@@ -17,7 +17,7 @@ type Props = {
1717
export default function CmtForm({ postId }: Props) {
1818
const [content, setContent] = useState<string>("");
1919
const router = useRouter();
20-
20+
const {createComment} = svCmt()
2121
// Handle form submission
2222
const handleSubmit = async (e: React.FormEvent) => {
2323
e.preventDefault();
@@ -27,6 +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
3031
await axios.post(
3132
`https://spezbe.hungnq.online/posts/cmt/${postId}`,
3233
{
@@ -40,6 +41,9 @@ export default function CmtForm({ postId }: Props) {
4041
}
4142
);
4243

44+
=======
45+
await createComment(postId, token, content)
46+
>>>>>>> 5a06110 (chore(fe): split service)
4347
// Redirect or give feedback upon success
4448
alert("Post created successfully!");
4549
} catch (error) {

spez-frontend/src/components/Comment.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { getRelativeTime } from "@/utils/getRelativeTime";
22
import Link from "next/link";
33
import Parser from "html-react-parser";
4-
import svPost from "@/utils/svPost";
54
import { GoTrash } from "react-icons/go";
65
import { IComment } from "@/types";
6+
import svCmt from "@/services/svCmt";
77

88
export default async function Comment({id, author, content, created_at}: IComment) {
99
const usr_url = "/user/" + author.id;
10-
const { delComment } = await svPost();
10+
const { delComment } = await svCmt();
1111
const handleDelCmt = async () => {
1212
let token: string | null = null;
1313
if (typeof window !== "undefined") token = localStorage.getItem("jwt");

spez-frontend/src/components/ImgUpload.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22
import React, { useState } from "react";
3-
import svUser from "@/utils/svUser";
3+
import svUser from "@/services/svUser";
44
interface ImgUploadProps {
55
userId: string;
66
}
@@ -15,7 +15,7 @@ function ImageUploader({ userId }: ImgUploadProps) {
1515
const base64String = reader.result? reader.result.toString().split(",")[1]: null
1616
setBase64Image(base64String);
1717
// You can also send the base64 string to the server here if needed
18-
console.log("Base64 Image String:", base64String);
18+
// console.log("Base64 Image String:", base64String);
1919
};
2020
reader.readAsDataURL(file);
2121
}

spez-frontend/src/components/LikeButton.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import React, { useState } from "react";
33
import Image from "next/image";
44
import EagleEmoji from "../../public/eagle_emoji.png";
5-
import svPost from "@/utils/svPost";
5+
import svLike from "@/services/svLike";
66
interface LikeButtonProps {
77
initialLikes: number;
88
post_id: string
@@ -11,7 +11,7 @@ interface LikeButtonProps {
1111
const LikeButton: React.FC<LikeButtonProps> = ({ initialLikes, post_id }) => {
1212
const [likes, setLikes] = useState<number>(initialLikes);
1313
const [liked, setLiked] = useState(false);
14-
const {postLike, delLike} = svPost()
14+
const {postLike, delLike} = svLike()
1515

1616
const handleLike = async () => {
1717
try {

spez-frontend/src/components/NewsFeed.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import svPost from "@/utils/svPost"
1+
import svPost from "@/services/svPost"
22
import Post from "./Post";
33
import { IPost } from "@/types";
44
export default async function NewsFeed() {
55
const {getAllPost} = svPost();
66
const AllPost = await getAllPost();
7-
// console.log(AllPost)
87
return (
98
<>
109
<div className="w-full h-screen p-8">

spez-frontend/src/components/PostForm.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import SunEditor from 'suneditor-react';
55
import 'suneditor/dist/css/suneditor.min.css';
66
import axios from 'axios';
77
import { useRouter } from 'next/navigation';
8-
8+
import svPost from '@/services/svPost';
99
// Dynamically import SunEditor to avoid SSR issues
1010
// const DynamicSunEditor = dynamic(() => import('suneditor-react'), {
1111
// ssr: false,
@@ -15,18 +15,18 @@ export default function PostForm () {
1515
const [content, setContent] = useState<string>('');
1616
const [title, setTitle] = useState<string>('');
1717
const router = useRouter();
18-
18+
const {createPost} = svPost()
1919
// Handle form submission
2020
const handleSubmit = async (e: React.FormEvent) => {
2121
e.preventDefault();
2222

2323
try {
24-
// Send a POST request to your FastAPI backend
2524
let token: string | null = null
2625
if (typeof window !== 'undefined')
2726
token = localStorage.getItem('jwt')
2827
if(token === undefined)
2928
router.push('/')
29+
<<<<<<< HEAD
3030
await axios.post('https://spezbe.hungnq.online/posts/',
3131
{
3232

@@ -41,6 +41,9 @@ export default function PostForm () {
4141
}
4242
);
4343

44+
=======
45+
await createPost(title, content, token)
46+
>>>>>>> 5a06110 (chore(fe): split service)
4447
// Redirect or give feedback upon success
4548
alert('Post created successfully!');
4649
router.push('/'); // Redirect to the homepage or a posts page
@@ -70,6 +73,7 @@ export default function PostForm () {
7073
<div className="mb-4">
7174
<label htmlFor="content" className="block mb-2">Content</label>
7275
<SunEditor
76+
7377
setContents={content}
7478
onChange={(value: string) => setContent(value)}
7579
setOptions={{

spez-frontend/src/services/svCmt.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import axios from "axios";
2+
import { BE_URI } from "@/utils/constants";
3+
export default function svCmt() {
4+
const API = {
5+
comments: BE_URI + "/posts/cmt/",
6+
};
7+
async function getComment(id: string) {
8+
const data = await axios.get(API.comments + id, {
9+
headers: {
10+
accept: "application/json",
11+
},
12+
});
13+
return data.data;
14+
}
15+
async function delComment(id: string, token: string | null) {
16+
const data = await axios.delete(BE_URI + "/comments/" + id, {
17+
headers: {
18+
accept: "application/json",
19+
Authorization: `Bearer ${token}`,
20+
},
21+
});
22+
return data;
23+
}
24+
async function createComment(
25+
id: string,
26+
token: string | null,
27+
content: string
28+
) {
29+
await axios.post(API.comments + id, {
30+
content: content
31+
}, {
32+
headers: {
33+
accept: "application/json",
34+
Authorization: `Bearer ${token}`
35+
}
36+
});
37+
}
38+
return {
39+
getComment,
40+
delComment,
41+
createComment
42+
};
43+
}

spez-frontend/src/services/svLike.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import axios from "axios"
2+
import { BE_URI } from "@/utils/constants"
3+
export default function svLike(){
4+
const API = {
5+
like: BE_URI + "/likes"
6+
}
7+
async function postLike(id: string, token: string | null) {
8+
return await axios.post(API.like + "/" + id, {},{
9+
headers: {
10+
accept: "application/json",
11+
Authorization: `Bearer ${token}`
12+
}
13+
})
14+
}
15+
async function delLike(id: string, token: string | null) {
16+
return await axios.delete(API.like + "/" + id, {
17+
headers: {
18+
// accept: "application/json",
19+
Authorization: `Bearer ${token}`
20+
}
21+
})
22+
}
23+
24+
async function getLike(id: string) {
25+
const data = await axios.get(API.like + "/post/" + id, {
26+
headers: {
27+
accept: "application/json",
28+
},
29+
});
30+
return data.data;
31+
}
32+
return {
33+
postLike,
34+
delLike,
35+
getLike,
36+
}
37+
}

spez-frontend/src/services/svPost.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { BE_URI } from "@/utils/constants";
2+
import axios from "axios";
3+
import { headers } from "next/headers";
4+
export default function svPost() {
5+
const API = {
6+
all: BE_URI + "/posts",
7+
post: BE_URI + "/posts/",
8+
};
9+
async function createPost(title: string, content: string, token: string | null) {
10+
await axios.post(
11+
API.post,
12+
{
13+
title: title,
14+
content: content,
15+
},
16+
{
17+
headers: {
18+
accept: "application/json",
19+
Authorization: `Bearer ${token}`,
20+
},
21+
}
22+
);
23+
}
24+
async function getAllPost() {
25+
const data = await axios.get(API.all + "/?skip=10", {
26+
headers: {
27+
accept: "application/json",
28+
},
29+
});
30+
return data.data;
31+
}
32+
async function getPost(id: string) {
33+
const data = await axios.get(API.post + id, {
34+
headers: {
35+
accept: "application/json",
36+
},
37+
});
38+
return data.data;
39+
}
40+
async function delPost(id: string, token: string | null) {
41+
const data = await axios.delete(API.post + id, {
42+
headers: {
43+
accept: "application/json",
44+
Authorization: `Bearer ${token}`,
45+
},
46+
});
47+
return data.data;
48+
}
49+
return {
50+
getAllPost,
51+
getPost,
52+
delPost,
53+
createPost
54+
};
55+
}
File renamed without changes.

spez-frontend/src/utils/constants.ts

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

0 commit comments

Comments
 (0)