Vulcan/packages/vulcan-core/lib/modules/containers/withDelete.js

67 lines
1.6 KiB
JavaScript
Raw Normal View History

/*
Generic mutation wrapper to remove a document from a collection.
Sample mutation:
mutation deleteMovie($input: DeleteMovieInput) {
deleteMovie(input: $input) {
data {
_id
name
__typename
}
__typename
}
}
Arguments:
- input
- input.selector: the id of the document to remove
Child Props:
- deleteMovie({ selector })
*/
import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { getFragment, getFragmentName, getCollection, deleteClientTemplate } from 'meteor/vulcan:core';
const withDelete = (options) => {
const { collectionName } = options;
const collection = options.collection || getCollection(collectionName);
const fragment = options.fragment || getFragment(options.fragmentName || `${collectionName}DefaultFragment`);
const fragmentName = getFragmentName(fragment);
const typeName = collection.options.typeName;
const query = gql`${deleteClientTemplate({ typeName, fragmentName })}${fragment}`;
return graphql(query, {
2018-06-02 18:49:53 +09:00
alias: `withDelete${typeName}`,
props: ({ ownProps, mutate }) => ({
[`delete${typeName}`]: (args) => {
const { selector } = args;
2018-06-02 18:49:53 +09:00
return mutate({
variables: { input: { selector } }
2018-06-02 18:49:53 +09:00
});
},
2018-06-02 18:49:53 +09:00
// OpenCRUD backwards compatibility
removeMutation: (args) => {
const { documentId } = args;
const selector = { documentId };
return mutate({
variables: { input: { selector } }
});
},
}),
});
}
export default withDelete;