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

57 lines
1.2 KiB
JavaScript
Raw Normal View History

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