Skip to content

Support flagging comments using Markable#60

Merged
csebianlander merged 1 commit intomainfrom
comment-flagging
Jul 21, 2025
Merged

Support flagging comments using Markable#60
csebianlander merged 1 commit intomainfrom
comment-flagging

Conversation

@wrose504
Copy link
Collaborator

Comment flagging is not working properly at present for a few reasons:

  • it's not possible with the laravel-markable trait to count all flags irrespective of reason (aka value), so we have to implement this ourselves.
  • likewise, maintaining a single flag per comment per user is tricky when we may have multiple sessions open across tabs.
  • the server round-trip required to render the form and to show the note text area when flagging with notes leads to awkward UI pauses where it is not clear that actions have worked.
  • there are form layout issues.

This PR tries to fix several of these issues:

  • implement custom flag count, add and delete that preserve the "one flag per user" requirement.
  • manage more state client-side so we can quickly show/hide the note text area, and dismiss the form after clicking an action button.
  • add a loading state to the flag button so when clicked, or while updating after form submission, we see some indication that activity is occurring.
  • fix form layout issues.

I mostly tested this on a branch with #51, #52 and #59 all merged, so it would be better to merge those first then resolve the conflicts with this branch.

Before

as-is behavior

After

updated behavior

@wrose504 wrose504 requested review from Copilot and kirkaracha June 29, 2025 02:05
@wrose504 wrose504 self-assigned this Jun 29, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves how comment flagging is handled by implementing custom flag counting and state management to ensure that only one flag per user exists while enhancing the UI responsiveness and form layout. Key changes include:

  • Refactored flag components with client-side state management and loading indicators.
  • Updated model methods for accurate flag counting and user flag detection.
  • Improved styling for buttons and flag forms to address layout and icon display issues.

Reviewed Changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
resources/views/livewire/flags/flag-component.blade.php Refactored the flag form with enhanced identifiers, loading state handling, and event-based UI interactions.
resources/views/livewire/comments/partials/toggle-flagging-button.blade.php Updated the toggle button to include loading state and revised icon usage.
resources/views/livewire/comments/comment-show-component.blade.php Adjusted flag removal logic and corrected markup structure.
resources/views/livewire/comments/comment-component.blade.php Streamlined flag state updates by removing redundant properties and using updated methods for flag counts.
resources/sass/modules/_posts.scss Enhanced button icon display based on loading state.
resources/sass/modules/_forms.scss Revised form styling with flexbox for radio labels and updated textarea resize rules.
app/Traits/CommentComponentTrait.php Added flag loading state resets in flag start/stop methods.
app/Models/Post.php Implemented custom flag counting and user flag detection methods.
app/Models/Comment.php Renamed the flag count method and added a user flag check.
app/Livewire/Flags/FlagComponent.php Refactored flag logic with locked properties, improved state management, and event dispatch enhancements.
app/Livewire/Comments/CommentComponent.php Optimized flag update logic and removed obsolete flag handling methods.
app/Enums/LivewireEventEnum.php Added new events for flag cancellation in both comment and post contexts.
Comments suppressed due to low confidence (1)

resources/views/livewire/flags/flag-component.blade.php:32

  • Consider using a sanitized identifier (such as the loop index or a slugified version) instead of the raw '$reason' value for the 'id' attribute to ensure uniqueness and valid HTML.
            <label for="{{ $type }}-{{ $model->id }}-flag-note-{{ $reason }}" class="optional">

name="selectedReason"
wire:model="selectedReason"
wire:change="flagReasonSelected('{{ $reason }}')"
wire:click="showNoteField = {{ json_encode($this->isNoteVisibleForReason($reason)) }}"
Copy link

Copilot AI Jun 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a dedicated method call to toggle the note field visibility instead of an inline assignment to improve readability and testability.

Suggested change
wire:click="showNoteField = {{ json_encode($this->isNoteVisibleForReason($reason)) }}"
wire:click="toggleNoteField('{{ $reason }}')"

Copilot uses AI. Check for mistakes.
}

.flag-form textarea.flag-note {
resize: block;
Copy link

Copilot AI Jun 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CSS property value 'resize: block;' is invalid; consider using a valid value like 'vertical' or 'none' based on the intended behavior.

Suggested change
resize: block;
resize: vertical;

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this was wrong, but MDN was calling out support for block separately to the other resize options, so I suppose vertical might be more widely supported. I don't see any notes on this on caniuse.com either, so 🤷

@wrose504 wrose504 force-pushed the comment-flagging branch 4 times, most recently from 1fafed8 to 12ab4fb Compare June 29, 2025 15:44
Comment on lines +98 to 113
public function flagCount(): int
{
return Flag::count($this);
return Flag::where([
'markable_id' => $this->getKey(),
'markable_type' => $this->getMorphClass(),
])->count();
}

public function userFlagged(): bool
{
return Flag::where([
'user_id' => auth()->id(),
'markable_id' => $this->getKey(),
'markable_type' => $this->getMorphClass(),
])->exists();
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could try to move these methods more generically into a trait as countAll and userMarked or something (or a dynamic name). I think the downside is we wouldn't be able to use the Bookmark and Favorite marks directly from laravel-markable, we'd probably have to create a subclass.

@wrose504 wrose504 force-pushed the comment-flagging branch 3 times, most recently from 6a0c6b0 to 1ca3676 Compare July 5, 2025 17:09
@wrose504 wrose504 force-pushed the comment-flagging branch from 1ca3676 to 777ba62 Compare July 9, 2025 20:20
@csebianlander csebianlander merged commit 09d1d9e into main Jul 21, 2025
1 check passed
@wrose504 wrose504 deleted the comment-flagging branch July 24, 2025 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants