From 09453fe44dd3b777e05d2eaddfe11b774b38bee3 Mon Sep 17 00:00:00 2001
From: hilmanski
Date: Tue, 3 Oct 2023 11:17:02 +0800
Subject: [PATCH] adding raw text editor for making content
---
backend-crt/src/db/post.py | 11 ++
backend-crt/src/main.py | 9 +-
frontend-crt/src/app/editor/[id]/page.tsx | 176 +++++++++++----------
frontend-crt/src/components/TextEditor.tsx | 41 +++++
4 files changed, 151 insertions(+), 86 deletions(-)
create mode 100644 frontend-crt/src/components/TextEditor.tsx
diff --git a/backend-crt/src/db/post.py b/backend-crt/src/db/post.py
index a1f1442..432f9cf 100644
--- a/backend-crt/src/db/post.py
+++ b/backend-crt/src/db/post.py
@@ -11,6 +11,17 @@ def get_post(post_id: int):
post = session.get(Post, post_id)
return post
+def update_post(post_id: int, data: dict):
+ session = Session(engine)
+ post = session.get(Post, post_id)
+ for key, value in data.items():
+ setattr(post, key, value)
+ session.add(post)
+ session.commit()
+ session.refresh(post)
+
+ return post
+
def remove_post(post_id: int):
session = Session(engine)
post = session.get(Post, post_id)
diff --git a/backend-crt/src/main.py b/backend-crt/src/main.py
index 5698e12..8785902 100644
--- a/backend-crt/src/main.py
+++ b/backend-crt/src/main.py
@@ -1,5 +1,5 @@
import os
-import random
+# import random
from fastapi import FastAPI
from dotenv import load_dotenv
from serpapi import GoogleSearch
@@ -16,7 +16,7 @@ from nltk.util import bigrams, trigrams
import operator
import nltk
-from .db.post import add_post, save_links, get_post, get_all_post, remove_post
+from .db.post import add_post, save_links, get_post, get_all_post, remove_post, update_post
@@ -98,6 +98,11 @@ def getPost(post_id: str):
post = get_post(post_id)
return post
+@app.put('/posts/{post_id}')
+def updatePost(post_id: str, data: dict):
+ post = update_post(post_id, data)
+ return post
+
@app.delete('/posts/{post_id}')
def deletePost(post_id: str):
post = remove_post(post_id)
diff --git a/frontend-crt/src/app/editor/[id]/page.tsx b/frontend-crt/src/app/editor/[id]/page.tsx
index 04bc44d..582b218 100644
--- a/frontend-crt/src/app/editor/[id]/page.tsx
+++ b/frontend-crt/src/app/editor/[id]/page.tsx
@@ -1,5 +1,6 @@
import Link from "next/link";
import Terms from "@/components/Terms";
+import TextEditor from "@/components/TextEditor";
async function getData(id: string) {
const res = await fetch(`http://localhost:8000/posts/${id}`)
@@ -23,10 +24,20 @@ export default async function Editor({
const data = await getData(params.id);
const searchResult = data.search_result
-
return (
+
+
+ Title: {data.title}
+
+
+
+ Home
+
+
+
+
Menu
@@ -39,98 +50,95 @@ export default async function Editor({
-
+
-
-
- Title: {data.title}
-
-
-
- Home
-
-
+
- {
- searchResult.answer_box && (
-
- Answer Box (Featued Snippet)
-
- {
- searchResult.answer_box.snippet
- }
-
-
- {
- searchResult.answer_box.list && (
-
-
Info: Features snippet is a list
+ {
+ searchResult.answer_box && (
+
+ Answer Box (Featued Snippet)
+
{
- searchResult.answer_box.list.map((item, index) => {
- return (
-
- )
- })
+ searchResult.answer_box.snippet
}
-
- )
- }
+
-
-
- source
-
-
-
- )
- }
-
- {
- searchResult.related_questions.length > 0 && (
-
- )
- }
+ }
- {
- searchResult.related_searches.length > 0 && (
-
- )
- }
+
+
+ source
+
+
+
+ )
+ }
-
+ {
+ searchResult.related_questions.length > 0 && (
+
+ )
+ }
+
+ {
+ searchResult.related_searches.length > 0 && (
+
+ )
+ }
+
+
+
+
+
)
diff --git a/frontend-crt/src/components/TextEditor.tsx b/frontend-crt/src/components/TextEditor.tsx
new file mode 100644
index 0000000..1c72e14
--- /dev/null
+++ b/frontend-crt/src/components/TextEditor.tsx
@@ -0,0 +1,41 @@
+"use client";
+
+import { useState } from "react";
+
+export default function TextEditor({id, prevContent}: {id: string, prevContent: string}) {
+
+ const [content, setContent] = useState(prevContent)
+
+ const saveContent = () => {
+ fetch(`http://localhost:8000/posts/${id}/`, {
+ method: 'PUT',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ content: content
+ })
+ })
+ .then(res => res.json())
+ .then(data => console.log(data))
+ .catch(err => console.log(err))
+ }
+
+ return (
+
+
+
Text Editor
+
+
+
+
+
+ )
+}
\ No newline at end of file