Add frontend app

This commit is contained in:
Adam Svystun 2018-09-01 17:14:32 +02:00
parent 1722bf0bb0
commit 1540130ae2
15 changed files with 6665 additions and 2 deletions

0
frontend/__init__.py Normal file
View file

3
frontend/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
frontend/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class FrontendConfig(AppConfig):
name = 'frontend'

125
frontend/client/App.js Normal file
View file

@ -0,0 +1,125 @@
import React from "react";
import ReactDOM from "react-dom";
import { post } from "axios";
import Dropzone from "react-dropzone";
import { BarLoader } from "react-spinners";
import { css } from "react-emotion";
const COMIXIFY_API = "/comixify";
const MAX_FILE_SIZE = 50000000;
const PERMITTED_VIDEO_EXTENSIONS = "video/*";
class App extends React.Component {
static appStates = {
INITIAL: 0,
PROCESSING: 1,
FINISHED: 2,
UPLOAD_ERROR: 3,
DROP_ERROR: 4
};
constructor(props) {
super(props);
this.state = {
state: App.appStates.INITIAL,
drop_errors: [],
result_comics: null
};
this.onVideoDrop = this.onVideoDrop.bind(this);
this.onVideoUploadProgress = this.onVideoUploadProgress.bind(this);
}
onVideoUploadProgress(progressEvent) {
let percentCompleted = Math.round(
progressEvent.loaded * 100 / progressEvent.total
);
console.log(percentCompleted);
}
processVideo(video) {
let data = new FormData();
data.append("file", video);
post(COMIXIFY_API, data, {
headers: { "content-type": "multipart/form-data" },
onUploadProgress: this.onVideoUploadProgress
})
.then(res => {
if (res.data["status_message"] === "ok") {
this.setState({
state: App.appStates.FINISHED,
result_comics: res.data["comic"]
});
} else {
this.setState({
state: App.appStates.UPLOAD_ERROR
});
}
})
.catch(err => {
console.error(err);
this.setState({
state: App.appStates.UPLOAD_ERROR
});
});
this.setState({
state: App.appStates.PROCESSING
});
}
onVideoDrop(files, rejected) {
if (rejected.length !== 0) {
console.error(rejected);
this.setState({
drop_errors: ["Maximum size for single video is 50MB"],
stata: App.appStates.DROP_ERROR
});
return;
}
this.processVideo(files[0]);
}
render() {
let { state, drop_errors, result_comics } = this.state;
let showDropzone = [
App.appStates.INITIAL,
App.appStates.UPLOAD_ERROR,
App.appStates.DROP_ERROR,
App.appStates.FINISHED
].includes(state);
return (
<div className="wrap">
<h1>Comixify</h1>
{state === App.appStates.FINISHED && [
<img key="1" src={result_comics} />,
<p key="2">Go again:</p>
]}
{state === App.appStates.DROP_ERROR &&
drop_errors.map((o, i) => <p key={i}>{o}</p>)}
{state === App.appStates.UPLOAD_ERROR && (
<p>Server Error: Please try again later.</p>
)}
{showDropzone && (
<Dropzone
onDrop={this.onVideoDrop}
accept={PERMITTED_VIDEO_EXTENSIONS}
maxSize={MAX_FILE_SIZE}
className="dropzone"
acceptClassName="dropzone--accepted"
rejectClassName="dropzone--rejected"
multiple={false} // Only one video at the time
>
<p>Drop files here, or click to select manually</p>
</Dropzone>
)}
{state === App.appStates.PROCESSING && (
<BarLoader
color={"rgb(54, 215, 183)"}
className={css`
margin: 0 auto;
`}
width={10}
widthUnit="rem"
/>
)}
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));

View file

@ -0,0 +1,51 @@
const webpack = require("webpack");
const merge = require("webpack-merge");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
let common = {
entry: {
app: __dirname + "/App.js"
},
output: {
path: __dirname + "/../static/frontend/js",
filename: "[name].client.js"
},
externals: {
cheerio: "window",
"react/lib/ExecutionEnvironment": true,
"react/lib/ReactContext": true
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: "babel-loader",
query: {
presets: ["react", "stage-0", "flow"],
plugins: ["emotion"]
}
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
}
};
if (process.env.NODE_ENV === "production") {
module.exports = merge(common, {
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify("production")
}),
new UglifyJsPlugin()
],
mode: "production"
});
} else {
module.exports = merge(common, {
mode: "development"
});
}

View file

3
frontend/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,58 @@
html,
body {
margin: 0;
padding: 0;
}
body {
color: #222;
background: #eee;
}
.wrap {
padding: 1rem;
margin: 2rem auto 0 auto;
max-width: 700px;
background: white;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
border: 1px solid rgba(0, 0, 0, 0.09);
border-radius: 3px;
box-sizing: border-box;
}
img {
width: 100%;
}
.bar-loader {
margin: 0 auto;
}
.dropzone {
padding: 1rem;
height: 10rem;
border: 2px dashed gray;
}
.dropzone--rejected {
border-style: solid;
border-color: #c66;
background-color: #eee;
}
.dropzone--disabled {
opacity: 0.5;
}
.dropzone--accepted {
border-style: solid;
border-color: #6c6;
background-color: #eee;
}
@media all and (max-width: 700px) {
.wrap {
margin-top: 0;
border-radius: 0;
}
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,16 @@
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>Comixify</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{% static 'frontend/css/app.css' %}" />
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="{% static 'frontend/js/app.client.js' %}"></script>
</body>
</html>

3
frontend/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
frontend/urls.py Normal file
View file

@ -0,0 +1,8 @@
from django.conf.urls import url
from .views import index
urlpatterns = [
url(r'^$', index, name='comixify_index'),
]

5
frontend/views.py Normal file
View file

@ -0,0 +1,5 @@
from django.shortcuts import render
def index(request):
return render(request, "index.html")

6356
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -6,8 +6,10 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config ./frontend/client/webpack.config.js",
"build:watch": "webpack --config ./frontend/client/webpack.config.js --watch",
"build:prod": "NODE_ENV=production webpack --config ./frontend/client/webpack.config.js --watch"
"build:watch":
"webpack --config ./frontend/client/webpack.config.js --watch",
"build:prod":
"NODE_ENV=production webpack --config ./frontend/client/webpack.config.js"
},
"repository": {
"type": "git",