create separate PostNewForm component; add error messages to post new and edit forms (for now); add errors to comment new and edit

This commit is contained in:
Sacha Greif 2016-03-31 09:36:25 +09:00
parent deb48b5a97
commit 0cb56d8b79
15 changed files with 78 additions and 48 deletions

View file

@ -94,10 +94,10 @@ Meteor.startup(function () {
check(feedUrl, Feeds.schema);
if (Feeds.findOne({url: feedUrl.url}))
throw new Meteor.Error('already-exists', i18n.t('feed_already_exists'));
throw new Meteor.Error('already-exists', __('feed_already_exists'));
if (!Meteor.user() || !Users.is.admin(Meteor.user()))
throw new Meteor.Error('login-required', i18n.t('you_need_to_login_and_be_an_admin_to_add_a_new_feed'));
throw new Meteor.Error('login-required', __('you_need_to_login_and_be_an_admin_to_add_a_new_feed'));
return Feeds.insert(feedUrl);
}

View file

@ -21,7 +21,7 @@ Template.post_subscribe.events({
if (!Meteor.user()) {
FlowRouter.go('signIn');
Messages.flash(i18n.t("please_log_in_first"), "info");
Messages.flash(__("please_log_in_first"), "info");
}
Meteor.call('subscribePost', post._id, function(error, result) {
@ -36,7 +36,7 @@ Template.post_subscribe.events({
if (!Meteor.user()) {
FlowRouter.go('signIn');
Messages.flash(i18n.t("please_log_in_first"), "info");
Messages.flash(__("please_log_in_first"), "info");
}
Meteor.call('unsubscribePost', post._id, function(error, result) {

View file

@ -5,7 +5,7 @@ Template.user_subscribed_posts.helpers({
template: "posts_list_compact",
options: {
currentUser: user,
fieldLabel: i18n.t("subscribedAt"),
fieldLabel: __("subscribedAt"),
fieldValue: function (post) {
var user = this.currentUser;
var item = _.findWhere(user.telescope.subscribedItems.Posts, {itemId: post._id});

View file

@ -17,7 +17,7 @@ class CommentEdit extends Component {
data = {$set: data};
Actions.call("comments.edit", data, this.props.comment._id, (error, result) => {
if (error) {
// handle error
Messages.flash(error.message);
} else {
this.props.submitCallback();
}

View file

@ -3,6 +3,8 @@ import Formsy from 'formsy-react';
import FRC from 'formsy-react-components';
import Actions from '../../actions.js';
import { Button } from 'react-bootstrap';
import Core from "meteor/nova:core";
const Messages = Core.Messages;
const Textarea = FRC.Textarea;
@ -14,7 +16,7 @@ class CommentNew extends Component {
}
submitComment(data) {
const parentComment = this.props.parentComment;
const component = this;
@ -34,7 +36,7 @@ class CommentNew extends Component {
Actions.call("comments.new", data, (error, result) => {
if (error) {
// handle error
Messages.flash(error.message);
} else {
if (this.props.submitCallback) {
this.props.submitCallback();

View file

@ -1,42 +1,18 @@
import React, { PropTypes, Component } from 'react';
import { Button } from 'react-bootstrap';
import Router from '../router.js'
import Core from "meteor/nova:core";
const Messages = Core.Messages;
const ModalTrigger = Core.ModalTrigger;
import ReactForms from "meteor/nova:forms";
const NewDocument = ReactForms.NewDocument;
const NewPostButton = (props, context) => {
({CanCreatePost} = Telescope.components);
({PostNewForm} = Telescope.components);
return (
<ModalTrigger component={<Button bsStyle="primary">New Post</Button>}>
<CanCreatePost user={context.currentUser}>
<div className="new-post-form">
<h3 className="modal-form-title">New Post</h3>
<NewDocument
collection={Posts}
currentUser={context.currentUser}
methodName="posts.new"
successCallback={(post)=>{
Messages.flash("Post created.", "success");
Router.go('posts.single', post);
}}
labelFunction={Telescope.utils.camelToSpaces}
/>
</div>
</CanCreatePost>
<PostNewForm/>
</ModalTrigger>
)
}
NewPostButton.contextTypes = {
currentUser: React.PropTypes.object
};
module.exports = NewPostButton;
export default NewPostButton;

View file

@ -34,6 +34,7 @@ Telescope.registerComponent("PostDay", require('./posts/PostDay.jsx'));
Telescope.registerComponent("Vote", require('./posts/Vote.jsx'));
Telescope.registerComponent("PostThumbnail", require('./posts/list/PostThumbnail.jsx'));
Telescope.registerComponent("PostEditForm", require('./posts/PostEditForm.jsx'));
Telescope.registerComponent("PostNewForm", require('./posts/PostNewForm.jsx'));
// comments

View file

@ -4,6 +4,7 @@ const EditDocument = ReactForms.EditDocument;
import Core from "meteor/nova:core";
const Messages = Core.Messages;
const FlashContainer = Core.FlashContainer;
import Actions from "../actions.js";
@ -25,18 +26,25 @@ class PostEditForm extends Component{
}
render() {
({FlashMessages} = Telescope.components);
return (
<div className="edit-post-form">
<div className="modal-form-title edit-post-form-header">
<h3>Edit Post</h3>
<a onClick={this.deletePost} className="delete-post-link"><Icon name="close"/> Delete Post</a>
</div>
<FlashContainer component={FlashMessages}/>
<EditDocument
collection={Posts}
document={this.props.post}
currentUser={this.context.currentUser}
methodName="posts.edit"
labelFunction={Telescope.utils.camelToSpaces}
errorCallback={(post, error)=>{
Messages.flash(error.message);
}}
/>
</div>
)

View file

@ -0,0 +1,43 @@
import React, { PropTypes, Component } from 'react';
import Router from '../router.js'
import Core from "meteor/nova:core";
const Messages = Core.Messages;
const FlashContainer = Core.FlashContainer;
import ReactForms from "meteor/nova:forms";
const NewDocument = ReactForms.NewDocument;
const PostNewForm = (props, context) => {
({CanCreatePost, FlashMessages} = Telescope.components);
return (
<CanCreatePost user={context.currentUser}>
<div className="new-post-form">
<h3 className="modal-form-title">New Post</h3>
<FlashContainer component={FlashMessages}/>
<NewDocument
collection={Posts}
currentUser={context.currentUser}
methodName="posts.new"
successCallback={(post)=>{
Messages.flash("Post created.", "success");
Router.go('posts.single', post);
}}
errorCallback={(post, error)=>{
Messages.flash(error.message);
}}
labelFunction={Telescope.utils.camelToSpaces}
/>
</div>
</CanCreatePost>
)
}
PostNewForm.contextTypes = {
currentUser: React.PropTypes.object
};
module.exports = PostNewForm;
export default PostNewForm;

View file

@ -11,7 +11,7 @@ Comments.methods.new = function (comment) {
// Don't allow empty comments
if (!comment.body)
throw new Meteor.Error(704,i18n.t('your_comment_is_empty'));
throw new Meteor.Error(704,__('your_comment_is_empty'));
// ------------------------------ Properties ------------------------------ //
@ -89,7 +89,7 @@ Meteor.methods({
// check that user can comment
if (!user || !Users.can.comment(user))
throw new Meteor.Error(i18n.t('you_need_to_login_or_be_invited_to_post_new_comments'));
throw new Meteor.Error(__('you_need_to_login_or_be_invited_to_post_new_comments'));
// ------------------------------ Rate Limiting ------------------------------ //
@ -100,7 +100,7 @@ Meteor.methods({
// check that user waits more than 15 seconds between comments
if((timeSinceLastComment < commentInterval))
throw new Meteor.Error(704, i18n.t('please_wait')+(commentInterval-timeSinceLastComment)+i18n.t('seconds_before_commenting_again'));
throw new Meteor.Error(704, __('please_wait')+(commentInterval-timeSinceLastComment)+__('seconds_before_commenting_again'));
}
@ -118,7 +118,7 @@ Meteor.methods({
} else {
var field = schema[fieldName];
if (!Users.can.submitField(user, field)) {
throw new Meteor.Error("disallowed_property", i18n.t('disallowed_property_detected') + ": " + fieldName);
throw new Meteor.Error("disallowed_property", __('disallowed_property_detected') + ": " + fieldName);
}
}
@ -146,7 +146,7 @@ Meteor.methods({
// check that user can edit
if (!user || !Users.can.edit(user, comment)) {
throw new Meteor.Error(601, i18n.t('sorry_you_cannot_edit_this_comment'));
throw new Meteor.Error(601, __('sorry_you_cannot_edit_this_comment'));
}
// go over each field and throw an error if it's not editable
@ -157,7 +157,7 @@ Meteor.methods({
var field = schema[fieldName];
if (!Users.can.editField(user, field, comment)) {
throw new Meteor.Error("disallowed_property", i18n.t('disallowed_property_detected') + ": " + fieldName);
throw new Meteor.Error("disallowed_property", __('disallowed_property_detected') + ": " + fieldName);
}
});

View file

@ -10,7 +10,7 @@ Telescope.callbacks.add("postClass", addThumbnailClass);
function checkIfPreviouslyPosted (data) {
Meteor.call("checkForDuplicates", data.url, function (error, result) {
if (error) {
Messages.flash(error.reason + '. <a href="'+FlowRouter.path("postPage", {_id: error.details})+'">'+i18n.t("go_to_post")+'</a>');
Messages.flash(error.reason + '. <a href="'+FlowRouter.path("postPage", {_id: error.details})+'">'+__("go_to_post")+'</a>');
}
});
return data;

View file

@ -40,7 +40,7 @@ const EditDocument = React.createClass({
if (error) {
console.log(error)
if (this.props.errorCallback) {
this.props.errorCallback(document);
this.props.errorCallback(document, error);
}
} else {
if (this.props.successCallback) {

View file

@ -27,7 +27,7 @@ const NewDocument = React.createClass({
if (error) {
console.log(error)
if (this.props.errorCallback) {
this.props.errorCallback(document);
this.props.errorCallback(document, error);
}
} else {
if (this.props.successCallback) {

View file

@ -52,7 +52,7 @@ Users.before.update(function (userId, doc, fieldNames, modifier) {
// check for existing emails and throw error if necessary
var userWithSameEmail = Users.findByEmail(newEmail);
if (userWithSameEmail && userWithSameEmail._id !== doc._id) {
throw new Meteor.Error("email_taken2", i18n.t("this_email_is_already_taken") + " (" + newEmail + ")");
throw new Meteor.Error("email_taken2", __("this_email_is_already_taken") + " (" + newEmail + ")");
}
// if user.emails exists, change it too

View file

@ -47,12 +47,12 @@ Meteor.methods({
// check that user can edit document
if (!user || !Users.can.edit(currentUser, user)) {
throw new Meteor.Error(601, i18n.t('sorry_you_cannot_edit_this_user'));
throw new Meteor.Error(601, __('sorry_you_cannot_edit_this_user'));
}
// if an $unset modifier is present, it means one or more of the fields is missing
if (modifier.$unset) {
throw new Meteor.Error(601, i18n.t('all_fields_are_required'));
throw new Meteor.Error(601, __('all_fields_are_required'));
}
// check for existing emails and throw error if necessary
@ -60,7 +60,7 @@ Meteor.methods({
if (modifier.$set && modifier.$set["telescope.email"]) {
var email = modifier.$set["telescope.email"];
if (Users.findByEmail(email)) {
throw new Meteor.Error("email_taken1", i18n.t("this_email_is_already_taken") + " (" + email + ")");
throw new Meteor.Error("email_taken1", __("this_email_is_already_taken") + " (" + email + ")");
}
}
@ -72,7 +72,7 @@ Meteor.methods({
_.keys(operation).forEach(function (fieldName) {
var field = schema[fieldName];
if (!Users.can.editField(user, field, user)) {
throw new Meteor.Error("disallowed_property", i18n.t('disallowed_property_detected') + ": " + fieldName);
throw new Meteor.Error("disallowed_property", __('disallowed_property_detected') + ": " + fieldName);
}
});