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'
|
|
|
|
import { getDisplayName } from './utils';
|
2016-11-12 17:28:08 +01:00
|
|
|
import withRemove from './withRemove.jsx'
|
2016-11-12 15:49:07 +01:00
|
|
|
|
|
|
|
export default function withEdit(WrappedComponent, options) {
|
|
|
|
|
|
|
|
class WithEdit extends Component {
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { mutationName, fragment, resultQuery, collection } = this.props
|
|
|
|
|
|
|
|
// if the mutation given to NovaForm isn't about editing an existing document, do nothing
|
|
|
|
if (mutationName.indexOf('Edit') === -1) {
|
|
|
|
|
|
|
|
return <WrappedComponent {...this.props} />
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const collectionName = collection._name;
|
|
|
|
|
2016-11-13 14:12:15 +01:00
|
|
|
const ComponentWithEdit = graphql(gql`
|
2016-11-12 15:49:07 +01:00
|
|
|
mutation ${mutationName}($documentId: String, $set: ${collectionName}Input, $unset: ${collectionName}Unset) {
|
|
|
|
${mutationName}(documentId: $documentId, set: $set, unset: $unset) {
|
|
|
|
${fragment ? `...${fragment[0].name.value}` : resultQuery}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, {
|
|
|
|
options: (props) => props.fragment ? {fragments: props.fragment} : {},
|
|
|
|
props: ({ownProps, mutate}) => ({
|
|
|
|
mutation: ({documentId, set, unset}) => {
|
|
|
|
return mutate({
|
|
|
|
variables: {documentId: documentId, set, unset}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
})(WrappedComponent);
|
|
|
|
|
2016-11-13 14:12:15 +01:00
|
|
|
// add the remove mutation by default unless it's explicitly specified not to do it
|
|
|
|
const ComponentToRender = this.props.noRemoveMutation ? ComponentWithEdit : withRemove(ComponentWithEdit);
|
|
|
|
|
|
|
|
return <ComponentToRender {...this.props} />
|
2016-11-12 15:49:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
WithEdit.displayName = `withEdit(${getDisplayName(WrappedComponent)}`;
|
|
|
|
WithEdit.WrappedComponent = WrappedComponent;
|
|
|
|
|
|
|
|
return hoistStatics(WithEdit, WrappedComponent);
|
|
|
|
};
|