Skip to content

Commit

Permalink
wip wiring up account forms
Browse files Browse the repository at this point in the history
  • Loading branch information
cleverbeagle committed May 25, 2017
1 parent cdc15f0 commit 3459bf2
Show file tree
Hide file tree
Showing 25 changed files with 413 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ accounts-google
themeteorchef:bert
fortawesome:fontawesome
aldeed:collection2-core
audit-argument-checks
ddp-rate-limiter
1 change: 1 addition & 0 deletions .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ alanning:[email protected]
aldeed:[email protected]
aldeed:[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Expand Down
2 changes: 1 addition & 1 deletion imports/api/Documents/Documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ Documents.schema = new SimpleSchema({
},
});

Documents.attachSchema(Documents.schema);
// Documents.attachSchema(Documents.schema);

export default Documents;
52 changes: 52 additions & 0 deletions imports/api/Documents/methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import Documents from './Documents';
import rateLimit from '../../modules/rate-limit';

Meteor.methods({
'documents.insert': function documentsInsert(doc) {
check(doc, {
title: String,
body: String,
});

try {
return Documents.insert({ owner: this.userId, ...doc });
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
'documents.update': function documentsUpdate(doc) {
check(doc, {
_id: String,
title: String,
body: String,
});

try {
Documents.update(doc._id, { $set: doc });
return doc._id; // Return _id so we can redirect to document after update.
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
'documents.remove': function documentsRemove(documentId) {
check(documentId, String);

try {
return Documents.remove(documentId);
} catch (exception) {
throw new Meteor.Error('500', exception);
}
},
});

rateLimit({
methods: [
'documents.insert',
'documents.update',
'documents.remove',
],
limit: 5,
timeRange: 1000,
});
17 changes: 17 additions & 0 deletions imports/api/Documents/server/publications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check';
import Documents from '../Documents';

Meteor.publish('documents', function documentsView() {
return Documents.find({ owner: this.userId });
});

// Note: documents.view is also used when editing an existing document.
Meteor.publish('documents.view', function documentsView(documentId) {
check(documentId, String);

const doc = Documents.find(documentId);
const isOwner = doc.fetch()[0].owner === this.userId;

return isOwner ? doc : this.ready();
});
18 changes: 18 additions & 0 deletions imports/modules/rate-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Meteor } from 'meteor/meteor';
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
import { _ } from 'meteor/underscore';

const fetchMethodNames = methods => _.pluck(methods, 'name');

const assignLimits = ({ methods, limit, timeRange }) => {
const methodNames = fetchMethodNames(methods);

if (Meteor.isServer) {
DDPRateLimiter.addRule({
name(name) { return _.contains(methodNames, name); },
connectionId() { return true; },
}, limit, timeRange);
}
};

export default function rateLimit(options) { return assignLimits(options); }
2 changes: 2 additions & 0 deletions imports/startup/server/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '../../api/Documents/methods';
import '../../api/Documents/server/publications';
1 change: 1 addition & 0 deletions imports/startup/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './api';
97 changes: 97 additions & 0 deletions imports/ui/components/DocumentEditor/DocumentEditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* eslint-disable max-len, no-return-assign */

import React from 'react';
import PropTypes from 'prop-types';
import { FormGroup, ControlLabel, Button } from 'react-bootstrap';
import { Meteor } from 'meteor/meteor';
import { Bert } from 'meteor/themeteorchef:bert';
import validate from '../../../modules/validate';

class DocumentEditor extends React.Component {
componentDidMount() {
const component = this;
validate(component.form, {
rules: {
title: {
required: true,
},
body: {
required: true,
},
},
messages: {
title: {
required: 'Need a title in here, Seuss.',
},
body: {
required: 'This thneeds a body, please.',
},
},
submitHandler() { component.handleSubmit(); },
});
}

handleSubmit() {
const { history } = this.props;
const existingDocument = this.props.doc && this.props.doc._id;
const methodToCall = existingDocument ? 'documents.update' : 'documents.insert';
const doc = {
title: this.title.value.trim(),
body: this.body.value.trim(),
};

if (existingDocument) doc._id = existingDocument;

Meteor.call(methodToCall, doc, (error, documentId) => {
if (error) {
Bert.alert(error.reason, 'danger');
} else {
const confirmation = existingDocument ? 'Document updated!' : 'Document added!';
this.form.reset();
Bert.alert(confirmation, 'success');
history.push(`/documents/${documentId}`);
}
});
}

render() {
const { doc } = this.props;
return (<form ref={form => (this.form = form)} onSubmit={event => event.preventDefault()}>
<FormGroup>
<ControlLabel>Title</ControlLabel>
<input
type="text"
className="form-control"
name="title"
ref={title => (this.title = title)}
defaultValue={doc && doc.title}
placeholder="Oh, The Places You'll Go!"
/>
</FormGroup>
<FormGroup>
<ControlLabel>Body</ControlLabel>
<textarea
className="form-control"
name="body"
ref={body => (this.body = body)}
defaultValue={doc && doc.body}
placeholder="Congratulations! Today is your day. You're off to Great Places! You're off and away!"
/>
</FormGroup>
<Button type="submit" bsStyle="success">
{doc && doc._id ? 'Save Changes' : 'Add Document'}
</Button>
</form>);
}
}

DocumentEditor.defaultProps = {
doc: PropTypes.object,
};

DocumentEditor.propTypes = {
doc: PropTypes.object,
history: PropTypes.object.isRequired,
};

export default DocumentEditor;
2 changes: 1 addition & 1 deletion imports/ui/components/OAuthLoginButton/OAuthLoginButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ OAuthLoginButton.defaultProps = {

OAuthLoginButton.propTypes = {
service: PropTypes.string.isRequired,
options: PropTypes.object, // eslint-disable-line react/forbid-prop-types
options: PropTypes.object,
callback: PropTypes.func,
};

Expand Down
80 changes: 80 additions & 0 deletions imports/ui/pages/Documents/Documents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import { Table, Alert, Button } from 'react-bootstrap';
import { timeago, monthDayYearAtTime } from '@cleverbeagle/dates';
import { Meteor } from 'meteor/meteor';
import { createContainer } from 'meteor/react-meteor-data';
import { Bert } from 'meteor/themeteorchef:bert';
import DocumentsCollection from '../../../api/Documents/Documents';

import './Documents.scss';

const handleRemove = (documentId) => {
if (confirm('Are you sure? This is permanent!')) {
Meteor.call('documents.remove', documentId, (error) => {
if (error) {
Bert.alert(error.reason, 'danger');
} else {
Bert.alert('Document deleted!', 'success');
}
});
}
};

const Documents = ({ documents, match, history }) => (
<div className="Documents">
<div className="page-header clearfix">
<h4 className="pull-left">Documents</h4>
<Link className="btn btn-success pull-right" to={`${match.url}/new`}>Add Document</Link>
</div>
{documents.length ? <Table responsive>
<thead>
<tr>
<th>Title</th>
<th>Last Updated</th>
<th>Created</th>
<th />
<th />
</tr>
</thead>
<tbody>
{documents.map(({ _id, title, createdAt, updatedAt }) => (
<tr key={_id}>
<td>{title}</td>
<td>{timeago(updatedAt)}</td>
<td>{monthDayYearAtTime(createdAt)}</td>
<td>
<Button
bsStyle="primary"
onClick={() => history.push(`${match.url}/${_id}`)}
block
>View</Button>
</td>
<td>
<Button
bsStyle="danger"
onClick={() => handleRemove(_id)}
block
>Delete</Button>
</td>
</tr>
))}
</tbody>
</Table> : <Alert bsStyle="warning">No documents yet!</Alert>}
</div>
);

Documents.propTypes = {
documents: PropTypes.arrayOf(PropTypes.object).isRequired,
match: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
};

export default createContainer(() => {
const subscription = Meteor.subscribe('documents');
return {
loading: !subscription.ready(),
documents: DocumentsCollection.find().fetch(),
};
}, Documents);
3 changes: 3 additions & 0 deletions imports/ui/pages/Documents/Documents.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Documents table tbody tr td {
vertical-align: middle;
}
29 changes: 29 additions & 0 deletions imports/ui/pages/EditDocument/EditDocument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import PropTypes from 'prop-types';
import { createContainer } from 'meteor/react-meteor-data';
import { Meteor } from 'meteor/meteor';
import Documents from '../../../api/Documents/Documents';
import DocumentEditor from '../../components/DocumentEditor/DocumentEditor';
import NotFound from '../NotFound/NotFound';

const EditDocument = ({ doc, history }) => (doc ? (
<div className="EditDocument">
<h4 className="page-header">{`Editing "${doc.title}"`}</h4>
<DocumentEditor doc={doc} history={history} />
</div>
) : <NotFound />);

EditDocument.propTypes = {
doc: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
};

export default createContainer(({ match }) => {
const documentId = match.params._id;
const subscription = Meteor.subscribe('documents.view', documentId);

return {
loading: !subscription.ready(),
doc: Documents.findOne(documentId),
};
}, EditDocument);
7 changes: 5 additions & 2 deletions imports/ui/pages/Login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ class Login extends React.Component {
/>
</FormGroup>
<FormGroup>
<ControlLabel>Password</ControlLabel>
<ControlLabel className="clearfix">
<span className="pull-left">Password</span>
<Link className="pull-right" to="/recover-password">Forgot password?</Link>
</ControlLabel>
<input
type="password"
name="password"
Expand All @@ -100,7 +103,7 @@ class Login extends React.Component {
}

Login.propTypes = {
history: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
history: PropTypes.object.isRequired,
};

export default Login;
20 changes: 0 additions & 20 deletions imports/ui/pages/Login/Login.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
@import '../../stylesheets/colors';

.Login {
.page-header {
margin-top: 0;
}

form {
position: relative;
border-top: 1px solid $gray-lighter;
Expand All @@ -29,19 +25,3 @@
}
}
}

@include breakpoint(tablet) {
.Login {
.page-header {
margin-top: 10px;
}
}
}

@include breakpoint(desktop) {
.Login {
.page-header {
margin-top: 20px;
}
}
}
Loading

0 comments on commit 3459bf2

Please sign in to comment.