Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React #1

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3a6e588
A basic node server to serve frontend
yookoala Jul 1, 2015
ae06909
Proxy API request to prevent origin problem
yookoala Jul 1, 2015
fc85379
Basic react site to list all news items
yookoala Jul 1, 2015
1a01798
Update server structure to use webpack
yookoala Jul 2, 2015
a61f040
Fix package.json for webpack
yookoala Jul 2, 2015
69adc83
Added React-Router for routing
yookoala Jul 2, 2015
01d8a9a
Rewrite components and scripts in ES6 style
yookoala Jul 3, 2015
4666403
Added Alt to manage flux pattern
yookoala Jul 3, 2015
8933de0
Use CamelCase in file name, like class names
yookoala Jul 5, 2015
61110a8
Ditched jQuery for RESTful API call
yookoala Jul 6, 2015
a894d5b
Correct webpack.config.js indent format
yookoala Jul 7, 2015
9b2fc59
Use react-router to generate menu links
yookoala Jul 7, 2015
ce5f39a
Renamed file api.js to API.js
yookoala Jul 8, 2015
e3fdd91
Moved NewsIdOf to utils
yookoala Jul 8, 2015
c9f154a
NewsPage to review single news
yookoala Jul 8, 2015
feffbc7
Move route handlers to "routes" folder
yookoala Jul 8, 2015
e16bef0
Use absolute path in index.html
yookoala Jul 8, 2015
8218bf4
Move route action related code to route handler
yookoala Jul 8, 2015
63acb41
Added route and store for NewsDetails
yookoala Jul 8, 2015
4556cec
Streamline code in NewsList
yookoala Jul 8, 2015
0b9d474
Implemented basic PublisherNews UI
yookoala Jul 8, 2015
dfd8f86
Ensure generated env.js get served earlier
yookoala Jul 17, 2015
5308d34
Remove some unused part in server.js
yookoala Jul 17, 2015
1d1c409
Modify the dev setup
yookoala Jul 17, 2015
f72b11f
Changed server.js to a production server
yookoala Jul 17, 2015
39e910b
Added Material Design Lite text-only theme
yookoala Jul 18, 2015
d89a0a7
Fix browserSync default route problem
yookoala Jul 18, 2015
45ee077
Fix javascript reference for material design
yookoala Jul 18, 2015
d6d2932
Quit gulp when dev environment goes wrong
yookoala Jul 20, 2015
378c067
Theme the header with material design
yookoala Jul 20, 2015
cdd6758
Use CDN hosted mdl theme css
yookoala Jul 20, 2015
a2a45b5
Improve menu rendering
yookoala Jul 20, 2015
c62ed35
Moved mdl_layout classes to index.html
yookoala Jul 20, 2015
9a2611b
Centralize master template to PageMaster class
yookoala Jul 20, 2015
56da716
Move mdl layout class to PageMaster
yookoala Jul 20, 2015
6b35450
Added NotFoundRoute to routes
yookoala Jul 20, 2015
10169a0
Render the NewsList title in NewsList
yookoala Jul 21, 2015
d8506ab
Style title of the page better
yookoala Jul 21, 2015
bf61cd4
Fix NewsDetails loading problem
yookoala Jul 21, 2015
5c38a9e
Rearrange template structure
yookoala Jul 22, 2015
e77a1fc
Trigger RouteAction in NewsList instead of Route
yookoala Jul 22, 2015
a74ce68
Added request to package.json
yookoala Oct 14, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*.log
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,30 @@ README
======

Frontend for Watchdog (previously NewsdiffHK) project.

Install
-------

The server depends on packages. The serving scripts needs to build into bundle before running. Run these commands.

```bash
npm install
npm run build
```

Run
---

To start the server:

```bash
npm run start
```

The server will prompt you to enter the API_BASE_URL. To suppress the prompt, you may also add API_BASE_URL to the environment like this:

```bash
API_BASE_URL=http://api.server.com:3000/api npm run start
```

Replace `http://api.server.com:3000` with the appropriate host and port number.
153 changes: 153 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@

// basic
var gulp = require("gulp");
var browserSync = require("browser-sync");
var webpack = require("webpack");
var express = require("express");
var url = require("url");
var fs = require("fs");

// gulp plugins
var gutil = require("gulp-util");
var watch = require("gulp-watch");
var sass = require("gulp-sass");
var copy = require("gulp-copy");


// convert styles
gulp.task("build.styles", function(callback) {

// monitor css source
var files = [
"./src/styles/*.sass",
"./src/styles/*.scss"
];
var sassCfg = {
errLogToConsole: true
};
gulp.src(files)
.pipe(sass(sassCfg))
.pipe(gulp.dest("./public/styles/"));

// monitor plain css
gulp.src("./src/styles/*.css")
.pipe(copy("./public/styles/", { prefix: 2 }));

gutil.log("[styles]", "styles generated");
callback();
});

// bundle scripts
gulp.task("build.webpack", function(callback) {
var webpackCfg = require("./webpack.config");

// run webpack
webpack(webpackCfg, function(err, stats) {
if(err) throw new gutil.PluginError("webpack", err);
gutil.log("[webpack]", stats.toString({
// output options
}));
callback();
});
});


// generate environment to js
gulp.task("dev.env", function(callback) {
var fs = require("fs");
var proxy_port = process.env["PROXY_PORT"] || 8000;
var js = "var env = " + JSON.stringify({
"api_base_url": "http://localhost:"+proxy_port+"/api/"
}) + ";";
fs.writeFile("./public/scripts/env.js", js, function(err) {
if (err) {
callback(err);
process.exit(1);
return
}
gutil.log("[dev.env]", "env.js created");
});
});

// watch the source files
// generates public files
gulp.task("dev.watch", function(callback) {
watch([
"./src/styles/**/*.sass",
"./src/styles/**/*.scss",
"./src/styles/**/*.css"
], function () {
gulp.run("build.styles");
});
watch("./src/app/**/*.*", function () {
gulp.run("build.webpack");
});
});


// watch the public files
// hot reload if there is changes
gulp.task("dev.server", function(callback) {

var files = [
"public/**/*.html",
"public/styles/**/*.css",
"public/scripts/**/*.js"
];
var baseDir = "./public";

browserSync.init(files, {
server: {
baseDir: baseDir
},
middleware: function(req, res, next) {
var fileName = url.parse(req.url);
fileName = fileName.href.split(fileName.search).join("");
var fileExists = fs.existsSync(baseDir + "/" + fileName);
if (!fileExists && fileName.indexOf("browser-sync-client") < 0) {
req.url = "/index.html";
}
return next();
},
port: process.env["PORT"] || 8080
});

});

// api proxy for development
gulp.task("dev.api", function(callback) {

if (typeof process.env["API_BASE_URL"] == "undefined") {
callback("Environment variable API_BASE_URL is not defined");
process.exit(1);
}

var bodyParser = require("body-parser");
var app = express();
var forward = require("./includes/forward.js");

app.set("port", (process.env["PROXY_PORT"] || 8000));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(forward(/^\/api\/(.+?)$/, process.env["API_BASE_URL"], gutil.log.bind("[dev.api]")));
app.listen(app.get("port"), function() {
gutil.log("[dev.api]", "proxy server started at http://localhost:" + app.get("port") + "/");
gutil.log("[dev.api]", "proxy to " + process.env["API_BASE_URL"]);
});

});

// build the source files
gulp.task("build", ["build.styles", "build.webpack"]);

// run development server
gulp.task("dev", [
"build",
"dev.env",
"dev.api",
"dev.watch",
"dev.server"
]);

// define default task(s)
gulp.task("default", ["dev"]);
33 changes: 33 additions & 0 deletions includes/envars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


// add trailing slash
function trailSlash(str) {
if (str.substr(-1) != '/') {
str += '/';
}
return str
}

// wait for environment to be ready
exports.ready = function (callback) {
if (typeof process.env["API_BASE_URL"] == "undefined") {
// if API_BASE_URL is not defined, prompt user to define
(function (process, callback) {
var prompt = require('prompt');
prompt.start();
console.log('Missing environment variable');
prompt.get(['API_BASE_URL'], function (err, result) {
if (err) { return onErr(err); }
var url = trailSlash(result.API_BASE_URL);
console.log('API_BASE_URL: ' + url);
process.env["API_BASE_URL"] = url;
callback();
});
})(process, callback)
} else {
process.env["API_BASE_URL"] =
trailSlash(process.env["API_BASE_URL"]);
console.log('API_BASE_URL: ' + process.env["API_BASE_URL"]);
callback();
}
}
25 changes: 25 additions & 0 deletions includes/forward.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

var request = require('request')

// add trailing slash
function trailSlash(str) {
if (str.substr(-1) != '/') {
str += '/';
}
return str
}

module.exports = function(pattern, host, log){
host = trailSlash(host);
return function(req, res, next){
if(req.url.match(pattern)){
log("Accessing proxy api on "+req.url);
var db_path = req.url.match(pattern)[1]
, db_url = [host, db_path].join('');
res.header('Access-Control-Allow-Origin', '*');
req.pipe(request[req.method.toLowerCase()](db_url)).pipe(res);
}else{
next();
}
}
}
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "newsdiff-frontend",
"version": "0.0.1",
"description": "",
"scripts": {
"build": "gulp build",
"dev": "gulp",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "https://github.com/code4hk/Newsdiff-Frontend.git"
},
"author": "Code4HK",
"license": "GPLv3",
"browserify": {
"transform": ["aliasify"]
},
"dependencies": {
"express": "^4.13.0",
"body-parser": "^1.13.0",
"prompt": "^0.2.14",
"browser-request": "^0.3.3",
"react": "^0.13.3",
"react-router": "^0.13.3",
"request": "^2.65.0",
"alt": "^0.16.10"
},
"devDependencies": {
"browser-sync": "~2.7.13",
"gulp": "~3.9.0",
"gulp-util": "~3.0.6",
"gulp-watch": "~4.2.4",
"gulp-sass": "~2.0.3",
"gulp-copy": "~0.0.2",
"webpack": "^1.10.1",
"jsx-loader": "^0.13.2",
"babel-loader": "^5.2.2"
}
}
2 changes: 2 additions & 0 deletions public/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
styles
scripts
23 changes: 23 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Newsdiff HK</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic,thin,light,bolditalic,black,medium&amp;lang=en" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
<link rel="stylesheet" href="https://storage.googleapis.com/code.getmdl.io/1.0.0/material.blue_grey-blue.min.css" />
<link rel="stylesheet" href="/styles/theme.css" />
<link rel="stylesheet" href="/styles/styles.css" />
</head>
<body class="mdl-demo mdl-color--grey-100 mdl-color-text--grey-700 mdl-base">
<span class="msg-loading">Loading...</span>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0/JSXTransformer.js"></script>
<script src="https://storage.googleapis.com/code.getmdl.io/1.0.0/material.min.js"></script>
<script src="/scripts/env.js"></script>
<script src="/scripts/bundle.js"></script>
</body>
</html>
Loading