Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Data-driven Recursive Graphics in React! #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<html>
<head>
<meta charset="utf-8">
<title></title>
<title>Data-driven Recursive Graphics in React!</title>
</head>
<body>
<div id='app'></div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>
<script src="build/index.js"></script>
</body>
</html>
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"css-loader": "^0.23.1",
"node-sass": "^3.7.0",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.1",
"webpack": "^1.13.1"
},
"dependencies": {
Expand Down
85 changes: 25 additions & 60 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,42 @@
import React from 'react';

import Form from './form.js';
import Organ from './organ.js';

const fields = [
{
id: 'name',
inputComponentName: 'TextInput',
placeholder: 'Enter your name'
},
{
id: 'age',
inputComponentName: 'TextInput',
placeholder: 'Enter your age'
},
{
id: 'gender',
inputComponentName: 'Radio',
options: ['male', 'female', 'other']
}
];
const BASE_URL = 'http://api.socialsquare.dk/';
const AUTH_TOKEN = '';

export default class App extends React.Component {
constructor(props) {
super(props);
this.getURL = (path) => {
return BASE_URL + path + '/?auth_token=' + AUTH_TOKEN;
};
this.state = {
isActive: false,
formData: {
name: '',
age: ''
}
organ: null
};
this.toggleActiveState = this.toggleActiveState.bind(this);
this.handleFormFieldChange = this.handleFormFieldChange.bind(this);
}

render() {
console.log(this.state);
const style = this.state.isActive
?
{backgroundColor: 'teal', color: 'white'}
:
{};
return (
<div style={style} onClick={this.toggleActiveState}>
<h1>Marathon Estimate</h1>
<Form
fields={fields}
formData={this.state.formData}
onChange={this.handleFormFieldChange}
/>
</div>
);
}

toggleActiveState() {
this.setState({
isActive: !this.state.isActive
});
componentDidMount() {
this.serverRequest = $.get(this.getURL('organs/socialsquare/tree'), function (result) {
this.setState({
organ: result
});
}.bind(this));
}

handleFormFieldChange(value, id) {
this.setState({
formData: Object.assign({}, this.state.formData, {
[id]: value
})
});
componentWillUnmount() {
this.serverRequest.abort();
}

// TODO: implement
sendFormData() {
fetch('marathon.com/api', {
method: 'POST',
body: JSON.stringify(this.state.formData)
});
render() {
return (
<div>
<header>
<h1>&ldquo;Data-driven Recursive Graphics in React!&rdquo;</h1>
<h2>{this.state.organ ? this.state.organ.name : 'Loading ...'}</h2>
</header>
<Organ organ={this.state.organ} />
</div>
);
}
}
10 changes: 10 additions & 0 deletions src/circle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';

export default function Organ(props) {
console.log(props);
return null;

return (
{/*<circle cx="50" cy="50" r="40" stroke="black" strokeWidth="3" fill="none" />*/}
);
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import domReady from 'domready';

import App from './app.js';

require('../styles/main.scss');

domReady(() => {
const container = document.getElementById('app');
render(<App/>, container);
Expand Down
11 changes: 11 additions & 0 deletions src/organ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';

import Circle from './circle.js'

export default function Organ(props) {
return (
<svg className="canvas">
<Circle />
</svg>
);
}
27 changes: 27 additions & 0 deletions styles/main.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@import url(https://fonts.googleapis.com/css?family=Slabo+27px);

html {
height: 100%;
}

body {
font-family: 'Slabo 27px', serif;
position: relative;
margin: 0;
height: 100%;
}

header {
text-align: center;
position: absolute;
top: 0;
width: 100%;
z-index: 1; /* This is really strange - why would I need that? */
}

svg.canvas {
background-color: #eee;
position: absolute;
width: 100%;
height: 100%;
}
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module.exports = {
test: /\.jsx?$/,
loader: 'babel',
exclude: /node_modules/
}, {
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}]
},
resolve: {
Expand Down