2016-11-25 10:58:32 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Generic mutation wrapper to insert a new document in a collection and update
|
|
|
|
a related query on the client with the new item and a new total item count.
|
|
|
|
|
|
|
|
Sample mutation:
|
|
|
|
|
|
|
|
mutation moviesNew($document: MoviesInput) {
|
|
|
|
moviesNew(document: $document) {
|
|
|
|
...MoviesNewFormFragment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-11-12 15:49:07 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
|
|
|
import hoistStatics from 'hoist-non-react-statics'
|
2016-11-15 18:33:16 +01:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
2016-11-22 21:12:14 +09:00
|
|
|
import update from 'immutability-helper';
|
2016-11-12 15:49:07 +01:00
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
export default function withNew(WrappedComponent) {
|
2016-11-12 15:49:07 +01:00
|
|
|
|
2016-11-25 10:58:32 +09:00
|
|
|
// create a new stateless component so we can access its props
|
|
|
|
const WithNew = props => {
|
|
|
|
|
|
|
|
console.log(props)
|
|
|
|
|
|
|
|
const { collection, fragmentName, fragment, queryName } = props,
|
|
|
|
collectionName = collection._name,
|
|
|
|
mutationName = collection.options.mutations.new.name,
|
|
|
|
listResolverName = collection.options.resolvers.list.name,
|
|
|
|
totalResolverName = collection.options.resolvers.total.name;
|
|
|
|
|
|
|
|
// if a queryName is passed as prop, generate updateQueries function
|
|
|
|
let updateQueries = {};
|
|
|
|
if (queryName) {
|
|
|
|
updateQueries = {
|
|
|
|
[queryName]: (prev, { mutationResult }) => {
|
|
|
|
|
|
|
|
// get new document to insert
|
|
|
|
const newDocument = mutationResult.data[mutationName];
|
|
|
|
|
|
|
|
// generate new list with updated total count and document inserted at top
|
|
|
|
const newList = update(prev, {
|
|
|
|
[listResolverName]: { $unshift: [newDocument] },
|
|
|
|
[totalResolverName]: { $set: prev[totalResolverName] + 1 }
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(`// updated query ${queryName}:`)
|
|
|
|
console.log(prev)
|
|
|
|
console.log(newList)
|
|
|
|
|
|
|
|
return newList;
|
2016-11-22 21:12:14 +09:00
|
|
|
}
|
2016-11-12 15:49:07 +01:00
|
|
|
}
|
2016-11-25 10:58:32 +09:00
|
|
|
}
|
2016-11-24 16:58:08 +09:00
|
|
|
|
2016-11-25 10:58:32 +09:00
|
|
|
// wrap component with graphql HoC
|
|
|
|
const ComponentWithNew = graphql(gql`
|
|
|
|
mutation ${mutationName}($document: ${collectionName}Input) {
|
|
|
|
${mutationName}(document: $document) {
|
|
|
|
...${fragmentName}
|
2016-11-22 21:12:14 +09:00
|
|
|
}
|
2016-11-25 10:58:32 +09:00
|
|
|
}
|
|
|
|
${fragment}
|
|
|
|
`, {
|
|
|
|
props: ({ownProps, mutate}) => ({
|
|
|
|
newMutation: ({document}) => {
|
|
|
|
|
|
|
|
console.log('// newMutation')
|
|
|
|
console.log(updateQueries)
|
|
|
|
|
|
|
|
return mutate({
|
|
|
|
variables: { document },
|
|
|
|
updateQueries: props.updateQueries || updateQueries
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
})(WrappedComponent);
|
|
|
|
|
|
|
|
// render wrapped component
|
|
|
|
return <ComponentWithNew {...props} />
|
2016-11-24 16:58:08 +09:00
|
|
|
}
|
2016-11-12 15:49:07 +01:00
|
|
|
|
2016-11-15 18:33:16 +01:00
|
|
|
WithNew.displayName = `withNew(${Telescope.utils.getComponentDisplayName(WrappedComponent)}`;
|
2016-11-12 15:49:07 +01:00
|
|
|
WithNew.WrappedComponent = WrappedComponent;
|
2016-11-25 10:58:32 +09:00
|
|
|
|
|
|
|
// hoist wrapped component's statics up to the new wrapper component and return it
|
2016-11-12 15:49:07 +01:00
|
|
|
return hoistStatics(WithNew, WrappedComponent);
|
2016-11-25 10:58:32 +09:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
withNew.propTypes = {
|
|
|
|
collection: React.PropTypes.object, // the collection to mutate
|
|
|
|
fragmentName: React.PropTypes.string, // the name of the fragment
|
|
|
|
fragment: React.PropTypes.object, // controls what data the mutation should return
|
|
|
|
queryName: React.PropTypes.string, // the query to update on the client
|
|
|
|
}
|