@kiwicom/smart-faq
Version:
57 lines (49 loc) • 1.15 kB
JavaScript
// @flow
import { commitMutation, graphql } from 'react-relay';
import createEnvironment from '../../shared/relay/environment';
const mutation = graphql`
mutation CreateCommentMutation(
$articleId: ID!
$type: FAQCommentType!
$comment: String!
) {
_addFAQArticleComment(id: $articleId, type: $type, comment: $comment) {
isRateLimitExhausted
}
}
`;
const createComment = (
articleId: string,
type: string,
comment: string,
callback: () => void,
commentLimitReachedCallback: () => void,
errorCallback: () => void,
) => {
const variables = {
articleId,
type,
comment,
};
commitMutation(createEnvironment(), {
mutation,
variables,
onCompleted: (response, errors) => {
const isCommentLimitReached =
response?._addFAQArticleComment?.isRateLimitExhausted;
if (!errors && !isCommentLimitReached) {
callback();
return;
}
if (isCommentLimitReached) {
commentLimitReachedCallback();
} else {
errorCallback();
}
},
onError: () => {
errorCallback();
},
});
};
export default createComment;