Fix page count increasing when refreshing comments, disable "Submit" button until comments are refreshed

This commit is contained in:
jacob1
2024-05-16 00:02:01 -04:00
parent bb51daa05f
commit 080f059da9
4 changed files with 35 additions and 14 deletions

View File

@@ -126,10 +126,15 @@ bool PreviewController::PrevCommentPage()
return false; return false;
} }
void PreviewController::RefreshComments()
{
previewModel->UpdateComments(1);
}
void PreviewController::CommentAdded() void PreviewController::CommentAdded()
{ {
previewModel->CommentAdded(); previewModel->CommentAdded();
previewModel->UpdateComments(1); RefreshComments();
} }
void PreviewController::Exit() void PreviewController::Exit()

View File

@@ -35,6 +35,7 @@ public:
bool NextCommentPage(); bool NextCommentPage();
bool PrevCommentPage(); bool PrevCommentPage();
void RefreshComments();
void CommentAdded(); void CommentAdded();
virtual ~PreviewController(); virtual ~PreviewController();

View File

@@ -435,7 +435,8 @@ void PreviewView::OnTick(float dt)
{ {
new ErrorMessage("Error submitting comment", ByteString(ex.what()).FromUtf8()); new ErrorMessage("Error submitting comment", ByteString(ex.what()).FromUtf8());
} }
submitCommentButton->Enabled = true; isSubmittingComment = false;
CheckCommentSubmitEnabled();
commentBoxAutoHeight(); commentBoxAutoHeight();
addCommentRequest.reset(); addCommentRequest.reset();
CheckComment(); CheckComment();
@@ -603,25 +604,34 @@ void PreviewView::submitComment()
String comment = addCommentBox->GetText(); String comment = addCommentBox->GetText();
if (comment.length() == 0) if (comment.length() == 0)
{ {
c->CommentAdded(); c->RefreshComments();
return; isRefreshingComments = true;
} }
if (comment.length() < 4) else if (comment.length() < 4)
{ {
new ErrorMessage("Error", "Comment is too short"); new ErrorMessage("Error", "Comment is too short");
return; }
else
{
isSubmittingComment = true;
FocusComponent(NULL);
addCommentRequest = std::make_unique<http::AddCommentRequest>(c->SaveID(), comment);
addCommentRequest->Start();
CheckComment();
} }
submitCommentButton->Enabled = false; CheckCommentSubmitEnabled();
FocusComponent(NULL);
addCommentRequest = std::make_unique<http::AddCommentRequest>(c->SaveID(), comment);
addCommentRequest->Start();
CheckComment();
} }
} }
void PreviewView::CheckCommentSubmitEnabled()
{
if (submitCommentButton)
submitCommentButton->Enabled = !isRefreshingComments && !isSubmittingComment;
}
void PreviewView::NotifyCommentBoxEnabledChanged(PreviewModel * sender) void PreviewView::NotifyCommentBoxEnabledChanged(PreviewModel * sender)
{ {
if(addCommentBox) if(addCommentBox)
@@ -654,7 +664,6 @@ void PreviewView::NotifyCommentBoxEnabledChanged(PreviewModel * sender)
AddComponent(addCommentBox); AddComponent(addCommentBox);
submitCommentButton = new ui::Button(ui::Point(Size.X-40, Size.Y-19), ui::Point(40, 19), "Submit"); submitCommentButton = new ui::Button(ui::Point(Size.X-40, Size.Y-19), ui::Point(40, 19), "Submit");
submitCommentButton->SetActionCallback({ [this] { submitComment(); } }); submitCommentButton->SetActionCallback({ [this] { submitComment(); } });
//submitCommentButton->Enabled = false;
AddComponent(submitCommentButton); AddComponent(submitCommentButton);
commentWarningLabel = new ui::Label(ui::Point((XRES/2)+4, Size.Y-19), ui::Point(Size.X-(XRES/2)-48, 16), "If you see this it is a bug"); commentWarningLabel = new ui::Label(ui::Point((XRES/2)+4, Size.Y-19), ui::Point(Size.X-(XRES/2)-48, 16), "If you see this it is a bug");
@@ -696,6 +705,9 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender)
commentTextComponents.clear(); commentTextComponents.clear();
commentsPanel->InnerSize = ui::Point(0, 0); commentsPanel->InnerSize = ui::Point(0, 0);
isRefreshingComments = false;
CheckCommentSubmitEnabled();
if (commentsPtr) if (commentsPtr)
{ {
auto &comments = *commentsPtr; auto &comments = *commentsPtr;

View File

@@ -59,6 +59,8 @@ class PreviewView: public ui::Window
String doErrorMessage; String doErrorMessage;
bool showAvatars; bool showAvatars;
bool prevPage; bool prevPage;
bool isSubmittingComment = false;
bool isRefreshingComments = false;
int commentBoxHeight; int commentBoxHeight;
float commentBoxPositionX; float commentBoxPositionX;
@@ -72,6 +74,7 @@ class PreviewView: public ui::Window
void displayComments(); void displayComments();
void commentBoxAutoHeight(); void commentBoxAutoHeight();
void submitComment(); void submitComment();
void CheckCommentSubmitEnabled();
bool CheckSwearing(String text); bool CheckSwearing(String text);
void CheckComment(); void CheckComment();
void ShowMissingCustomElements(); void ShowMissingCustomElements();