Skip to content

Commit 6560a1c

Browse files
committed
order change option added
1 parent 9a1960f commit 6560a1c

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

assets/src/js/frontend/learning-area/lesson/comments.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const lessonComments = (lessonId?: number) => {
2222
currentPage: 1,
2323
loading: false,
2424
hasMore: true,
25+
currentOrder: 'DESC' as 'ASC' | 'DESC',
26+
isReloading: false,
2527
$el: null as unknown as HTMLElement,
2628
$refs: {} as {
2729
commentList: HTMLElement;
@@ -33,6 +35,11 @@ const lessonComments = (lessonId?: number) => {
3335
replyCommentMutation: null as MutationState<unknown, ReplyCommentPayload> | null,
3436

3537
init() {
38+
// Initialize order from URL.
39+
const url = new URL(window.location.href);
40+
const orderParam = url.searchParams.get('order');
41+
this.currentOrder = orderParam === 'ASC' ? 'ASC' : 'DESC';
42+
3643
this.initInfiniteScroll();
3744

3845
// Lesson comment create mutation.
@@ -95,6 +102,44 @@ const lessonComments = (lessonId?: number) => {
95102
return wpAjaxInstance.post(endpoints.REPLY_LESSON_COMMENT, payload);
96103
},
97104

105+
handleChangeOrder(newOrder: 'ASC' | 'DESC') {
106+
if (this.currentOrder === newOrder) return;
107+
108+
this.currentOrder = newOrder;
109+
this.updateURL(newOrder);
110+
this.reloadComments();
111+
},
112+
113+
updateURL(order: 'ASC' | 'DESC') {
114+
const url = new URL(window.location.href);
115+
url.searchParams.set('order', order);
116+
window.history.pushState({}, '', url);
117+
},
118+
119+
reloadComments() {
120+
this.isReloading = true;
121+
this.currentPage = 1;
122+
this.hasMore = true;
123+
124+
wpAjaxInstance
125+
.post(endpoints.LOAD_LESSON_COMMENTS, {
126+
lesson_id: this.lessonId,
127+
current_page: 1,
128+
order: this.currentOrder,
129+
})
130+
.then((response) => {
131+
// Replace entire comment list.
132+
this.$refs.commentItems.innerHTML = response.data.html;
133+
this.hasMore = response.data.has_more;
134+
})
135+
.catch((error) => {
136+
window.TutorCore.toast.error(convertToErrorMessage(error));
137+
})
138+
.finally(() => {
139+
this.isReloading = false;
140+
});
141+
},
142+
98143
loadNextPage() {
99144
if (!this.lessonId || this.loading || !this.hasMore) {
100145
return;
@@ -106,6 +151,7 @@ const lessonComments = (lessonId?: number) => {
106151
.post(endpoints.LOAD_LESSON_COMMENTS, {
107152
lesson_id: this.lessonId,
108153
current_page: this.currentPage + 1,
154+
order: this.currentOrder,
109155
})
110156
.then((response) => {
111157
if (response.data.has_more !== undefined) {

classes/Lesson.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ public function render_single_content( $lesson ): void {
858858
public function load_lesson_comments() {
859859
$lesson_id = Input::post( 'lesson_id' ?? 0 );
860860
$current_page = Input::post( 'current_page', '1' );
861+
$order = QueryHelper::get_valid_sort_order( Input::post( 'order', 'DESC' ) );
861862

862863
$item_per_page = tutor_utils()->get_option( 'pagination_per_page', 10 );
863864

@@ -867,6 +868,7 @@ public function load_lesson_comments() {
867868
'parent' => 0,
868869
'paged' => $current_page,
869870
'number' => $item_per_page,
871+
'order' => $order,
870872
)
871873
);
872874

templates/learning-area/lesson/comment-items.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
return;
2424
}
2525
?>
26-
<div x-ref="commentItems" class="tutor-comments-list tutor-border-t">
27-
<?php foreach ( $comment_list as $comment_item ) : ?>
26+
<?php foreach ( $comment_list as $comment_item ) : ?>
2827
<div class="tutor-comment" x-data="{showReplyForm: false, repliesExpanded: false}">
2928
<?php Avatar::make()->user( $comment_item->user_id )->size( Size::SIZE_40 )->render(); ?>
3029
<div class="tutor-flex-1">
@@ -54,7 +53,7 @@ class="tutor-comment-reply-form tutor-mt-6"
5453
x-collapse
5554
x-data="{ ...tutorForm({ id: 'lesson-comment-reply-form' }), focused: false }"
5655
x-bind="getFormBindings()"
57-
@submit.prevent="handleSubmit((data) => replyCommentMutation?.mutate({ ...data, comment_post_ID: <?php echo esc_html( $lesson_id ); ?>, comment_parent: <?php echo esc_html( $comment_item->comment_ID ); ?> }))($event)"
56+
@submit.prevent="handleSubmit((data) => replyCommentMutation?.mutate({ ...data, comment_post_ID: <?php echo esc_html( $lesson_id ); ?>, comment_parent: <?php echo esc_html( $comment_item->comment_ID ); ?>, order: currentOrder }))($event)"
5857
>
5958
<?php
6059
InputField::make()
@@ -109,5 +108,4 @@ class="tutor-comment-reply-form tutor-mt-6"
109108
?>
110109
</div>
111110
</div>
112-
<?php endforeach; ?>
113-
</div>
111+
<?php endforeach; ?>

templates/learning-area/lesson/comment-list.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
$item_per_page = tutor_utils()->get_option( 'pagination_per_page', 10 );
1919
$current_page = max( 1, Input::post( 'current_page', 0, Input::TYPE_INT ) );
2020
$lesson_id = Input::post( 'comment_post_ID', get_the_ID(), Input::TYPE_INT );
21+
$order_filter = Input::get( 'order' ) ?? Input::post( 'order' ) ?? 'DESC';
2122

2223
$comments_list_args = array(
2324
'post_id' => $lesson_id,
2425
'parent' => 0,
2526
'paged' => $current_page,
2627
'number' => $item_per_page,
28+
'order' => $order_filter,
2729
);
2830

2931
$comment_count_args = array(
@@ -37,13 +39,24 @@
3739
?>
3840
<?php if ( ! empty( $comment_list ) ) : ?>
3941
<div x-ref="commentList">
40-
<div class="tutor-flex tutor-items-center tutor-justify-between tutor-px-6 tutor-py-5 tutor-border-t">
42+
<div
43+
class="tutor-flex tutor-items-center tutor-justify-between tutor-px-6 tutor-py-5 tutor-border-t"
44+
:class="{ 'tutor-loading-spinner': isReloading }"
45+
>
4146
<div class="tutor-small tutor-text-secondary">
4247
<?php esc_html_e( 'Comments', 'tutor' ); ?>
4348
<span class="tutor-text-primary tutor-font-medium">(<?php echo esc_html( $comments_count ); ?>)</span>
4449
</div>
45-
<?php Sorting::make()->order( Input::get( 'order', 'DESC' ) )->render(); ?>
50+
<?php
51+
Sorting::make()
52+
->order( $order_filter )
53+
->on_change( 'handleChangeOrder' )
54+
->bind_active_order( 'currentOrder' )
55+
->render();
56+
?>
57+
</div>
58+
<div x-ref="commentItems" class="tutor-comments-list tutor-border-t">
59+
<?php tutor_load_template( 'learning-area.lesson.comment-items', compact( 'comment_list', 'lesson_id' ) ); ?>
4660
</div>
47-
<?php tutor_load_template( 'learning-area.lesson.comment-items', compact( 'comment_list', 'lesson_id' ) ); ?>
4861
</div>
4962
<?php endif; ?>

templates/learning-area/lesson/comments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class="tutor-p-6"
2525
x-data="{ ...tutorForm({ id: 'lesson-comment-form' }), focused: false }"
2626
x-bind="getFormBindings()"
27-
@submit.prevent="handleSubmit((data) => createCommentMutation?.mutate({ ...data, comment_post_ID: <?php echo esc_html( $lesson_id ); ?>, comment_parent: 0 }))($event)"
27+
@submit.prevent="handleSubmit((data) => createCommentMutation?.mutate({ ...data, comment_post_ID: <?php echo esc_html( $lesson_id ); ?>, comment_parent: 0, order: currentOrder }))($event)"
2828
>
2929
<div class="tutor-text-medium tutor-font-semibold tutor-mb-4">
3030
<?php esc_html_e( 'Join The Conversation', 'tutor' ); ?>

0 commit comments

Comments
 (0)