fetchComments()

in static/src/javascripts/projects/common/modules/discussion/comments.js [147:216]


    fetchComments(options = {}) {
        const { discussionId } = this.options || {};
        const url = `/discussion/${
            options.comment
                ? `comment-context/${options.comment}`
                : discussionId
        }.json`;
        let orderBy = options.order || (this.options && this.options.order);
        let promise;
        const ajaxParams = { mode: 'cors' };

        if (orderBy === 'recommendations') {
            orderBy = 'mostRecommended';
        }

        const queryParams = {
            orderBy,
            pageSize:
                options.pagesize || (this.options && this.options.pagesize),
            displayThreaded: !!(
                this.options && this.options.threading !== 'unthreaded'
            ),
            commentsClosed: options.commentsClosed,
        };

        if (options.page) {
            queryParams.page = options.page;
        }

        if (
            !options.comment &&
            this.options &&
            this.options.threading === 'collapsed'
        ) {
            queryParams.maxResponses = 3;
        }

        if (this.isAllPageSizeActive()) {
            promise = new WholeDiscussion({
                discussionId: this.options && this.options.discussionId,
                orderBy: queryParams.orderBy,
                displayThreaded: queryParams.displayThreaded,
                maxResponses: queryParams.maxResponses,
                commentsClosed: queryParams.commentsClosed,
            })
                .loadAllComments()
                .catch(() => {
                    this.wholeDiscussionErrors = true;
                    queryParams.pageSize = 100;

                    return fetchJson(
                        `${url}?${constructQuery(queryParams)}`,
                        ajaxParams
                    );
                });
        } else {
            // It is possible that the user has chosen to view all comments,
            // but the WholeDiscussion module has failed. Fall back to 100 comments.
            if (queryParams.pageSize === 'All') {
                queryParams.pageSize = 100;
            }

            promise = fetchJson(
                `${url}?${constructQuery(queryParams)}`,
                ajaxParams
            );
        }

        return promise.then(resp => this.renderComments(resp));
    }