From a11e1579a6949a8598b83e28501c98079cc8c578 Mon Sep 17 00:00:00 2001 From: AuxiliumCDNG Date: Sun, 26 Dec 2021 23:09:10 +0100 Subject: [PATCH] react frontend transition WIP 1 working basic forum components --- README.md | 1 + main.py | 2 + public/js/forum_page.js | 50 ++++++++++++----- routes/api_get.py | 16 ++++-- routes/api_moderate.py | 8 ++- templates/forum_page.html | 1 - web/.gitignore | 23 ++++++++ web/README.md | 70 ++++++++++++++++++++++++ web/package.json | 38 +++++++++++++ web/public/index.html | 47 ++++++++++++++++ web/public/manifest.json | 25 +++++++++ web/public/robots.txt | 3 + web/src/components/category/category.css | 25 +++++++++ web/src/components/category/category.js | 16 ++++++ web/src/components/content/content.js | 26 +++++++++ web/src/components/content/current.js | 12 ++++ web/src/components/footer/footer.css | 3 + web/src/components/footer/footer.js | 37 +++++++++++++ web/src/components/thread/thread.css | 19 +++++++ web/src/components/thread/thread.js | 16 ++++++ web/src/css/forum.css | 14 +++++ web/src/index.css | 9 +++ web/src/index.js | 17 ++++++ web/src/pages/forum.js | 51 +++++++++++++++++ web/src/reportWebVitals.js | 13 +++++ web/src/setupTests.js | 5 ++ 26 files changed, 525 insertions(+), 22 deletions(-) create mode 100644 web/.gitignore create mode 100644 web/README.md create mode 100644 web/package.json create mode 100644 web/public/index.html create mode 100644 web/public/manifest.json create mode 100644 web/public/robots.txt create mode 100644 web/src/components/category/category.css create mode 100644 web/src/components/category/category.js create mode 100644 web/src/components/content/content.js create mode 100644 web/src/components/content/current.js create mode 100644 web/src/components/footer/footer.css create mode 100644 web/src/components/footer/footer.js create mode 100644 web/src/components/thread/thread.css create mode 100644 web/src/components/thread/thread.js create mode 100644 web/src/css/forum.css create mode 100644 web/src/index.css create mode 100644 web/src/index.js create mode 100644 web/src/pages/forum.js create mode 100644 web/src/reportWebVitals.js create mode 100644 web/src/setupTests.js diff --git a/README.md b/README.md index 5d3910b..10cbaeb 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,4 @@ * [materialize](https://github.com/Dogfalo/materialize) (Licence=MIT) * [jquery](https://github.com/jcushman/pdfquery) (Licence=MIT) * [stackedit.js](https://github.com/benweet/stackedit.js) (Licence=MIT) +* [react](https://github.com/facebook/react) (Licence=MIT) diff --git a/main.py b/main.py index 97bb9fe..eb7dcf9 100644 --- a/main.py +++ b/main.py @@ -56,6 +56,8 @@ def add_header(r): r.headers["Pragma"] = "no-cache" r.headers["Expires"] = "0" r.headers['Cache-Control'] = 'public, max-age=0' + + r.headers["Access-Control-Allow-Origin"] = "*" return r diff --git a/public/js/forum_page.js b/public/js/forum_page.js index 8d6aa8d..6fa1bca 100644 --- a/public/js/forum_page.js +++ b/public/js/forum_page.js @@ -23,20 +23,42 @@ function location_change(location, back=false, version=null) { fetch(query) .then( response => response.text() - .then((res) => { document.getElementById("forum-contents").innerHTML = res - try {let text = " " + document.getElementById("thread-contents").innerHTML; - let html = converter.makeHtml(text); - document.getElementById("thread-contents").innerHTML = html; + /* + .then(res => { + document.getElementById("forum-contents").innerHTML = res; + + console.log(res); + var stack_converter = new Stackedit(); + + try { + stack_converter.openFile({ + name: "", + content: {text: document.getElementById("thread-contents").innerHTML} + }, true); + } catch (error) { } + stack_converter.on("fileChange", file => { + console.log(file.content.html); + document.getElementById("thread-contents").innerHTML = file.content.html; + }); + })*/ + .then(res => { + document.getElementById("forum-contents").setHTML(res); + try { + let text = " " + document.getElementById("thread-contents").innerHTML; + + let html = converter.makeHtml(text); + console.log(html); + document.getElementById("thread-contents").setHTML(html); document.querySelectorAll("code").forEach( (ele) => { //add copy button to code blocks }) document.querySelectorAll('pre code').forEach((el) => { hljs.highlightElement(el); }); - } catch {} }) + .then(window.scrollTo(0, 0)) .then(clearTimeout(loader)) //prevents animation from playing if this is done early .then(document.getElementById("page-load").classList.remove("active")) .then(document.getElementById("page-warn").classList.add("hide")) @@ -95,15 +117,6 @@ window.onpopstate = () => { } } -let l = window.location.pathname; -if(l.slice(6) == "/") { - location_change("0", back=true, version=null); -} else { - let location = l.slice(6).split("/").filter(n => n); - location = location.at(-1); - location_change(location, back=true, version=findGetParameter("version")); -} - var converter = new showdown.Converter({"simplifiedAutoLink": true, "strikethrough": true, "tasklists": true, @@ -117,4 +130,13 @@ document.addEventListener('DOMContentLoaded', function() { direction: 'top', hoverEnabled: false }); + + let l = window.location.pathname; + if(l.slice(6) == "/") { + location_change("0", back=true, version=null); + } else { + let location = l.slice(6).split("/").filter(n => n); + location = location.at(-1); + location_change(location, back=true, version=findGetParameter("version")); + } }); diff --git a/routes/api_get.py b/routes/api_get.py index 6ec871c..428f215 100644 --- a/routes/api_get.py +++ b/routes/api_get.py @@ -13,8 +13,9 @@ @crossdomain(origin="*", current_app=app) @app.route("/api/content/get/") -@login_required +# @login_required def get_content(): + current_user = None contents, current = api_get_content(current_user, request) if isinstance(contents, tuple): # content is empty @@ -37,6 +38,8 @@ def get_content(): # print("building page for ", request.args["location"]) contents = [contents[key] for key in contents.keys()] + print("\n\n", contents, current, "\n\n") + return render_template("components/contents.html", contents=contents, current=current) if "current" in request.args.keys(): @@ -106,9 +109,10 @@ def api_get_content(current_user, request): if res is None: return flask.abort(flask.Response(response="Location not found", status=404)) + """ if not permissions_checker(current_user, "view", "all", location): return flask.abort(flask.Response(response="No permission to view this location", status=906)) - + """ q = f"SELECT `id`, `name`, `location`, `type` FROM {config.Instance.instance}_content WHERE `location`='{location}'" cursor.execute(q) content = cursor.fetchall() @@ -122,15 +126,17 @@ def api_get_content(current_user, request): con.close() + """ if current is not None: try: current["permissions"] = json.loads(current["permissions"])[current_user.email] except KeyError: current["permissions"] = {} - + """ parsed = {} + """ for i in range(0, len(content)): parsed[i] = content[i] - - return parsed, current + """ + return jsonify(content), current diff --git a/routes/api_moderate.py b/routes/api_moderate.py index b98d9f9..2a580a6 100644 --- a/routes/api_moderate.py +++ b/routes/api_moderate.py @@ -120,10 +120,14 @@ def edit(): with connection_pool.connection() as con, con.cursor(dictionary=True) as cursor: cursor.execute(f"SELECT * FROM `{config.Instance.instance}_content` WHERE `id`='{content_id}'") old_data = cursor.fetchone() + + query = (old_data['id'], old_data['name'], old_data['content'], time.strftime('%Y-%m-%d %H:%M:%S')) cursor.execute(f"INSERT INTO `{config.Instance.instance}_versions` " f"(content_id, name, content, date) VALUES " - f"('{old_data['id']}', '{old_data['name']}', '{old_data['content']}', '{time.strftime('%Y-%m-%d %H:%M:%S')}')") - cursor.execute(f"UPDATE `{config.Instance.instance}_content` SET `name`='{new_name}', `content`='{new_content}' WHERE `id`='{content_id}'") + f"(%s, %s, %s, %s)", query) + + query = (new_name, new_content, content_id) + cursor.execute(f"UPDATE `{config.Instance.instance}_content` SET `name`=%s, `content`=%s WHERE `id`=%s", query) con.commit() con.close() diff --git a/templates/forum_page.html b/templates/forum_page.html index 792754d..56cf9c5 100644 --- a/templates/forum_page.html +++ b/templates/forum_page.html @@ -3,7 +3,6 @@ - diff --git a/web/.gitignore b/web/.gitignore new file mode 100644 index 0000000..4d29575 --- /dev/null +++ b/web/.gitignore @@ -0,0 +1,23 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/web/README.md b/web/README.md new file mode 100644 index 0000000..58beeac --- /dev/null +++ b/web/README.md @@ -0,0 +1,70 @@ +# Getting Started with Create React App + +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). + +## Available Scripts + +In the project directory, you can run: + +### `npm start` + +Runs the app in the development mode.\ +Open [http://localhost:3000](http://localhost:3000) to view it in your browser. + +The page will reload when you make changes.\ +You may also see any lint errors in the console. + +### `npm test` + +Launches the test runner in the interactive watch mode.\ +See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `npm run build` + +Builds the app for production to the `build` folder.\ +It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.\ +Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `npm run eject` + +**Note: this is a one-way operation. Once you `eject`, you can't go back!** + +If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. + +You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. + +## Learn More + +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). + +To learn React, check out the [React documentation](https://reactjs.org/). + +### Code Splitting + +This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) + +### Analyzing the Bundle Size + +This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) + +### Making a Progressive Web App + +This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) + +### Advanced Configuration + +This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) + +### Deployment + +This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) + +### `npm run build` fails to minify + +This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) diff --git a/web/package.json b/web/package.json new file mode 100644 index 0000000..3064317 --- /dev/null +++ b/web/package.json @@ -0,0 +1,38 @@ +{ + "name": "web", + "version": "0.1.0", + "private": true, + "dependencies": { + "@testing-library/jest-dom": "^5.16.1", + "@testing-library/react": "^12.1.2", + "@testing-library/user-event": "^13.5.0", + "react": "^17.0.2", + "react-dom": "^17.0.2", + "react-scripts": "5.0.0", + "web-vitals": "^2.1.2" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "eslintConfig": { + "extends": [ + "react-app", + "react-app/jest" + ] + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } +} diff --git a/web/public/index.html b/web/public/index.html new file mode 100644 index 0000000..5b7f56b --- /dev/null +++ b/web/public/index.html @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + React App + + + +
+ + + + + diff --git a/web/public/manifest.json b/web/public/manifest.json new file mode 100644 index 0000000..080d6c7 --- /dev/null +++ b/web/public/manifest.json @@ -0,0 +1,25 @@ +{ + "short_name": "React App", + "name": "Create React App Sample", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + }, + { + "src": "logo192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "logo512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/web/public/robots.txt b/web/public/robots.txt new file mode 100644 index 0000000..e9e57dc --- /dev/null +++ b/web/public/robots.txt @@ -0,0 +1,3 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * +Disallow: diff --git a/web/src/components/category/category.css b/web/src/components/category/category.css new file mode 100644 index 0000000..ac5c610 --- /dev/null +++ b/web/src/components/category/category.css @@ -0,0 +1,25 @@ +.category { + display: inline-flex; + align-items: center; + justify-content: center; + text-align: center; + + font-size: xx-large; + color: white; + + min-height: 100px; + height: fit-content; + min-width: 200px; + max-width: 250px; + + background-color: #0f166e; + cursor: pointer; + + border-radius: 5px; + padding: 10px; + margin: 10px; +} +.category p { + margin: 0; + padding: 0; +} \ No newline at end of file diff --git a/web/src/components/category/category.js b/web/src/components/category/category.js new file mode 100644 index 0000000..e02b7e4 --- /dev/null +++ b/web/src/components/category/category.js @@ -0,0 +1,16 @@ +import React from "react"; +import "./category.css" + +class Category extends React.Component { + click() { + this.props.renew.bind(this.props.that)(this.props.data.id); + } + render() { + return ( +
+

{this.props.data.name}

+
+ )} +}; + +export default Category; diff --git a/web/src/components/content/content.js b/web/src/components/content/content.js new file mode 100644 index 0000000..99d24f5 --- /dev/null +++ b/web/src/components/content/content.js @@ -0,0 +1,26 @@ +import React from "react"; +import Thread from "../thread/thread"; +import Category from "../category/category"; + +class Content extends React.Component { + render() { return ( + <> + { + this.props.data.map(ele => { + if(ele.type === "thread") { + return() + } + }) + } + { + this.props.data.map(ele => { + if(ele.type === "category") { + return() + } + }) + } + + )} +}; + +export default Content; diff --git a/web/src/components/content/current.js b/web/src/components/content/current.js new file mode 100644 index 0000000..157a3bb --- /dev/null +++ b/web/src/components/content/current.js @@ -0,0 +1,12 @@ +import React from "react"; + +const Current = ( {data} ) => { + return ( + <> +

{data.name}

+

{data.content}

+ + ) +}; + +export default Current; diff --git a/web/src/components/footer/footer.css b/web/src/components/footer/footer.css new file mode 100644 index 0000000..5fb8596 --- /dev/null +++ b/web/src/components/footer/footer.css @@ -0,0 +1,3 @@ +#footer-links { + text-align: right; +} \ No newline at end of file diff --git a/web/src/components/footer/footer.js b/web/src/components/footer/footer.js new file mode 100644 index 0000000..02bcc86 --- /dev/null +++ b/web/src/components/footer/footer.js @@ -0,0 +1,37 @@ +import React from "react"; +import "./footer.css" + +const Footer = ( ) => { + return ( +
+
+
+
+
Powered by BulletFlask
+

+ BulletFlask ist eine hochflexible Forum Software. BulletFlask ist OpenSource und unter GPL-3.0 lizensiert. + Das macht es jedem kostenfrei möglich, diese Software zu betreiben. +

+
+ +
+
+
+
+ © 2021 Sönke Klock + GitHub +
+
+
+ ) +}; + +export default Footer; diff --git a/web/src/components/thread/thread.css b/web/src/components/thread/thread.css new file mode 100644 index 0000000..bc13396 --- /dev/null +++ b/web/src/components/thread/thread.css @@ -0,0 +1,19 @@ +.thread { + font-size: x-large; + color: white; + padding: 10px; + + display: flex; + height: fit-content; + justify-content: center; + align-items: center; + + background-color: #0f166e; + cursor: pointer; + + margin: 10px; + border-radius: 5px; +} +.thread p { + margin: 0; +} \ No newline at end of file diff --git a/web/src/components/thread/thread.js b/web/src/components/thread/thread.js new file mode 100644 index 0000000..56f21ac --- /dev/null +++ b/web/src/components/thread/thread.js @@ -0,0 +1,16 @@ +import React from "react"; +import "./thread.css" + +class Thread extends React.Component { + click() { + this.props.renew.bind(this.props.that)(this.props.data.id); + } + + render() { return ( +
+

{this.props.data.name}

+
+ )} +}; + +export default Thread; diff --git a/web/src/css/forum.css b/web/src/css/forum.css new file mode 100644 index 0000000..ba4beaf --- /dev/null +++ b/web/src/css/forum.css @@ -0,0 +1,14 @@ +@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@400&display=swap'); + +main { + overflow: hidden; + height: fit-content; + width: 95vw; + max-width: 1000px; + top: auto; + margin: auto auto; + + overflow-x: hidden; + + font-family: 'Open Sans', sans-serif; +} \ No newline at end of file diff --git a/web/src/index.css b/web/src/index.css new file mode 100644 index 0000000..387fdb2 --- /dev/null +++ b/web/src/index.css @@ -0,0 +1,9 @@ +/* Sticky Footer */ +body { + display: flex; + min-height: 100vh; + flex-direction: column; +} +main { + flex: 1 0 auto; +} \ No newline at end of file diff --git a/web/src/index.js b/web/src/index.js new file mode 100644 index 0000000..91c4bf5 --- /dev/null +++ b/web/src/index.js @@ -0,0 +1,17 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import Forum from './pages/forum'; +import reportWebVitals from './reportWebVitals'; +import "./index.css"; + +ReactDOM.render( + + + , + document.getElementById('root') +); + +// If you want to start measuring performance in your app, pass a function +// to log results (for example: reportWebVitals(console.log)) +// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals +reportWebVitals(); diff --git a/web/src/pages/forum.js b/web/src/pages/forum.js new file mode 100644 index 0000000..d6508bd --- /dev/null +++ b/web/src/pages/forum.js @@ -0,0 +1,51 @@ +import React, { Component } from 'react'; +import Current from '../components/content/current'; +import Content from '../components/content/content'; +import Footer from '../components/footer/footer'; +import "../css/forum.css"; + +class Forum extends Component { + state = { + current: {}, + content: [] + } + + getData(location) { + console.log(location); + let api = "http://localhost:8080"; + fetch(api + "/api/content/get/?current&location="+location.toString()) + .then(res => res.json()) + .then(data => { + this.setState({ current: data }); + }) + .catch(console.log); + + fetch(api + "/api/content/get/?location="+location.toString()) + .then(res => res.json()) + .then(data => { + this.setState({ content: data }); + }) + .catch(console.log); + } + + componentDidMount() { + this.getData(0); + + setInterval(() => { + console.log(this.state.content) + }, 5000); + } + render() { + return ( + <> +
+ + +
+