Skip to content

Commit 1bcd04a

Browse files
authored
Update to liveview 1.x (#28)
* Use upstream exqlite The changes have been merged into upstream: elixir-sqlite/exqlite@main...elixir-desktop:exqlite:main#diff-76ed074a9305c04054cdebb9e9aad2d818052b07091de1f20cad0bbac34ffb52R62 Merged in elixir-sqlite/exqlite#214 and 898bd41101b738097bf * Heavy updates to liveview 1.x Copy over most of a fresh phoenix 1.8 generated project skeleton.
1 parent a591f54 commit 1bcd04a

26 files changed

+893
-240
lines changed

config/config.exs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import Config
22

33
config :esbuild,
4-
version: "0.12.18",
4+
version: "0.25.4",
55
default: [
6-
args: ~w(js/app.js --bundle --target=es2016 --outdir=../priv/static/assets),
6+
args:
7+
~w(js/app.js --bundle --target=es2022 --outdir=../priv/static/assets/js --external:/fonts/* --external:/images/* --alias:@=.),
78
cd: Path.expand("../assets", __DIR__),
8-
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
9+
env: %{"NODE_PATH" => [Path.expand("../deps", __DIR__), Mix.Project.build_path()]}
910
]
1011

1112
config :dart_sass,
1213
version: "1.61.0",
1314
default: [
14-
args: ~w(css/app.scss ../priv/static/assets/app.css),
15+
args: ~w(css/app.scss ../priv/static/assets/css/app.css),
1516
cd: Path.expand("../assets", __DIR__)
1617
]
1718

@@ -27,12 +28,17 @@ config :logger, :console,
2728

2829
# Configures the endpoint
2930
config :todo_app, TodoWeb.Endpoint,
31+
# url: [host: "localhost", port: 10_000 + :rand.uniform(45_000)],
3032
# because of the iOS rebind - this is now a fixed port, but randomly selected
3133
http: [ip: {127, 0, 0, 1}, port: 10_000 + :rand.uniform(45_000)],
32-
render_errors: [view: TodoWeb.ErrorView, accepts: ~w(html json), layout: false],
34+
adapter: Bandit.PhoenixAdapter,
35+
render_errors: [
36+
formats: [html: TodoWeb.ErrorHTML, json: TodoWeb.ErrorJSON],
37+
layout: false
38+
],
3339
pubsub_server: TodoApp.PubSub,
3440
live_view: [signing_salt: "sWpG9ljX"],
35-
secret_key_base: :crypto.strong_rand_bytes(32),
41+
secret_key_base: :crypto.strong_rand_bytes(64),
3642
server: true
3743

3844
config :phoenix, :json_library, Jason

lib/todo_app.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,24 @@ defmodule TodoApp do
2323
Desktop.identify_default_locale(TodoWeb.Gettext)
2424
File.mkdir_p!(config_dir())
2525

26+
# TODO: move to runtime.exs
2627
Application.put_env(:todo_app, TodoApp.Repo,
2728
database: Path.join(config_dir(), "/database.sq3")
2829
)
2930

30-
{:ok, sup} = Supervisor.start_link([TodoApp.Repo], name: __MODULE__, strategy: :one_for_one)
31-
TodoApp.Repo.initialize()
31+
:ets.new(:session, [:named_table, :public, read_concurrency: true])
3232

33-
{:ok, _} = Supervisor.start_child(sup, TodoWeb.Sup)
33+
{:ok, sup} = Supervisor.start_link([
34+
TodoWeb.Telemetry,
35+
TodoApp.Repo,
36+
{DNSCluster, query: Application.get_env(:todo, :dns_cluster_query) || :ignore},
37+
{Phoenix.PubSub, name: TodoApp.PubSub},
38+
# Start a worker by calling: Todo.Worker.start_link(arg)
39+
# {Todo.Worker, arg},
40+
# Start to serve requests, typically the last entry
41+
TodoWeb.Endpoint
42+
], name: __MODULE__, strategy: :one_for_one)
43+
TodoApp.Repo.initialize()
3444

3545
{:ok, _} =
3646
Supervisor.start_child(sup, {

lib/todo_app/menu_bar.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ defmodule TodoApp.MenuBar do
1616
<item
1717
type="checkbox" onclick={"toggle:#{item.id}"}
1818
checked={item.status == "done"}
19-
><%= item.text %></item>
19+
>{item.text}</item>
2020
<% end %>
2121
<hr/>
22-
<item onclick="quit"><%= gettext "Quit" %></item>
22+
<item onclick="quit">{gettext "Quit"}</item>
2323
</menu>
2424
<menu label={gettext "Extra"}>
25-
<item onclick="notification"><%= gettext "Show Notification" %></item>
26-
<item onclick="observer"><%= gettext "Show Observer" %></item>
27-
<item onclick="browser"><%= gettext "Open Browser" %></item>
25+
<item onclick="notification">{gettext "Show Notification"}</item>
26+
<item onclick="observer">{gettext "Show Observer"}</item>
27+
<item onclick="browser">{gettext "Open Browser"}</item>
2828
</menu>
2929
</menubar>
3030
"""

lib/todo_web.ex

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,84 +17,91 @@ defmodule TodoWeb do
1717
and import those modules here.
1818
"""
1919

20-
def controller do
20+
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
21+
22+
def router do
2123
quote do
22-
use Phoenix.Controller, namespace: TodoWeb
24+
use Phoenix.Router, helpers: false
2325

26+
# Import common connection and controller functions to use in pipelines
2427
import Plug.Conn
25-
use Gettext, backend: TodoWeb.Gettext
26-
alias TodoWeb.Router.Helpers, as: Routes
28+
import Phoenix.Controller
29+
import Phoenix.LiveView.Router
2730
end
2831
end
2932

30-
def view do
33+
def channel do
3134
quote do
32-
use Phoenix.View,
33-
root: "lib/todo_web/templates",
34-
namespace: TodoWeb
35+
use Phoenix.Channel
36+
end
37+
end
3538

36-
# Import convenience functions from controllers
37-
import Phoenix.Controller,
38-
only: [view_module: 1, view_template: 1]
39+
def controller do
40+
quote do
41+
use Phoenix.Controller, formats: [:html, :json]
42+
43+
use Gettext, backend: TodoWeb.Gettext
3944

40-
alias Phoenix.Flash
45+
import Plug.Conn
4146

42-
# Include shared imports and aliases for views
43-
unquote(view_helpers())
44-
alias TodoWeb.Router.Helpers, as: Routes
47+
unquote(verified_routes())
4548
end
4649
end
4750

4851
def live_view do
4952
quote do
50-
use Phoenix.LiveView,
51-
layout: {TodoWeb.LayoutView, :live}
53+
use Phoenix.LiveView
5254

53-
unquote(view_helpers())
54-
alias TodoWeb.Router.Helpers, as: Routes
55+
unquote(html_helpers())
5556
end
5657
end
5758

5859
def live_component do
5960
quote do
6061
use Phoenix.LiveComponent
6162

62-
unquote(view_helpers())
63-
alias TodoWeb.Router.Helpers, as: Routes
63+
unquote(html_helpers())
6464
end
6565
end
6666

67-
def router do
67+
def html do
6868
quote do
69-
use Phoenix.Router
69+
use Phoenix.Component
7070

71-
import Plug.Conn
72-
import Phoenix.Controller
73-
import Phoenix.LiveView.Router
71+
# Import convenience functions from controllers
72+
import Phoenix.Controller,
73+
only: [get_csrf_token: 0, view_module: 1, view_template: 1]
74+
75+
# Include general helpers for rendering HTML
76+
unquote(html_helpers())
7477
end
7578
end
7679

77-
def channel do
80+
defp html_helpers do
7881
quote do
79-
use Phoenix.Channel
82+
# Translation
8083
use Gettext, backend: TodoWeb.Gettext
81-
end
82-
end
8384

84-
defp view_helpers do
85-
quote do
86-
# Use all HTML functionality (forms, tags, etc)
87-
use Phoenix.HTML
85+
# HTML escaping functionality
86+
import Phoenix.HTML
87+
# Core UI components
88+
import TodoWeb.CoreComponents
8889

89-
# Import LiveView helpers (live_render, live_component, live_patch, etc)
90-
import Phoenix.LiveView.Helpers
91-
import Phoenix.Component
90+
# Common modules used in templates
91+
alias Phoenix.LiveView.JS
92+
alias TodoWeb.Layouts
9293

93-
# Import basic rendering functionality (render, render_layout, etc)
94-
import Phoenix.View
94+
# Routes generation with the ~p sigil
95+
unquote(verified_routes())
96+
end
97+
end
9598

96-
import TodoWeb.ErrorHelpers
97-
use Gettext, backend: TodoWeb.Gettext
99+
def verified_routes do
100+
quote do
101+
use Phoenix.VerifiedRoutes,
102+
endpoint: TodoWeb.Endpoint,
103+
router: TodoWeb.Router,
104+
statics: TodoWeb.static_paths()
98105
end
99106
end
100107

0 commit comments

Comments
 (0)