Skip to content

Commit

Permalink
react frontend transition WIP 1
Browse files Browse the repository at this point in the history
working basic forum components
  • Loading branch information
AuxiliumCDNG committed Dec 26, 2021
1 parent ab88bb9 commit a11e157
Show file tree
Hide file tree
Showing 26 changed files with 525 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
50 changes: 36 additions & 14 deletions public/js/forum_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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,
Expand All @@ -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"));
}
});
16 changes: 11 additions & 5 deletions routes/api_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand Down Expand Up @@ -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()
Expand All @@ -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
8 changes: 6 additions & 2 deletions routes/api_moderate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 0 additions & 1 deletion templates/forum_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/public/materialize/css/materialize.css">
<link rel="stylesheet" href="/public/materialize/css/loading.css">
<link rel="stylesheet" href="/public/css/common.css">
<link rel="stylesheet" href="/public/css/forum.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
Expand Down
23 changes: 23 additions & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
@@ -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*
70 changes: 70 additions & 0 deletions web/README.md
Original file line number Diff line number Diff line change
@@ -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)
38 changes: 38 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
47 changes: 47 additions & 0 deletions web/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<link rel="stylesheet" href="http://192.168.178.201:8080/public/materialize/css/materialize.css">

<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</html>
25 changes: 25 additions & 0 deletions web/public/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 3 additions & 0 deletions web/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
Loading

0 comments on commit a11e157

Please sign in to comment.