diff --git a/client/components/Editor.js b/client/components/Editor.js
index f2321b7..ac59bb9 100644
--- a/client/components/Editor.js
+++ b/client/components/Editor.js
@@ -1,8 +1,10 @@
import React from 'react'
+import {connect} from 'react-redux'
import {UnControlled} from 'react-codemirror2'
import {transformCodeMirrorChange} from '../javascriptstuff/otoffsetfuncs'
import connection from '../connect'
import {operationsfunc} from '../javascriptstuff/operationsfunc'
+import {withRouter} from 'react-router-dom'
require('codemirror/mode/javascript/javascript')
@@ -20,7 +22,14 @@ export class Editor extends React.Component {
}
componentDidMount() {
- this.doc = connection.get('demo', 'inputbox')
+ console.log(this.props.match.params)
+ //this.doc = connection.get('demo', 'inputbox')
+ this.doc = connection.get(
+ this.props.match.params.user,
+ this.props.match.params.docname
+ )
+ console.log(this.doc.collection)
+ console.log(this.doc.id)
this.doc.subscribe(err => {
if (err) throw err
@@ -61,3 +70,5 @@ export class Editor extends React.Component {
return this.editor ? main : loading
}
}
+
+export default withRouter(connect(null, null)(Editor))
diff --git a/client/components/MyForm.js b/client/components/MyForm.js
new file mode 100644
index 0000000..9cf2124
--- /dev/null
+++ b/client/components/MyForm.js
@@ -0,0 +1,86 @@
+import React, {Component} from 'react'
+import {connect} from 'react-redux'
+//import connection from '../connect'
+import {withRouter} from 'react-router-dom'
+import {getDocsThunk, updateDocThunk} from '../store/doc'
+import {Container, Form, TextArea, Button} from 'semantic-ui-react'
+
+//Props has username availabe, so why dont we
+//make a query to the database to check all documents
+//with username and also
+//make a thunk that posts new ones and routes to them here
+
+export class MyForm extends Component {
+ constructor(props) {
+ super(props)
+
+ this.state = {
+ doc: '',
+ userId: ''
+ }
+
+ this.handleChange = this.handleChange.bind(this)
+ this.handleSubmit = this.handleSubmit.bind(this)
+ }
+
+ componentDidMount() {
+ //stuff
+ //this.props.docs()
+ }
+
+ handleChange(event) {
+ this.setState({
+ [event.target.name]: event.target.value
+ })
+ }
+
+ handleSubmit(event) {
+ event.preventDefault()
+ let userId = this.state.userId
+ let docname = this.state.doc
+ this.props.props.history.push(`/docs/${userId}/${docname}`)
+ }
+
+ render() {
+ let main = (
+
+
+
+
+ )
+
+ return main
+ }
+}
+
+const mapDispatchToProps = dispatch => ({
+ update: (userId, docname) => dispatch(updateDocThunk(userId, docname)),
+ docs: () => dispatch(getDocsThunk())
+})
+
+export default withRouter(connect(null, mapDispatchToProps)(MyForm))
diff --git a/client/components/index.js b/client/components/index.js
index 8ee2f23..acb340f 100644
--- a/client/components/index.js
+++ b/client/components/index.js
@@ -8,3 +8,4 @@ export {default as UserHome} from './user-home'
export {Login, Signup} from './auth-form'
export {Editor} from './Editor'
+export {MyForm} from './MyForm'
diff --git a/client/components/user-home.js b/client/components/user-home.js
index 8b0f1e0..9607374 100644
--- a/client/components/user-home.js
+++ b/client/components/user-home.js
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import {Editor} from './Editor'
+import {MyForm} from './MyForm'
/**
* COMPONENT
@@ -10,11 +11,15 @@ import {Editor} from './Editor'
export const UserHome = props => {
const {email} = props
+ const userId = props.id
+ console.log(props)
return (
Welcome, {email}
-
+ {/* */}
+ {/* */}
+
)
}
@@ -24,6 +29,7 @@ export const UserHome = props => {
*/
const mapState = state => {
return {
+ id: state.user.id,
email: state.user.email
}
}
diff --git a/client/routes.js b/client/routes.js
index 0f6f794..998bb7d 100644
--- a/client/routes.js
+++ b/client/routes.js
@@ -4,6 +4,7 @@ import {withRouter, Route, Switch} from 'react-router-dom'
import PropTypes from 'prop-types'
import {Login, Signup, UserHome} from './components'
import {me} from './store'
+import {Editor} from './components/Editor'
/**
* COMPONENT
@@ -25,6 +26,7 @@ class Routes extends Component {
{/* Routes placed here are only available after logging in */}
+
)}
{/* Displays our Login component as a fallback */}
diff --git a/client/store/doc.js b/client/store/doc.js
new file mode 100644
index 0000000..fc93b8a
--- /dev/null
+++ b/client/store/doc.js
@@ -0,0 +1,42 @@
+import axios from 'axios'
+
+//const UPDATE_DOCS_TABLE = 'UPDATE_DOCS_TABLE'
+const GET_DOCS = 'GET_DOCS'
+
+export const getDocs = document => ({
+ type: GET_DOCS,
+ document
+})
+
+// export const updateDocsTable = doc => ({
+// type: UPDATE_DOCS_TABLE,
+// doc
+// })
+
+export const getDocsThunk = (userId, docname) => async dispatch => {
+ console.log('you made it to getDocshunkT')
+ const {data} = await axios.get(`/api/docs/${userId}/${docname}`)
+ console.log(data)
+ dispatch(getDocs())
+}
+
+// export const updateDocThunk = (userId, docname) => async dispatch => {
+// console.log('you made it to updateDocThunk')
+// const {data} = await axios.post(`/api/docs/${userId}/${docname}`)
+// console.log(data)
+// dispatch(updateDocsTable())
+// }
+
+export default function docReducer(document = '', action) {
+ //console.log('made it to reducer')
+ console.log(action)
+ switch (action.type) {
+ //case UPDATE_DOCS_TABLE:
+ // return action.doc
+ case GET_DOCS:
+ console.log('you made it to GET_DOCS in the reducer')
+ return action.document
+ default:
+ return document
+ }
+}
diff --git a/client/store/index.js b/client/store/index.js
index 4bbc6da..d51b32c 100644
--- a/client/store/index.js
+++ b/client/store/index.js
@@ -3,8 +3,9 @@ import {createLogger} from 'redux-logger'
import thunkMiddleware from 'redux-thunk'
import {composeWithDevTools} from 'redux-devtools-extension'
import user from './user'
+import docReducer from './doc'
-const reducer = combineReducers({user})
+const reducer = combineReducers({user, document: docReducer})
const middleware = composeWithDevTools(
applyMiddleware(thunkMiddleware, createLogger({collapsed: true}))
)
@@ -12,3 +13,4 @@ const store = createStore(reducer, middleware)
export default store
export * from './user'
+export * from './doc'
diff --git a/server/api/docs.js b/server/api/docs.js
new file mode 100644
index 0000000..7123372
--- /dev/null
+++ b/server/api/docs.js
@@ -0,0 +1,46 @@
+const router = require('express').Router()
+const {User, Doc, ThroughTable} = require('../db/models')
+
+module.exports = router
+
+router.get('/:userId/:docname', async (req, res, next) => {
+ try {
+ //const docs = await Doc.findAll()
+ //let docs = req.params.userId + ' ' + req.params.docname
+ let userId = req.params.userId
+ let docname = req.params.docname
+ let docs = await Doc.findAll({
+ include: [
+ {
+ model: ThroughTable,
+ where: {userId}
+ }
+ ]
+ })
+ res.json(docs)
+ } catch (err) {
+ next(err)
+ }
+})
+
+router.get('/', async (req, res, next) => {
+ try {
+ const docs = await Doc.findAll()
+ res.json(docs)
+ } catch (err) {
+ next(err)
+ }
+})
+
+router.post('/:userId/:docname', async (req, res, next) => {
+ try {
+ //find or create document
+ console.log('made it to router.put')
+ const doc = await Doc.findOrCreate({
+ where: {userId: req.params.userId, docname: req.params.docname}
+ })
+ res.status(200)
+ } catch (error) {
+ next(error)
+ }
+})
diff --git a/server/api/index.js b/server/api/index.js
index 91685d3..07b081e 100644
--- a/server/api/index.js
+++ b/server/api/index.js
@@ -2,6 +2,7 @@ const router = require('express').Router()
module.exports = router
router.use('/users', require('./users'))
+router.use('/docs', require('./docs'))
router.use((req, res, next) => {
const error = new Error('Not Found')
diff --git a/server/db/models/doc.js b/server/db/models/doc.js
new file mode 100644
index 0000000..425658a
--- /dev/null
+++ b/server/db/models/doc.js
@@ -0,0 +1,18 @@
+const Sequelize = require('sequelize')
+const db = require('../db')
+
+const Doc = db.define(
+ 'doc',
+ {
+ docname: {
+ type: Sequelize.STRING,
+ unique: true,
+ allowNull: false
+ }
+ },
+ {
+ timestamps: false
+ }
+)
+
+module.exports = Doc
diff --git a/server/db/models/index.js b/server/db/models/index.js
index 7b33fd0..e3619a7 100644
--- a/server/db/models/index.js
+++ b/server/db/models/index.js
@@ -1,7 +1,8 @@
const User = require('./user')
const Ops = require('./ops')
const Snapshots = require('./snapshots')
-
+const Doc = require('./doc')
+const ThroughTable = require('./throughTable')
/**
* If we had any associations to make, this would be a great place to put them!
* ex. if we had another model called BlogPost, we might say:
@@ -9,6 +10,9 @@ const Snapshots = require('./snapshots')
* BlogPost.belongsTo(User)
*/
+User.belongsToMany(Doc, {through: ThroughTable})
+Doc.belongsToMany(User, {through: ThroughTable})
+
/**
* We'll export all of our models here, so that any time a module needs a model,
* we can just require it from 'db/models'
@@ -18,5 +22,7 @@ const Snapshots = require('./snapshots')
module.exports = {
User,
Ops,
- Snapshots
+ Snapshots,
+ Doc,
+ ThroughTable
}
diff --git a/server/db/models/throughTable.js b/server/db/models/throughTable.js
new file mode 100644
index 0000000..848fa3a
--- /dev/null
+++ b/server/db/models/throughTable.js
@@ -0,0 +1,16 @@
+const Sequelize = require('sequelize')
+const db = require('../db')
+
+const ThroughTable = db.define(
+ 'throughTable',
+ {
+ userId: {
+ type: Sequelize.STRING
+ }
+ },
+ {
+ timestamps: false
+ }
+)
+
+module.exports = ThroughTable
diff --git a/server/db/sdb.js b/server/db/sdb.js
deleted file mode 100644
index ecb80b7..0000000
--- a/server/db/sdb.js
+++ /dev/null
@@ -1,19 +0,0 @@
-//onst Sequelize = require('sequelize')
-// const Sharedb = require('sharedb-postgres')
-// const pkg = require('../../package.json')
-
-// const databaseName = pkg.name + (process.env.NODE_ENV === 'test' ? '-test' : '')
-
-// const sdb = new Sharedb(
-// process.env.DATABASE_URL || `postgres://localhost:5432/${databaseName}`,
-// {
-// logging: false
-// }
-// )
-// module.exports = sdb
-
-// This is a global Mocha hook used for resource cleanup.
-// Otherwise, Mocha v4+ does not exit after tests.
-// if (process.env.NODE_ENV === 'test') {
-// after('close database connection', () => sdb.close())
-// }