var React = require('react');
var ReactDOM = require('react-dom');
var Remarkable = require('remarkable');
var Button = require('react-bootstrap').Button;
var ButtonToolbar = require('react-bootstrap').ButtonToolbar;
var FormControl = require('react-bootstrap').FormControl;
var FormGroup = require('react-bootstrap').FormGroup;
var Panel = require('react-bootstrap').Panel;
var Modal = require('react-bootstrap').Modal;
var DatePicker = require('react-bootstrap-date-picker');
var FontAwesome = require('react-fontawesome');
var dateFormat = require('dateformat');
var $ = require('jquery');
const API_URL = "https://todo.tankernn.eu/php/api.php";
const priorityNames = {1: "danger", 2: "warning", 3: "info", 4: "success"};
function dateToString(date) {
return dateFormat(date, "yyyy-mm-dd");
}
var TodoForm = React.createClass({
getInitialState: function() {
return {showModal: false, title: this.props.title, text: this.props.text, deadline: this.props.deadline, priority: this.props.priority};
},
getDefaultProps: function() {
return {edit: false};
},
closeModal() {
this.setState({ showModal: false });
},
openModal() {
this.setState({ showModal: true });
},
handleTitleChange: function(e) {
this.setState({title: e.target.value});
},
handleDateChange: function(dateString) {
this.setState({deadline: dateString});
console.log(dateToString(dateString));
},
handlePriorityChange: function(e) {
this.setState({priority: e.target.value});
},
handleTextChange: function(e) {
this.setState({text: e.target.value});
},
handleSubmit: function(e) {
e.preventDefault();
var title = this.state.title.trim();
var text = this.state.text.trim();
var priority = this.state.priority;
var deadline = dateToString(this.state.deadline);
console.log(deadline);
if (!title || !text || !deadline) {
return;
}
this.props.onCommentSubmit({a: (this.props.edit ? 'edit' : 'add'), id: -1, title: title, text: text, deadline: deadline, priority: priority});
this.closeModal();
if (!this.props.edit)
this.setState(this.getInitialState());
},
render: function() {
return (
);
}
});
var Item = React.createClass({
rawMarkup: function() {
var md = new Remarkable();
var rawMarkup = md.render(this.props.children.toString());
return { __html: rawMarkup };
},
handleEditSubmit: function(data) {
data.a = 'edit';
data.id = this.props.id;
this.props.handleEditSubmit(data);
},
handleDeleteClick: function() {
console.log("Deleting " + this.props.id);
this.props.handleDeleteClick(this.props.id);
},
render: function() {
var md = new Remarkable();
var daysLeft = Math.ceil((new Date(this.props.deadline) - new Date()) / (1000 * 60 * 60 * 24));
if (isNaN(daysLeft)) {
daysLeft = "No deadline.";
} else if (daysLeft > 1) {
daysLeft += " days left.";
} else if (daysLeft == 1) {
daysLeft = "One day left.";
} else if (daysLeft == 0) {
daysLeft = "Deadline today!";
} else {
daysLeft = "Should have been done " + Math.abs(daysLeft) + " day(s) ago."
}
var deadline = new Date(this.props.deadline);
if (isNaN(deadline))
deadline = new Date();
var todoForm = ;
return (
{daysLeft}{this.props.title}
}
footer={
{todoForm}
}
bsStyle={priorityNames[this.props.priority]}
>
);
}
});
var TodoList = React.createClass({
render: function() {
console.log(this.props.list);
var onDeleteClick = this.props.onDeleteClick;
var handleEditSubmit = this.props.handleEditSubmit;
var itemList = this.props.list.map(function(item) {
return (
-
{item.description}
);
});
return (
{itemList}
);
}
});
var App = React.createClass({
getInitialState: function() {
return {list: [], result: 0};
},
updateList: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'GET',
success: function(data) {
// Not logged in
if (data.result == 1) {
document.location.href = "https://login.tankernn.eu/";
}
// Success
this.setState({list: data.list, result: data.result});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
componentDidMount: function() {
this.updateList();
this.timer = setInterval(this.updateList, 5000);
},
componentWillUnmount: function() {
clearInterval(this.timer);
},
handleCommentSubmit: function(comment) {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
type: 'POST',
data: comment,
success: function(data) {
if (data.result != 0) {
console.log("Error in API: " + data.result);
}
if (data.hasOwnProperty("message")) {
console.log("API message: " + data.message);
}
this.setState({list: data.list, result: data.result});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
this.setState({result: 1});
}.bind(this)
});
},
handleDelete: function(id) {
$.ajax({
url: this.props.url,
dataType: 'json',
cache: false,
type: 'POST',
data: {a: 'rm', id: id},
success: function(data) {
this.setState({list: data.list, result: data.result});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
render: function() {
return (
Tankernn TODO list
);
}
});
ReactDOM.render(, document.getElementById('content'));