1111use Livewire \WithPagination ;
1212use Illuminate \Support \Facades \Auth ;
1313use Illuminate \Support \Facades \Log ;
14+ use App \Models \Friendship ;
15+ use App \Models \SocialPostHide ;
16+ use Illuminate \View \View ;
17+ use Illuminate \Contracts \Pagination \Paginator ;
18+ use App \Events \SocialPostUpdated ;
1419
1520class Feed extends Component
1621{
@@ -32,11 +37,56 @@ class Feed extends Component
3237
3338 public ?int $ deletingPostId = null ;
3439
35- public ?string $ fullscreenImageUrl = null ;
3640 public ?int $ activeCommentPostId = null ;
37- public string $ commentContent = '' ;
41+ public ?int $ fullscreenPostId = null ;
42+ public ?string $ fullscreenImageUrl = null ;
43+ public array $ commentContent = [];
44+
45+ public function getListeners (): array
46+ {
47+ return [
48+ 'loadMore ' => 'loadMore ' ,
49+ "echo:social-feed,SocialPostUpdated " => 'handlePostUpdated ' ,
50+ ];
51+ }
52+
53+ public function handlePostUpdated (array $ event ): void
54+ {
55+ // When a post is updated (like, comment, etc.), we refresh to get latest counts
56+ // Livewire will only re-render affected parts.
57+ }
3858
39- protected $ listeners = ['loadMore ' => 'loadMore ' ];
59+ protected function rules (): array
60+ {
61+ return [
62+ 'content ' => 'required|string|max:1000 ' ,
63+ 'image ' => 'nullable|image|max:10240 ' ,
64+
65+ 'editContent ' => 'required|string|max:1000 ' ,
66+ 'reportReason ' => 'required|string ' ,
67+ ];
68+ }
69+
70+ protected function validationAttributes (): array
71+ {
72+ return [
73+ 'content ' => 'conteúdo da postagem ' ,
74+ 'image ' => 'imagem ' ,
75+
76+ 'editContent ' => 'conteúdo editado ' ,
77+ 'reportReason ' => 'motivo da denúncia ' ,
78+ ];
79+ }
80+
81+ protected function messages (): array
82+ {
83+ return [
84+ 'content.required ' => 'O que você está pensando? Escreva algo! ' ,
85+ 'image.max ' => 'A imagem deve ter no máximo 10MB. ' ,
86+ 'editContent.required ' => 'O conteúdo não pode ficar vazio. ' ,
87+ 'reportReason.required ' => 'Por favor, selecione um motivo. ' ,
88+ ];
89+ }
4090
4191 public function loadMore (): void
4292 {
@@ -45,10 +95,11 @@ public function loadMore(): void
4595
4696 public function submitPost (SocialService $ socialService , FileService $ fileService ): void
4797 {
48- $ this ->validate ([
49- 'content ' => 'required|string|max:1000 ' ,
50- 'image ' => 'nullable|image|max:10240 ' , // 10MB
51- ]);
98+ if (empty ($ this ->content ) && empty ($ this ->image )) return ;
99+
100+ if ($ this ->image ) {
101+ $ this ->validateOnly ('image ' );
102+ }
52103
53104 try {
54105 $ post = $ socialService ->createPost ($ this ->content );
@@ -72,12 +123,13 @@ public function toggleLike(int $postId, SocialService $socialService): void
72123
73124 public function submitComment (int $ postId , SocialService $ socialService ): void
74125 {
75- if (empty ($ this ->commentContent )) return ;
126+ if (empty ($ this ->commentContent [ $ postId ] ?? '' )) return ;
76127
77- $ socialService ->addComment ($ postId , $ this ->commentContent );
78- $ this ->reset ('commentContent ' );
79- $ this ->activeCommentPostId = null ;
80- $ this ->dispatch ('toast ' , message: 'Comentário adicionado! ' , type: 'success ' );
128+ $ socialService ->addComment ($ postId , $ this ->commentContent [$ postId ]);
129+
130+ // Reset specific comment input
131+ unset($ this ->commentContent [$ postId ]);
132+ $ this ->dispatch ('toast ' , message: 'Incentivo enviado! ' , type: 'success ' );
81133 }
82134
83135 public function toggleComments (int $ postId ): void
@@ -100,9 +152,7 @@ public function deletePost(SocialService $socialService): void
100152
101153 public function reportPost (SocialService $ socialService ): void
102154 {
103- $ this ->validate ([
104- 'reportReason ' => 'required|string ' ,
105- ]);
155+ $ this ->validateOnly ('reportReason ' );
106156
107157 if ($ socialService ->reportPost ($ this ->reportingPostId , $ this ->reportReason , $ this ->reportDetails )) {
108158 $ this ->showReportSuccess = true ;
@@ -122,9 +172,7 @@ public function hidePost(int $postId, SocialService $socialService): void
122172
123173 public function updatePost (SocialService $ socialService ): void
124174 {
125- $ this ->validate ([
126- 'editContent ' => 'required|string|max:1000 ' ,
127- ]);
175+ $ this ->validateOnly ('editContent ' );
128176
129177 if ($ socialService ->editPost ($ this ->editingPostId , $ this ->editContent )) {
130178 $ this ->dispatch ('toast ' , message: 'Post atualizado! ' , type: 'success ' );
@@ -136,43 +184,14 @@ public function updatePost(SocialService $socialService): void
136184 $ this ->reset ('editContent ' );
137185 }
138186
139- public function render (SocialService $ socialService ): \ Illuminate \ View \ View
187+ public function render (SocialService $ socialService ): View
140188 {
141- $ userId = Auth::id ();
142189 $ user = Auth::user ();
143190
144- // Get feed items manually to support infinite scroll with perPage
145- // In a real app we might use pagination, but let's follow the React logic of "allPosts" list
146- $ feedData = $ socialService ->getFeed (); // This returns paginated results based on internal logic
147-
148- // Let's refactor slightly to use perPage directly
149- $ friendIds = \App \Models \Friendship::query ()
150- ->where (function ($ q ) use ($ userId ) {
151- $ q ->where ('user_id ' , $ userId )
152- ->orWhere ('friend_id ' , $ userId );
153- })
154- ->where ('status ' , 'accepted ' )
155- ->get ()
156- ->flatMap (fn ($ f ) => [$ f ->user_id , $ f ->friend_id ])
157- ->unique ()
158- ->toArray ();
159-
160- $ userIds = array_unique (array_merge ($ friendIds , [$ userId ]));
161- $ hiddenPostIds = \App \Models \SocialPostHide::where ('user_id ' , $ userId )->pluck ('post_id ' )->toArray ();
162-
163- $ posts = SocialPost::query ()
164- ->with (['user ' , 'files ' , 'comments.user ' ])
165- ->withCount (['likes ' , 'comments ' ])
166- ->withExists (['likes as is_liked ' => function ($ q ) use ($ userId ) {
167- $ q ->where ('user_id ' , $ userId );
168- }])
169- ->whereIn ('user_id ' , $ userIds )
170- ->whereNotIn ('id ' , $ hiddenPostIds )
171- ->latest ()
172- ->paginate ($ this ->perPage );
191+ $ feed = $ socialService ->getFeed ($ this ->perPage );
173192
174193 return view ('livewire.social.feed ' , [
175- 'posts ' => $ posts ,
194+ 'posts ' => $ feed [ ' paginator ' ] ,
176195 'suggestions ' => $ socialService ->getSuggestions (),
177196 'isAdmin ' => $ user ->hasAnyRole (['Administrador ' , 'Suporte ' ]),
178197 ]);
0 commit comments