Skip to content

Commit

Permalink
Merge branch 'master' into xingyi
Browse files Browse the repository at this point in the history
  • Loading branch information
Waffelz authored Aug 11, 2017
2 parents b7b5803 + 4b57aa3 commit 1474474
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
1 change: 0 additions & 1 deletion backend/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var userSchema = mongoose.Schema({

var documentSchema = mongoose.Schema({
title: String,
date: String,
content:Object,
author: {
type: mongoose.Schema.Types.ObjectId,
Expand Down
32 changes: 25 additions & 7 deletions backend/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@ router.use(function(req, res, next){

router.post('/newdoc', function(req,res){

// console.log("body.content", req.body.content);
// console.log("req.user?", req.user);
Document.findById(req.user._id, function(err, dock){
Document.find({title:req.body.title}, function(err, dock){
console.log("THIS IS DOCK:", dock);

if (err){
console.log("error in doc find");
} else if (!dock){
} else if (dock.length === 0){
console.log("HALP");
var doc = new Document({

title: req.body.title,
date: req.body.date,
author: req.user._id,
content: req.body.content

})
doc.save(function(err, doc) {
if (err) {
Expand All @@ -53,10 +59,10 @@ router.post('/newdoc', function(req,res){
res.send('created');
});
} else{
console.log("dock in else", dock);
dock.title= req.body.title,
dock.content=req.body.content,
dock.save(function(err,doc){
console.log("dock in else", req.body);
dock[0].title= req.body.body.title,
dock[0].content=req.body.body.content,
dock[0].save(function(err,doc){
if (err){
console.log("robinson",err);
res.status(500).send('ERR');
Expand Down Expand Up @@ -93,7 +99,19 @@ router.get('/doclist', function(req, res){
})
// console.log("array of docs? or objects?", Doc);
})

router.get('/opendoc', function(req,res){
console.log("getting doc info to pop editor", req.query);
Document.findById(req.query.id, function(err, doc){
if(err){
console.log("problem getting doc info to popualte editor", err);
res.status(500).send('ERR');
return;
}else {
console.log("got doc info", doc);
res.send(doc)
}
})
})
router.get('/protected', function(req, res, next) {
console.log('user reached backend')
res.send('yo')
Expand Down
1 change: 0 additions & 1 deletion reactApp/components/Docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class Docs extends React.Component {
<input type="submit" value="Create" onClick={(e) => {
this.modal1Toggle(e)
this.createDoc(e)
this.props.history.push('/editor')
}}/>
</div>
</div>
Expand Down
33 changes: 27 additions & 6 deletions reactApp/components/MyEditor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var React = require('react');
import ReactDOM from 'react-dom';
import {Editor, EditorState, Modifier, RichUtils, ContentState, convertFromoRaw} from 'draft-js';
import {Editor, EditorState, Modifier, RichUtils, ContentState, convertFromRaw} from 'draft-js';
import Toolbar from './Toolbar';
import Register from './Register';
var axios = require('axios');
Expand Down Expand Up @@ -121,17 +121,38 @@ const ColorControls = (props) => {
class MyEditor extends React.Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.state = {
editorState: EditorState.createEmpty(),
title: "Untitled"
};

this.focus = () => this.refs.editor.focus();
this.onChange = (editorState) => this.setState({editorState});
this.toggleColor = (toggledColor) => this.toggleColor(toggledColor);
}
componentDidMount(){
console.log("state:", this.state.editorState);
axios.get('http://localhost:3000/doc')
console.log("props", this.props.match);
fetch(`http://localhost:3000/opendoc?id=${this.props.match.params.id}`, {
credentials: 'include',
})
.then((response)=>{
console.log("find the raw content to convert from:", response.data);
// console.log("find the raw content to convert from:", response.json());
// var notRaw = convertFromRaw(response.data.content);
// console.log("converted from raw", notRaw);
// this.setState({
// editorState: EditorState.createWithContent(notRaw),
// title: response.data.title
// })
return response.json();
})
.then((resp) => {
console.log("RESP: ", resp);
var notRaw = convertFromRaw(JSON.parse(resp.content));
console.log("converted from raw", notRaw);
this.setState({
editorState: EditorState.createWithContent(notRaw),
title: resp.title
})
})
.catch((err)=>{
console.log("error getting doc page", err);
Expand Down Expand Up @@ -216,7 +237,7 @@ class MyEditor extends React.Component {
onToggle={this.toggleColor}
/> */}

<Toolbar editorState={this.state.editorState} boldClicked={(e) => this.boldClicked(e)}
<Toolbar title={this.state.title} editorState={this.state.editorState} boldClicked={(e) => this.boldClicked(e)}
italicClicked={(e) => this.italicClicked(e)}
underlineClicked={(e) => this.underlineClicked(e)}
codeClicked={(e) => this.codeClicked(e)}
Expand Down
19 changes: 13 additions & 6 deletions reactApp/components/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Toolbar extends React.Component {
constructor(props){
super(props);
this.state = {
title: "Untitled Document",
title: this.props.title,
modal: false
}
}
Expand All @@ -17,12 +17,19 @@ class Toolbar extends React.Component {
var rawContent = convertToRaw(content)
console.log("raw content state:", rawContent);
var date = new Date();
//{content:JSON.stringify(rawContent), title: this.state.title}
axios.post('http://localhost:3000/newdoc', {
title: this.state.title,
date: date,
content: rawContent

})
method: "POST",
credentials: 'include',
headers: {'Content-Type': 'application/json'},
body: {content:JSON.stringify(rawContent), title: this.state.title}
} )
// fetch('http://localhost:3000/newdoc', {
// method: "POST",
// credentials: 'include',
// headers: {'Content-Type': 'application/json'},
// body: {name: "xhonia"}
// })
.then(function (response) {
console.log("success response on creat doc",response);
//if response is good, do something maybe?
Expand Down

0 comments on commit 1474474

Please sign in to comment.