mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
default mutations can now take options object; add Flash core component; fix Stripe bug
This commit is contained in:
parent
c4cd01a0f3
commit
e4e1f0c41c
4 changed files with 77 additions and 9 deletions
56
packages/vulcan-core/lib/modules/components/Flash.jsx
Normal file
56
packages/vulcan-core/lib/modules/components/Flash.jsx
Normal file
|
@ -0,0 +1,56 @@
|
|||
import { Components, registerComponent } from 'meteor/vulcan:lib';
|
||||
import withMessages from '../containers/withMessages.js';
|
||||
import React, { PureComponent } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import Alert from 'react-bootstrap/lib/Alert'
|
||||
|
||||
class Flash extends PureComponent {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.dismissFlash = this.dismissFlash.bind(this);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.props.markAsSeen(this.props.message._id);
|
||||
}
|
||||
|
||||
dismissFlash(e) {
|
||||
e.preventDefault();
|
||||
this.props.clear(this.props.message._id);
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
let flashType = this.props.message.flashType;
|
||||
flashType = flashType === "error" ? "danger" : flashType; // if flashType is "error", use "danger" instead
|
||||
|
||||
return (
|
||||
<Alert className="flash-message" bsStyle={flashType} onDismiss={this.dismissFlash}>
|
||||
{this.props.message.content}
|
||||
</Alert>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Flash.propTypes = {
|
||||
message: PropTypes.object.isRequired
|
||||
}
|
||||
|
||||
registerComponent('Flash', Flash);
|
||||
|
||||
const FlashMessages = ({messages, clear, markAsSeen}) => {
|
||||
return (
|
||||
<div className="flash-messages">
|
||||
{messages
|
||||
.filter(message => message.show)
|
||||
.map(message => <Components.Flash key={message._id} message={message} clear={clear} markAsSeen={markAsSeen} />)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
FlashMessages.displayName = "FlashMessages";
|
||||
|
||||
registerComponent('FlashMessages', FlashMessages, withMessages);
|
||||
|
||||
export default withMessages(FlashMessages);
|
|
@ -7,7 +7,7 @@ Default mutations
|
|||
import { newMutation, editMutation, removeMutation, Utils } from 'meteor/vulcan:lib';
|
||||
import Users from 'meteor/vulcan:users';
|
||||
|
||||
export const getDefaultMutations = collectionName => ({
|
||||
export const getDefaultMutations = (collectionName, options = {}) => ({
|
||||
|
||||
// mutation for inserting a new document
|
||||
|
||||
|
@ -16,14 +16,17 @@ export const getDefaultMutations = collectionName => ({
|
|||
name: `${collectionName}New`,
|
||||
|
||||
// check function called on a user to see if they can perform the operation
|
||||
check(user) {
|
||||
check(user, document) {
|
||||
if (options.newCheck) {
|
||||
return options.newCheck(user, document);
|
||||
}
|
||||
// if user is not logged in, disallow operation
|
||||
if (!user) return false;
|
||||
// else, check if they can perform "foo.new" operation (e.g. "movies.new")
|
||||
return Users.canDo(user, `${collectionName.toLowerCase()}.new`);
|
||||
},
|
||||
|
||||
mutation(root, {document}, context) {
|
||||
async mutation(root, {document}, context) {
|
||||
|
||||
const collection = context[collectionName];
|
||||
|
||||
|
@ -31,7 +34,7 @@ export const getDefaultMutations = collectionName => ({
|
|||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
||||
// pass document to boilerplate newMutation function
|
||||
return newMutation({
|
||||
return await newMutation({
|
||||
collection,
|
||||
document: document,
|
||||
currentUser: context.currentUser,
|
||||
|
@ -50,6 +53,10 @@ export const getDefaultMutations = collectionName => ({
|
|||
|
||||
// check function called on a user and document to see if they can perform the operation
|
||||
check(user, document) {
|
||||
if (options.editCheck) {
|
||||
return options.editCheck(user);
|
||||
}
|
||||
|
||||
if (!user || !document) return false;
|
||||
// check if user owns the document being edited.
|
||||
// if they do, check if they can perform "foo.edit.own" action
|
||||
|
@ -57,7 +64,7 @@ export const getDefaultMutations = collectionName => ({
|
|||
return Users.owns(user, document) ? Users.canDo(user, `${collectionName.toLowerCase()}.edit.own`) : Users.canDo(user, `${collectionName.toLowerCase()}.edit.all`);
|
||||
},
|
||||
|
||||
mutation(root, {documentId, set, unset}, context) {
|
||||
async mutation(root, {documentId, set, unset}, context) {
|
||||
|
||||
const collection = context[collectionName];
|
||||
|
||||
|
@ -68,7 +75,7 @@ export const getDefaultMutations = collectionName => ({
|
|||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
||||
// call editMutation boilerplate function
|
||||
return editMutation({
|
||||
return await editMutation({
|
||||
collection,
|
||||
documentId: documentId,
|
||||
set: set,
|
||||
|
@ -88,18 +95,22 @@ export const getDefaultMutations = collectionName => ({
|
|||
name: `${collectionName}Remove`,
|
||||
|
||||
check(user, document) {
|
||||
if (options.removeCheck) {
|
||||
return options.removeCheck(user);
|
||||
}
|
||||
|
||||
if (!user || !document) return false;
|
||||
return Users.owns(user, document) ? Users.canDo(user, `${collectionName.toLowerCase()}.remove.own`) : Users.canDo(user, `${collectionName.toLowerCase()}.remove.all`);
|
||||
},
|
||||
|
||||
mutation(root, {documentId}, context) {
|
||||
async mutation(root, {documentId}, context) {
|
||||
|
||||
const collection = context[collectionName];
|
||||
|
||||
const document = collection.findOne(documentId);
|
||||
Utils.performCheck(this.check, context.currentUser, document, context);
|
||||
|
||||
return removeMutation({
|
||||
return await removeMutation({
|
||||
collection,
|
||||
documentId: documentId,
|
||||
currentUser: context.currentUser,
|
||||
|
|
|
@ -18,6 +18,7 @@ export { default as HeadTags } from './components/HeadTags.jsx';
|
|||
export { default as Avatar } from './components/Avatar.jsx';
|
||||
export { default as Card } from './components/Card.jsx';
|
||||
export { default as Datatable } from './components/Datatable.jsx';
|
||||
export { default as Flash } from './components/Flash.jsx';
|
||||
|
||||
export { default as withMessages } from "./containers/withMessages.js";
|
||||
export { default as withList } from './containers/withList.js';
|
||||
|
|
|
@ -114,7 +114,7 @@ export const createCharge = async (args) => {
|
|||
// run collection.charge.sync callbacks
|
||||
modifier = runCallbacks(`${collection._name}.charge.sync`, modifier, document, chargeDoc);
|
||||
|
||||
returnDocument = editMutation({
|
||||
returnDocument = await editMutation({
|
||||
collection,
|
||||
documentId: associatedId,
|
||||
set: modifier.$set,
|
||||
|
|
Loading…
Add table
Reference in a new issue