-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdev.exs
295 lines (246 loc) · 7.81 KB
/
dev.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
Logger.configure(level: :debug)
pg_url = System.get_env("PG_URL") || "postgres:postgres@127.0.0.1"
Application.put_env(:live_admin, Demo.Repo,
url: "ecto://#{pg_url}/phx_admin_dev"
)
_ = Ecto.Adapters.Postgres.storage_up(Demo.Repo.config())
Application.put_env(:live_admin, DemoWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "Hu4qQN3iKzTV4fJxhorPQlA/osH9fAMtbtjVS58PFgfw3ja5Z18Q/WSNR9wP4OfW",
live_view: [signing_salt: "hMegieSe"],
http: [port: System.get_env("PORT") || 4000],
debug_errors: true,
check_origin: false,
watchers: [
npm: ["run", "watch", cd: "assets"],
npx: [
"tailwindcss",
"--input=css/app.css",
"--output=../dist/css/app.css",
"--postcss",
"--watch",
cd: "assets"
],
npx: [
"tailwindcss",
"--input=css/default_overrides.css",
"--output=../dist/css/default_overrides.css",
"--postcss",
"--watch",
cd: "assets"
]
],
live_reload: [
patterns: [
~r"dist/.*(js|css|png|jpeg|jpg|gif|svg)$",
~r"lib/live_admin/components/.*(ex)$",
~r"lib/live_admin/templates/.*/.*(ex)$",
~r"lib/live_admin/.*(ex)$"
]
],
pubsub_server: Demo.PubSub
)
Application.put_env(:live_admin, :ecto_repo, Demo.Repo)
Application.put_env(:live_admin, :immutable_fields, [:inserted_at])
Application.put_env(:live_admin, :css_overrides, {DemoWeb.Renderer, :render_css, []})
Application.put_env(:live_admin, :gettext_backend, Demo.Gettext)
defmodule Demo.Populator do
import Ecto.Query
alias Demo.Repo
def reset do
teardown()
run()
end
def run do
Enum.each(1..Enum.random(1..10001), fn _ ->
%Demo.Accounts.User{
name: Faker.Person.name(),
email: "#{Ecto.UUID.generate()}@example.com",
settings: %{},
active: true,
birth_date: ~D[1999-12-31],
stars_count: Enum.random(0..100),
private_data: %{},
encrypted_password: :crypto.strong_rand_bytes(16) |> Base.encode16(),
posts: [
%Demo.Posts.Post{
title: Faker.Lorem.paragraph(1) |> String.slice(0..9),
categories: Ecto.Enum.values(Demo.Posts.Post, :categories) |> Enum.take(Enum.random(0..2)),
tags: Faker.Lorem.words(Enum.random(0..5)),
body: Faker.Lorem.paragraphs() |> Enum.join("\n\n"),
disabled_user: get_user_if(:rand.uniform(2) == 1),
previous_versions: [%Demo.Posts.Post.Version{body: Faker.Lorem.paragraphs() |> Enum.join("\n\n")}]
}
]
}
|> Demo.Repo.insert!()
end)
end
defp teardown do
Repo.delete_all(Demo.Accounts.User)
Repo.delete_all(Demo.Posts.Post)
Repo.delete_all(Demo.Accounts.User.Profile)
end
defp get_user_if(true), do: from(Demo.Accounts.User, order_by: fragment("RANDOM()"), limit: 1) |> Demo.Repo.one()
defp get_user_if(false), do: nil
end
defmodule DemoWeb.CreateUserForm do
use Phoenix.LiveComponent
use PhoenixHTMLHelpers
import Phoenix.HTML
import Phoenix.HTML.Form
import LiveAdmin, only: [route_with_params: 2]
import LiveAdmin.ErrorHelpers
@impl true
def update(assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign(:changeset, Ecto.Changeset.change(%Demo.Accounts.User{}))
{:ok, socket}
end
@impl true
def render(assigns) do
assigns = assign(assigns, :enabled, Enum.empty?(assigns.changeset.errors))
~H"""
<div>
<.form
:let={f}
for={@changeset}
as={:params}
phx-change="validate"
phx-submit="create"
phx-target={@myself}
class="resource__form"
>
<div class={"field__group"}>
<%= label(f, :name, class: "field__label") %>
<%= textarea(f, :name, rows: 1, class: "field__text") %>
<%= error_tag(f, :name) %>
</div>
<div class={"field__group"}>
<%= label(f, :email, class: "field__label") %>
<%= textarea(f, :email, rows: 1, class: "field__text") %>
<%= error_tag(f, :email) %>
</div>
<div class={"field__group"}>
<%= label(f, :password, class: "field__label") %>
<%= password_input(f, :password, class: "field__text", value: input_value(f, :password)) %>
<%= error_tag(f, :password) %>
</div>
<div class={"field__group"}>
<%= label(f, :password_confirmation, class: "field__label") %>
<%= password_input(f, :password_confirmation, class: "field__text") %>
<%= error_tag(f, :password_confirmation) %>
</div>
<div class="form__actions">
<%= submit("Save",
class: "resource__action#{if !@enabled, do: "--disabled", else: "--btn"}",
disabled: !@enabled
) %>
</div>
</.form>
</div>
"""
end
@impl true
def handle_event(
"validate",
%{"params" => params},
%{assigns: %{changeset: changeset}} = socket
) do
changeset =
changeset.data
|> Ecto.Changeset.cast(params, [:name, :email, :password])
|> Ecto.Changeset.validate_required([:name, :email, :password])
|> Ecto.Changeset.validate_confirmation(:password)
|> Map.put(:action, :validate)
{:noreply, assign(socket, changeset: changeset)}
end
@impl true
def handle_event("create", %{"params" => params}, socket = %{assigns: assigns}) do
socket =
%Demo.Accounts.User{}
|> Ecto.Changeset.cast(params, [:name, :email, :stars_count, :roles])
|> Ecto.Changeset.validate_required([:name, :email])
|> Ecto.Changeset.unique_constraint(:email)
|> Demo.Repo.insert(prefix: assigns.prefix)
|> case do
{:ok, _} -> push_redirect(socket, to: route_with_params(assigns, params: [prefix: assigns.prefix]))
{:error, changeset} -> assign(socket, changeset: changeset)
end
{:noreply, socket}
end
end
defmodule DemoWeb.PostsAdmin.Home do
use Phoenix.LiveComponent
@impl true
def render(assigns) do
~H"""
<div class="flex h-full items-center justify-center">
<div class="w-1/2">
This is only for managing posts
</div>
</div>
"""
end
end
defmodule DemoWeb.Extra do
use Phoenix.LiveView
@impl true
def render(assigns) do
~H"""
<div class="flex h-full items-center justify-center">
<div class="w-1/2">
This is an extra page
</div>
</div>
"""
end
end
defmodule DemoWeb.Router do
use Phoenix.Router
import LiveAdmin.Router
import Phoenix.LiveView.Router
pipeline :browser do
plug :fetch_session
plug :user_id_stub
end
scope "/" do
pipe_through :browser
get "/", DemoWeb.PageController, :index
live_admin "/admin", title: "DevAdmin" do
admin_resource "/users/profiles", Demo.Accounts.User.Profile
admin_resource "/users", DemoWeb.UserAdmin
live "/extra", DemoWeb.Extra, :index, as: :extra
end
live_admin "/posts-admin", components: [home: DemoWeb.PostsAdmin.Home] do
admin_resource "/posts", Demo.Posts.Post
admin_resource "/users", DemoWeb.UserAdmin
end
end
defp user_id_stub(conn, _) do
Plug.Conn.assign(conn, :user_id, 1)
end
end
defmodule DemoWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :live_admin
socket "/live", Phoenix.LiveView.Socket
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader
plug Plug.Session,
store: :cookie,
key: "_live_view_key",
signing_salt: "/VEDsdfsffMnp5"
plug DemoWeb.Router
end
Application.put_env(:phoenix, :serve_endpoints, true)
Application.ensure_all_started(:os_mon)
Task.async(fn ->
children = [Demo.Repo, DemoWeb.Endpoint, {Phoenix.PubSub, name: Demo.PubSub, adapter: Phoenix.PubSub.PG2}]
{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
Demo.Populator.reset()
Process.sleep(:infinity)
end)
|> Task.await(:infinity)