Skip to content

Commit defd505

Browse files
committed
end creating adminChat on BroadcastingChannel
1 parent 8bab690 commit defd505

File tree

8 files changed

+88
-28
lines changed

8 files changed

+88
-28
lines changed

app/Events/PostUpdated.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use App\Post;
66
use Illuminate\Broadcasting\InteractsWithSockets;
7-
use Illuminate\Broadcasting\PrivateChannel;
8-
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
97
use Illuminate\Foundation\Events\Dispatchable;
108
use Illuminate\Queue\SerializesModels;
119

12-
class PostUpdated implements ShouldBroadcast
10+
class PostUpdated
1311
{
1412
use Dispatchable, InteractsWithSockets, SerializesModels;
1513

@@ -19,19 +17,4 @@ public function __construct(Post $post)
1917
{
2018
$this->post = $post;
2119
}
22-
23-
/**
24-
* Get the channels the event should broadcast on.
25-
*
26-
* @return \Illuminate\Broadcasting\Channel|\Illuminate\Broadcasting\Channel[]
27-
*/
28-
public function broadcastOn()
29-
{
30-
return new PrivateChannel('admin-chat-channel');
31-
}
32-
33-
public function broadcastWith()
34-
{
35-
return ['hi' => 'hello', 'changes' => $this->post->getChanges(), 'origin' => $this->post->getOriginal()];
36-
}
3720
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Broadcasting\PrivateChannel;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class PostUpdatedAdminChat implements ShouldBroadcast
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public $post;
16+
public $data;
17+
18+
public function __construct($post, $data)
19+
{
20+
$this->post = $post;
21+
$this->data = $data;
22+
}
23+
24+
public function broadcastOn()
25+
{
26+
return new PrivateChannel('admin-chat-channel');
27+
}
28+
}

app/Listeners/AddHistoryOnUpdatePost.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Listeners;
44

55
use App\Events\PostUpdated;
6+
use App\Events\PostUpdatedAdminChat;
67

78
class AddHistoryOnUpdatePost
89
{
@@ -33,5 +34,7 @@ public function handle(PostUpdated $event)
3334
];
3435

3536
$event->post->history()->create($values);
37+
38+
event(new PostUpdatedAdminChat($event->post, $result));
3639
}
3740
}

public/css/app.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10922,3 +10922,9 @@ a.text-dark:focus {
1092210922
overflow: auto;
1092310923
}
1092410924

10925+
.message-link {
10926+
color: white;
10927+
font-weight: bold;
10928+
text-decoration: underline;
10929+
}
10930+

public/js/app.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76149,16 +76149,31 @@ var AdminChat = /*#__PURE__*/function (_React$Component) {
7614976149

7615076150
_this = _super.call(this, props);
7615176151
_this.state = {
76152-
postUpdateMessage: ''
76152+
message: ''
7615376153
};
7615476154
return _this;
7615576155
}
7615676156

7615776157
_createClass(AdminChat, [{
76158+
key: "addMessage",
76159+
value: function addMessage(data) {
76160+
this.setState({
76161+
message: this.state.message + data
76162+
});
76163+
var chat = document.querySelector('.admin-chat__message-block');
76164+
chat.innerHTML = this.state.message;
76165+
}
76166+
}, {
7615876167
key: "componentDidMount",
7615976168
value: function componentDidMount() {
76160-
Echo["private"]('admin-chat-channel').listen('PostUpdated', function (data) {
76161-
console.log(data);
76169+
var _this2 = this;
76170+
76171+
Echo["private"]('admin-chat-channel').listen('PostUpdatedAdminChat', function (event) {
76172+
var time = new Date(event.post.updated_at).toTimeString();
76173+
time = time.slice(0, time.indexOf(' '));
76174+
var result = "<div class=\"message\">\n <time>".concat(time, "</time>\n <p>\u0411\u044B\u043B\u0430 \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0430 \n <a class=\"message-link\" href=\"/posts/").concat(event.post.id, "\">\u0441\u0442\u0430\u0442\u044C\u044F #").concat(event.post.id, "</a>\n </p>\n \n <p>").concat(event.data, "</p>\n </div>");
76175+
76176+
_this2.addMessage(result);
7616276177
});
7616376178
}
7616476179
}, {
@@ -76175,7 +76190,7 @@ var AdminChat = /*#__PURE__*/function (_React$Component) {
7617576190
className: "admin-chat_header text-center"
7617676191
}, "\u0421\u043E\u043E\u0431\u0449\u0435\u043D\u0438\u044F"), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("div", {
7617776192
className: "admin-chat__message-block"
76178-
}, this.state.postUpdateMessage)), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("a", {
76193+
})), /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("a", {
7617976194
className: "admin-chat-btn btn btn-primary",
7618076195
"data-toggle": "collapse",
7618176196
href: "#adminChatCollapse",

resources/js/components/AdminChat.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,35 @@ class AdminChat extends React.Component {
55
constructor(props) {
66
super(props);
77
this.state = {
8-
postUpdateMessage : '',
8+
message : '',
99
};
1010
}
1111

12+
addMessage(data) {
13+
this.setState({
14+
message : this.state.message + data
15+
});
16+
const chat = document.querySelector('.admin-chat__message-block');
17+
chat.innerHTML = this.state.message;
18+
}
19+
1220
componentDidMount() {
1321
Echo.private('admin-chat-channel')
14-
.listen('PostUpdated', (data) => {
15-
console.log(data);
22+
.listen('PostUpdatedAdminChat', (event) => {
23+
24+
let time = new Date(event.post.updated_at).toTimeString();
25+
time = time.slice(0, time.indexOf(' '));
26+
27+
let result = `<div class="message">
28+
<time>${time}</time>
29+
<p>Была изменена
30+
<a class="message-link" href="/posts/${event.post.id}">статья #${event.post.id}</a>
31+
</p>
32+
33+
<p>${event.data}</p>
34+
</div>`;
35+
36+
this.addMessage(result);
1637
});
1738
}
1839

@@ -25,7 +46,7 @@ class AdminChat extends React.Component {
2546
<h5 className="admin-chat_header text-center">Сообщения</h5>
2647

2748
<div className="admin-chat__message-block">
28-
{this.state.postUpdateMessage}
49+
2950
</div>
3051
</div>
3152

resources/sass/admin-chat.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
overflow: auto;
1717
}
1818

19-
.admin-chat-btn {
20-
//max-width: 100px;
19+
.message-link {
20+
color: white;
21+
font-weight: bold;
22+
text-decoration: underline;
2123
}
2224

resources/views/layouts/app.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727

2828
@yield('content')
2929

30+
@if (auth()->user() && auth()->user()->hasRole('admin'))
3031
<div class="admin-channel position-fixed border border-primary" id="admin-channel">
3132

3233
</div>
34+
@endif
3335

3436
<footer class="footer mt-auto">
3537
@yield('footer')

0 commit comments

Comments
 (0)