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-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
|
|
|
|
|
|
|
class WithNew extends Component {
|
2016-11-22 16:15:00 +09:00
|
|
|
// constructor(...args) {
|
|
|
|
// super(...args);
|
|
|
|
// }
|
2016-11-12 15:49:07 +01:00
|
|
|
|
|
|
|
render() {
|
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
const collection = this.props.collection,
|
|
|
|
collectionName = collection._name,
|
|
|
|
mutationName = collection.options.mutations.new.name,
|
|
|
|
fragmentName = collection.options.fragments.single.name,
|
|
|
|
fragment = collection.options.fragments.single.fragment
|
2016-11-12 15:49:07 +01:00
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
const ComponentWithNew = graphql(gql`
|
2016-11-12 15:49:07 +01:00
|
|
|
mutation ${mutationName}($document: ${collectionName}Input) {
|
|
|
|
${mutationName}(document: $document) {
|
2016-11-22 16:15:00 +09:00
|
|
|
...${fragmentName}
|
2016-11-12 15:49:07 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 16:15:00 +09:00
|
|
|
${fragment}
|
2016-11-12 15:49:07 +01:00
|
|
|
`, {
|
|
|
|
props: ({ownProps, mutate}) => ({
|
2016-11-22 16:15:00 +09:00
|
|
|
newMutation: ({document}) => {
|
2016-11-12 15:49:07 +01:00
|
|
|
return mutate({
|
2016-11-22 16:15:00 +09:00
|
|
|
variables: { document },
|
2016-11-12 15:49:07 +01:00
|
|
|
updateQueries: ownProps.updateQueries
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
})(WrappedComponent);
|
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
return <ComponentWithNew {...this.props} />
|
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;
|
|
|
|
|
|
|
|
return hoistStatics(WithNew, WrappedComponent);
|
|
|
|
};
|