Skip to content

Commit ae0bed9

Browse files
committed
Update to latest phoenix and liveview
1 parent 41af779 commit ae0bed9

File tree

20 files changed

+2746
-2093
lines changed

20 files changed

+2746
-2093
lines changed

apps/components_guide/lib/components_guide/content/text.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ defmodule ComponentsGuide.Content.Text do
1111
|> cast(params, [:content])
1212
|> validate_required([:content])
1313
end
14-
end
1514

16-
defimpl Phoenix.Param, for: ComponentsGuide.Content.Text do
1715
def to_param(%ComponentsGuide.Content.Text{id: id}) do
1816
Base.url_encode64(id)
1917
end
@@ -22,3 +20,13 @@ defimpl Phoenix.Param, for: ComponentsGuide.Content.Text do
2220
Base.url_encode64(:crypto.hash(:sha256, content))
2321
end
2422
end
23+
24+
# defimpl Phoenix.Param, for: ComponentsGuide.Content.Text do
25+
# def to_param(%ComponentsGuide.Content.Text{id: id}) do
26+
# Base.url_encode64(id)
27+
# end
28+
29+
# def to_param(%ComponentsGuide.Content.Text{content: content}) when is_binary(content) do
30+
# Base.url_encode64(:crypto.hash(:sha256, content))
31+
# end
32+
# end

apps/components_guide_web/assets/css/app.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
@tailwind utilities;
88

9+
@import "../node_modules/nprogress/nprogress.css";
10+
911
html {
1012
@apply bg-gray-900;
1113
}

apps/components_guide_web/assets/js/app.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,26 @@ import css from "../css/app.css"
1010
// Import dependencies
1111
//
1212
import "phoenix_html";
13-
13+
import {Socket} from "phoenix"
14+
import NProgress from "nprogress"
1415
import LiveSocket from "phoenix_live_view"
1516

16-
let liveSocket = new LiveSocket("/live")
17+
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
18+
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})
19+
20+
// Show progress bar on live navigation and form submits
21+
window.addEventListener("phx:page-loading-start", info => NProgress.start())
22+
window.addEventListener("phx:page-loading-stop", info => NProgress.done())
23+
24+
// connect if there are any LiveViews on the page
1725
liveSocket.connect()
1826

27+
// expose liveSocket on window for web console debug logs and latency simulation:
28+
// >> liveSocket.enableDebug()
29+
// >> liveSocket.enableLatencySim(1000)
30+
window.liveSocket = liveSocket
31+
32+
1933
import Vue from "vue";
2034
import VueEditor from "./editor.vue";
2135

apps/components_guide_web/assets/package-lock.json

Lines changed: 2495 additions & 1976 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/components_guide_web/assets/package.json

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@
66
"watch": "webpack --mode development --watch"
77
},
88
"dependencies": {
9+
"nprogress": "^0.2.0",
910
"phoenix": "file:../../../deps/phoenix",
1011
"phoenix_html": "file:../../../deps/phoenix_html",
1112
"phoenix_live_view": "file:../../../deps/phoenix_live_view",
12-
"tiptap": "^1.20.1",
13-
"vue": "^2.6.10"
13+
"tiptap": "^1.27.1",
14+
"vue": "^2.6.11"
1415
},
1516
"devDependencies": {
16-
"@babel/core": "^7.0.0",
17-
"@babel/preset-env": "^7.0.0",
17+
"@babel/core": "^7.9.6",
18+
"@babel/preset-env": "^7.9.6",
1819
"@tailwindcss/ui": "^0.2.2",
19-
"autoprefixer": "^9.6.0",
20-
"babel-loader": "^8.0.0",
20+
"autoprefixer": "^9.8.0",
21+
"babel-loader": "^8.1.0",
2122
"copy-webpack-plugin": "^4.5.0",
2223
"css-loader": "^2.1.1",
2324
"install": "^0.13.0",
2425
"mini-css-extract-plugin": "^0.4.0",
25-
"npm": "^6.14.4",
26-
"optimize-css-assets-webpack-plugin": "^5.0.1",
26+
"npm": "^6.14.5",
27+
"optimize-css-assets-webpack-plugin": "^5.0.3",
2728
"postcss-loader": "^3.0.0",
28-
"tailwindcss": "^1.4.1",
29+
"tailwindcss": "^1.4.6",
2930
"uglifyjs-webpack-plugin": "^1.2.4",
30-
"vue-loader": "^15.7.0",
31-
"vue-template-compiler": "^2.6.10",
32-
"webpack": "^4.4.0",
33-
"webpack-cli": "^3.3.4"
31+
"vue-loader": "^15.9.2",
32+
"vue-template-compiler": "^2.6.11",
33+
"webpack": "^4.43.0",
34+
"webpack-cli": "^3.3.11"
3435
}
3536
}

apps/components_guide_web/lib/components_guide_web.ex

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ defmodule ComponentsGuideWeb do
2020
def controller do
2121
quote do
2222
use Phoenix.Controller, namespace: ComponentsGuideWeb
23+
2324
import Plug.Conn
2425
import ComponentsGuideWeb.Gettext
2526
alias ComponentsGuideWeb.Router.Helpers, as: Routes
26-
import Phoenix.LiveView.Controller, only: [live_render: 3]
2727
end
2828
end
2929

@@ -36,23 +36,32 @@ defmodule ComponentsGuideWeb do
3636
# Import convenience functions from controllers
3737
import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1]
3838

39-
# Use all HTML functionality (forms, tags, etc)
40-
use Phoenix.HTML
39+
# Include shared imports and aliases for views
40+
unquote(view_helpers())
41+
end
42+
end
4143

42-
import ComponentsGuideWeb.ErrorHelpers
43-
import ComponentsGuideWeb.Gettext
44-
alias ComponentsGuideWeb.Router.Helpers, as: Routes
45-
# import Phoenix.LiveView.Helpers
46-
import Phoenix.LiveView, only: [live_render: 2, live_render: 3]
44+
def live_view do
45+
quote do
46+
use Phoenix.LiveView,
47+
layout: {ComponentsGuideWeb.LayoutView, "live.html"}
4748

48-
import Paredown
49-
alias ComponentsGuideWeb.StylingHelpers, as: Styling
49+
unquote(view_helpers())
50+
end
51+
end
52+
53+
def live_component do
54+
quote do
55+
use Phoenix.LiveComponent
56+
57+
unquote(view_helpers())
5058
end
5159
end
5260

5361
def router do
5462
quote do
5563
use Phoenix.Router
64+
5665
import Plug.Conn
5766
import Phoenix.Controller
5867
import Phoenix.LiveView.Router
@@ -66,6 +75,26 @@ defmodule ComponentsGuideWeb do
6675
end
6776
end
6877

78+
defp view_helpers do
79+
quote do
80+
# Use all HTML functionality (forms, tags, etc)
81+
use Phoenix.HTML
82+
83+
# Import LiveView helpers (live_render, live_component, live_patch, etc)
84+
import Phoenix.LiveView.Helpers
85+
86+
# Import basic rendering functionality (render, render_layout, etc)
87+
import Phoenix.View
88+
89+
import ComponentsGuideWeb.ErrorHelpers
90+
import ComponentsGuideWeb.Gettext
91+
alias ComponentsGuideWeb.Router.Helpers, as: Routes
92+
93+
import Paredown
94+
alias ComponentsGuideWeb.StylingHelpers, as: Styling
95+
end
96+
end
97+
6998
@doc """
7099
When used, dispatch to the appropriate controller/view/etc.
71100
"""

apps/components_guide_web/lib/components_guide_web/application.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ defmodule ComponentsGuideWeb.Application do
88
def start(_type, _args) do
99
# List all child processes to be supervised
1010
children = [
11+
# Start the Telemetry supervisor
12+
ComponentsGuideWeb.Telemetry,
13+
# Start the PubSub system
14+
{Phoenix.PubSub, name: ComponentsGuide.PubSub},
1115
# Start the endpoint when the application starts
1216
ComponentsGuideWeb.Endpoint
1317
# Starts a worker by calling: ComponentsGuideWeb.Worker.start_link(arg)

apps/components_guide_web/lib/components_guide_web/endpoint.ex

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
defmodule ComponentsGuideWeb.Endpoint do
22
use Phoenix.Endpoint, otp_app: :components_guide_web
33

4-
socket "/live", Phoenix.LiveView.Socket
4+
@session_options [
5+
store: :cookie,
6+
key: "_components_guide_web_key",
7+
signing_salt: "BfF2quM8"
8+
]
59

610
socket "/socket", ComponentsGuideWeb.UserSocket,
711
websocket: true,
812
longpoll: false
913

14+
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
15+
1016
# Serve at "/" the static files from "priv/static" directory.
1117
#
1218
# You should set gzip to true if you are running phx.digest
@@ -25,8 +31,12 @@ defmodule ComponentsGuideWeb.Endpoint do
2531
plug Phoenix.CodeReloader
2632
end
2733

34+
plug Phoenix.LiveDashboard.RequestLogger,
35+
param_key: "request_logger",
36+
cookie_key: "request_logger"
37+
2838
plug Plug.RequestId
29-
plug Plug.Logger
39+
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
3040

3141
plug Plug.Parsers,
3242
parsers: [:urlencoded, :multipart, :json],
@@ -35,14 +45,6 @@ defmodule ComponentsGuideWeb.Endpoint do
3545

3646
plug Plug.MethodOverride
3747
plug Plug.Head
38-
39-
# The session will be stored in the cookie and signed,
40-
# this means its contents can be read but not tampered with.
41-
# Set :encryption_salt if you would also like to encrypt it.
42-
plug Plug.Session,
43-
store: :cookie,
44-
key: "_components_guide_web_key",
45-
signing_salt: "VQAmPv+f"
46-
48+
plug Plug.Session, @session_options
4749
plug ComponentsGuideWeb.Router
4850
end

apps/components_guide_web/lib/components_guide_web/live/activity.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule ComponentsGuideWeb.ActivityLive do
1111
Ecto.UUID.generate
1212
end
1313

14-
def mount(%{}, socket) do
14+
def mount(%{}, _session, socket) do
1515
if connected?(socket), do: :timer.send_interval(5000, self(), :update)
1616

1717
{:ok, assign(socket, uuid: new_uuid())}

apps/components_guide_web/lib/components_guide_web/live/fake_search.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule ComponentsGuideWeb.FakeSearchLive do
2828
Ecto.UUID.generate()
2929
end
3030

31-
def mount(%{}, socket) do
31+
def mount(%{}, _session, socket) do
3232
#if connected?(socket), do: :timer.send_interval(5000, self(), :update)
3333

3434
items = FakeSearch.list()

0 commit comments

Comments
 (0)