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 16:15:00 +09:00
|
|
|
// import withRemove from './withRemove.jsx'
|
2016-11-12 15:49:07 +01:00
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
export default function withEdit(WrappedComponent) {
|
2016-11-12 15:49:07 +01:00
|
|
|
|
|
|
|
class WithEdit 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-17 09:46:35 +01:00
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
const collection = this.props.collection,
|
|
|
|
collectionName = collection._name,
|
|
|
|
mutationName = collection.options.mutations.edit.name,
|
|
|
|
fragmentName = collection.options.fragments.single.name,
|
|
|
|
fragment = collection.options.fragments.single.fragment
|
|
|
|
|
|
|
|
const ComponentWithEdit = graphql(gql`
|
|
|
|
mutation ${mutationName}($documentId: String, $set: ${collectionName}Input, $unset: ${collectionName}Unset) {
|
|
|
|
${mutationName}(documentId: $documentId, set: $set, unset: $unset) {
|
|
|
|
...${fragmentName}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${fragment}
|
|
|
|
`, {
|
|
|
|
props: ({ ownProps, mutate }) => ({
|
|
|
|
editMutation: (args) => {
|
|
|
|
const { documentId, set, unset } = args;
|
|
|
|
return mutate({
|
|
|
|
variables: { documentId, set, unset }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
})(WrappedComponent);
|
2016-11-12 15:49:07 +01:00
|
|
|
|
2016-11-22 16:15:00 +09:00
|
|
|
return <ComponentWithEdit {...this.props} />
|
2016-11-12 15:49:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-15 18:33:16 +01:00
|
|
|
WithEdit.displayName = `withEdit(${Telescope.utils.getComponentDisplayName(WrappedComponent)}`;
|
2016-11-12 15:49:07 +01:00
|
|
|
WithEdit.WrappedComponent = WrappedComponent;
|
|
|
|
|
|
|
|
return hoistStatics(WithEdit, WrappedComponent);
|
|
|
|
};
|