@kiwicom/smart-faq
Version:
Smart FAQ
62 lines (55 loc) • 1.42 kB
JavaScript
// @flow
import { commitMutation, graphql } from 'react-relay';
import idx from 'idx';
import createEnvironment from '../../shared/relay/environment';
import { simpleTracker } from '../../shared/helpers/analytics/trackers';
import { track } from '../../shared/cuckoo/tracker';
const mutation = graphql`
mutation CreateCommentMutation(
$articleId: ID!
$type: FAQCommentType!
$comment: String!
) {
_addFAQArticleComment(id: $articleId, type: $type, comment: $comment) {
isRateLimitExhausted
}
}
`;
export default (
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 = idx(
response,
_ => _._addFAQArticleComment.isRateLimitExhausted,
);
if (!errors && !isCommentLimitReached) {
callback();
return;
}
if (isCommentLimitReached) {
simpleTracker('smartFAQCategories', { action: 'commentLimitReached' });
track('FAQs', 'commentLimitReached');
commentLimitReachedCallback();
} else {
errorCallback();
}
},
onError: () => {
errorCallback();
},
});
};