Skip to content

Commit 882b783

Browse files
committed
refactor: refactor watch to single video hook
1 parent e0c7be5 commit 882b783

File tree

7 files changed

+15
-38
lines changed

7 files changed

+15
-38
lines changed

assets/js/app.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ channel.join()
4949
let liveSocket = new LiveSocket("/live", Socket, {
5050
params: { _csrf_token: csrfToken },
5151
hooks: {
52-
movie: {
52+
video: {
5353
mounted() {
5454
const urlParams = new URLSearchParams(window.location.search)
5555

@@ -58,38 +58,15 @@ let liveSocket = new LiveSocket("/live", Socket, {
5858
}
5959

6060
window.addEventListener("beforeunload", event => {
61-
this.pushEvent("movie_destroyed", {
61+
this.pushEvent("video_destroyed", {
6262
current_time: Math.floor(this.el.currentTime),
6363
duration: Math.floor(this.el.duration)
6464
})
6565

6666
delete event["returnValue"]
6767
})
6868

69-
this.pushEvent("movie_played")
70-
},
71-
destroyed() {
72-
window.removeEventListener("beforeunload")
73-
}
74-
},
75-
episode: {
76-
mounted() {
77-
const urlParams = new URLSearchParams(window.location.search)
78-
79-
if (urlParams.has("seconds")) {
80-
this.el.currentTime = urlParams.get("seconds")
81-
}
82-
83-
window.addEventListener("beforeunload", event => {
84-
this.pushEvent("episode_destroyed", {
85-
current_time: Math.floor(this.el.currentTime),
86-
duration: Math.floor(this.el.duration)
87-
})
88-
89-
delete event["returnValue"]
90-
})
91-
92-
this.pushEvent("episode_played")
69+
this.pushEvent("video_played")
9370
},
9471
destroyed() {
9572
window.removeEventListener("beforeunload")

lib/media_server_web/live/watch_episode_live/show.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule MediaServerWeb.WatchEpisodeLive.Show do
3030

3131
@impl true
3232
def handle_event(
33-
"episode_destroyed",
33+
"video_destroyed",
3434
%{
3535
"current_time" => current_time,
3636
"duration" => duration
@@ -50,7 +50,7 @@ defmodule MediaServerWeb.WatchEpisodeLive.Show do
5050
{:noreply, socket}
5151
end
5252

53-
def handle_event("episode_played", _params, socket) do
53+
def handle_event("video_played", _params, socket) do
5454
action = Components.list_actions() |> List.first()
5555

5656
Actions.create_episode(%{

lib/media_server_web/live/watch_episode_live/show.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<video id="video" phx-hook="episode" poster={MediaServerWeb.Repositories.Episodes.get_background(@episode)} controlsList="nodownload" controls autoplay>
1+
<video id="video" phx-hook="video" poster={MediaServerWeb.Repositories.Episodes.get_background(@episode)} controlsList="nodownload" controls autoplay>
22
<source src={Routes.stream_episode_path(@socket, :show, @episode["id"], token: Phoenix.Token.sign(MediaServerWeb.Endpoint, "user auth", @current_user.id))} type="video/mp4">
33

44
<%= if MediaServerWeb.Helpers.has_subtitle(MediaServerWeb.Helpers.get_parent_path(@episode["episodeFile"]["path"]), MediaServerWeb.Helpers.get_file_name(@episode["episodeFile"]["relativePath"])) do %>

lib/media_server_web/live/watch_movie_live/show.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule MediaServerWeb.WatchMovieLive.Show do
3030

3131
@impl true
3232
def handle_event(
33-
"movie_destroyed",
33+
"video_destroyed",
3434
%{
3535
"current_time" => current_time,
3636
"duration" => duration
@@ -49,7 +49,7 @@ defmodule MediaServerWeb.WatchMovieLive.Show do
4949
{:noreply, socket}
5050
end
5151

52-
def handle_event("movie_played", _params, socket) do
52+
def handle_event("video_played", _params, socket) do
5353
action = Components.list_actions() |> List.first()
5454

5555
Actions.create_movie(%{

lib/media_server_web/live/watch_movie_live/show.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<video id="video" phx-hook="movie" poster={MediaServerWeb.Repositories.Movies.get_background(@movie)} controlsList="nodownload" controls autoplay>
1+
<video id="video" phx-hook="video" poster={MediaServerWeb.Repositories.Movies.get_background(@movie)} controlsList="nodownload" controls autoplay>
22
<source src={Routes.stream_movie_path(@socket, :show, @movie["id"], token: Phoenix.Token.sign(MediaServerWeb.Endpoint, "user auth", @current_user.id))} type="video/mp4">
33

44
<%= if MediaServerWeb.Helpers.has_subtitle(@movie["folderName"], @movie["movieFile"]["relativePath"]) do %>

test/media_server_web/live/watch_episode_live_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule MediaServerWeb.WatchEpisodeLiveTest do
3030

3131
{:ok, view, _html} = live(conn, Routes.watch_episode_show_path(conn, :show, episode["id"]))
3232

33-
render_hook(view, :episode_played)
33+
render_hook(view, :video_played)
3434

3535
assert Enum.count(Actions.list_episode_actions()) === 1
3636
end
@@ -47,7 +47,7 @@ defmodule MediaServerWeb.WatchEpisodeLiveTest do
4747

4848
{:ok, view, _html} = live(conn, Routes.watch_episode_show_path(conn, :show, episode["id"]))
4949

50-
render_hook(view, :episode_destroyed, %{
50+
render_hook(view, :video_destroyed, %{
5151
episode_id: episode["id"],
5252
serie_id: episode["seriesId"],
5353
current_time: 39,
@@ -70,7 +70,7 @@ defmodule MediaServerWeb.WatchEpisodeLiveTest do
7070

7171
{:ok, view, _html} = live(conn, Routes.watch_episode_show_path(conn, :show, episode["id"]))
7272

73-
render_hook(view, :episode_destroyed, %{
73+
render_hook(view, :video_destroyed, %{
7474
episode_id: episode["id"],
7575
serie_id: episode["seriesId"],
7676
current_time: 90,

test/media_server_web/live/watch_movie_live_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule MediaServerWeb.WatchMovieLiveTest do
2727

2828
{:ok, view, _html} = live(conn, Routes.watch_movie_show_path(conn, :show, movie["id"]))
2929

30-
render_hook(view, :movie_played)
30+
render_hook(view, :video_played)
3131

3232
assert Enum.count(ActionsFixtures.get_movie_played()) === 1
3333
end
@@ -42,7 +42,7 @@ defmodule MediaServerWeb.WatchMovieLiveTest do
4242

4343
{:ok, view, _html} = live(conn, Routes.watch_movie_show_path(conn, :show, movie["id"]))
4444

45-
render_hook(view, :movie_destroyed, %{
45+
render_hook(view, :video_destroyed, %{
4646
current_time: 89,
4747
duration: 100
4848
})
@@ -60,7 +60,7 @@ defmodule MediaServerWeb.WatchMovieLiveTest do
6060

6161
{:ok, view, _html} = live(conn, Routes.watch_movie_show_path(conn, :show, movie["id"]))
6262

63-
render_hook(view, :movie_destroyed, %{
63+
render_hook(view, :video_destroyed, %{
6464
current_time: 90,
6565
duration: 100
6666
})

0 commit comments

Comments
 (0)