postComment()

in static/src/javascripts/projects/common/modules/discussion/comment-box.js [234:349]


    postComment() {
        const commentBody = this.getElem('body');
        const { value } =
        (commentBody &&
            commentBody instanceof HTMLTextAreaElement &&
            commentBody) ||
        {};

        const comment = {
            body: value,
        };

        this.clearErrors();

        const postCommentToDAPI = () => {
            this.removeState('onboarding-visible');
            comment.body = urlify(comment.body);
            this.setFormState(true);

            return postComment(this.getDiscussionId(), comment)
                .then((resp) => resp.status === 'ok' ? this.postCommentSuccess(comment, resp) : this.fail(resp))
                .catch((err) => console.error(`Post Comment to DAPI failed`, err));
        };

        const updateUsernameSuccess = (resp) => {
            const onbordingUsername = this.getElem('onboarding-username');

            mediator.emit(
                'user:username:updated',
                resp.user.publicFields.username
            );

            this.options.hasUsername = true;

            if (onbordingUsername) {
                onbordingUsername.classList.add('is-hidden');
            }

            postCommentToDAPI();
        };

        const updateUsernameFailure = (errorResponse) => {
            const usernameField = this.getElem('onboarding-username-input');
            const errorMessage = this.getElem(
                'onboarding-username-error-message'
            );

            this.setState('onboarding-visible');

            if (usernameField) {
                usernameField.classList.add(
                    'd-comment-box__onboarding-username-error-border'
                );
            }

            // TODO: this should be wrapped into a try-catch block
            if (errorMessage) {
                errorMessage.innerHTML = JSON.parse(
                    errorResponse.responseText
                ).errors[0].description;

                errorMessage.classList.remove('is-hidden');
            }
        };

        const validEmailCommentSubmission = () => {
            if (comment.body === '') {
                this.error('EMPTY_COMMENT_BODY');
            }

            if (comment.body.length > this.options.maxLength) {
                this.error(
                    'COMMENT_TOO_LONG',
                    `<b>Comments must be shorter than ${this.options.maxLength
                    } characters.</b>` +
                    `Yours is currently ${comment.body.length -
                    this.options.maxLength} character(s) too long.`
                );
            }

            if (this.options.replyTo) {
                comment.replyTo = this.options.replyTo;
            }

            if (this.errors.length === 0) {
                if (this.options.newCommenter && !this.options.hasUsername) {
                    const userNameInput = ((this.getElem(
                        'onboarding-username-input'
                    )));
                    return updateUsername(userNameInput.value).then(
                        updateUsernameSuccess,
                        updateUsernameFailure
                    );
                }
                return postCommentToDAPI();
            }

            return Promise.resolve();
        };

        if (!this.getUserData().statusFields.userEmailValidated) {
            // Cookie could be stale so lets check from the api
            const createdDate = new Date(this.getUserData().dates.accountCreatedDate);

            if (createdDate > this.options.priorToVerificationDate) {
                return getUserData().then(user => {
                    if (user.statusFields.userEmailValidated) {
                        return validEmailCommentSubmission();
                    }
                    this.invalidEmailError();
                })
            }
            return validEmailCommentSubmission();
        }
        return validEmailCommentSubmission();
    }