Skip to content

Commit

Permalink
chore: update deps, add map field for testing
Browse files Browse the repository at this point in the history
fix: missing handle event in read forms
  • Loading branch information
zachdaniel committed Mar 19, 2024
1 parent ef9b242 commit 7bafba8
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dev/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ defmodule Demo.Repo do
use AshPostgres.Repo, otp_app: :ash_admin

def installed_extensions() do
["uuid-ossp", "pg_trgm", "citext"]
["uuid-ossp", "pg_trgm", "citext", "ash-functions"]
end
end
21 changes: 21 additions & 0 deletions dev/repo/migrations/20240319011915_migrate_resources4.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Demo.Repo.Migrations.MigrateResources4 do
@moduledoc """
Updates resources based on their most recent snapshots.
This file was autogenerated with `mix ash_postgres.generate_migrations`
"""

use Ecto.Migration

def up do
alter table(:users) do
add :metadata, :map
end
end

def down do
alter table(:users) do
remove :metadata
end
end
end
188 changes: 188 additions & 0 deletions dev/resource_snapshots/repo/users/20240319011915.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{
"attributes": [
{
"default": "fragment(\"uuid_generate_v4()\")",
"size": null,
"type": "uuid",
"source": "id",
"references": null,
"allow_nil?": false,
"primary_key?": true,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "first_name",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "last_name",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "map",
"source": "metadata",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "boolean",
"source": "representative",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "false",
"size": null,
"type": "boolean",
"source": "admin",
"references": null,
"allow_nil?": false,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "text",
"source": "api_key",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "date",
"source": "date_of_birth",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": "map",
"source": "profile",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": [
"array",
"map"
],
"source": "alternate_profiles",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "\"type1\"",
"size": null,
"type": "text",
"source": "type",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": [
"array",
"text"
],
"source": "types",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "nil",
"size": null,
"type": [
"array",
"text"
],
"source": "tags",
"references": null,
"allow_nil?": true,
"primary_key?": false,
"generated?": false
},
{
"default": "fragment(\"now()\")",
"size": null,
"type": "utc_datetime_usec",
"source": "inserted_at",
"references": null,
"allow_nil?": false,
"primary_key?": false,
"generated?": false
},
{
"default": "fragment(\"now()\")",
"size": null,
"type": "utc_datetime_usec",
"source": "updated_at",
"references": null,
"allow_nil?": false,
"primary_key?": false,
"generated?": false
}
],
"table": "users",
"hash": "244C007BD8C9A899AA785259618248F17D1A7DA2C360EE1A75E2DEF11DBE2BEE",
"repo": "Elixir.Demo.Repo",
"multitenancy": {
"global": true,
"attribute": "id",
"strategy": "attribute"
},
"schema": null,
"check_constraints": [],
"identities": [
{
"name": "representative_name",
"keys": [
"first_name",
"last_name"
],
"base_filter": "representative = true",
"index_name": "users_representative_name_index"
}
],
"custom_indexes": [],
"base_filter": null,
"custom_statements": [],
"has_create_action": true
}
2 changes: 2 additions & 0 deletions dev/resources/accounts/resources/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ defmodule Demo.Accounts.User do
constraints min_length: 1
end

attribute :metadata, :map

attribute :representative, :boolean do
allow_nil? false
default false
Expand Down
81 changes: 81 additions & 0 deletions lib/ash_admin/components/resource/data_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,87 @@ defmodule AshAdmin.Components.Resource.DataTable do
)}
end

def handle_event("add_form", %{"path" => path} = params, socket) do
type =
case params["type"] do
"lookup" -> :read
_ -> :create
end

form = AshPhoenix.Form.add_form(socket.assigns.form, path, type: type)

{:noreply,
socket
|> assign(:form, form)}
end

def handle_event("remove_form", %{"path" => path}, socket) do
form = AshPhoenix.Form.remove_form(socket.assigns.form, path)

{:noreply,
socket
|> assign(:form, form)}
end

def handle_event("append_value", %{"path" => path, "field" => field}, socket) do
list =
AshPhoenix.Form.get_form(socket.assigns.form, path)
|> AshPhoenix.Form.value(String.to_existing_atom(field))
|> Kernel.||([])
|> indexed_list()
|> append_to_and_map(nil)

params =
put_in_creating(
socket.assigns.form.params || %{},
Enum.map(AshPhoenix.Form.parse_path!(socket.assigns.form, path) ++ [field], &to_string/1),
list
)

form = AshPhoenix.Form.validate(socket.assigns.form, params)

{:noreply,
socket
|> assign(:form, form)}
end

defp indexed_list(map) when is_map(map) do
map
|> Map.keys()
|> Enum.map(&String.to_integer/1)
|> Enum.sort()
|> Enum.map(&map[to_string(&1)])
rescue
_ ->
List.wrap(map)
end

defp indexed_list(other), do: List.wrap(other)

defp append_to_and_map(list, value) do
list
|> Enum.concat([value])
|> Enum.with_index()
|> Map.new(fn {v, i} ->
{"#{i}", v}
end)
end

defp put_in_creating(map, [key], value) do
Map.put(map || %{}, key, value)
end

defp put_in_creating(list, [key | rest], value) when is_list(list) do
List.update_at(list, String.to_integer(key), &put_in_creating(&1, rest, value))
end

defp put_in_creating(map, [key | rest], value) do
map
|> Kernel.||(%{})
|> Map.put_new(key, %{})
|> Map.update!(key, &put_in_creating(&1, rest, value))
end

defp render_pagination_links(assigns, placement) do
assigns = assign(assigns, :placement, placement)

Expand Down
Loading

0 comments on commit 7bafba8

Please sign in to comment.