@kiwicom/smart-faq
Version:
61 lines (53 loc) • 1.18 kB
JavaScript
// @flow
import { commitMutation, graphql } from 'react-relay';
import createEnvironment from '../../shared/relay/environment';
const mutation = graphql`
mutation CreateCommentMutation(
$articleId: Int!
$type: FAQCommentType!
$comment: String!
) {
addFAQArticleComment(
originalId: $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;