feat: type not editable
This commit is contained in:
parent
adea380d86
commit
e47e266570
6 changed files with 180 additions and 14 deletions
|
|
@ -5,7 +5,7 @@ defmodule MvWeb.CustomFieldLive.FormComponent do
|
|||
## Features
|
||||
- Create new custom field definitions
|
||||
- Edit existing custom fields
|
||||
- Select value type from supported types
|
||||
- Select value type from supported types (only on create; immutable after creation)
|
||||
- Set required flag
|
||||
- Real-time validation
|
||||
|
||||
|
|
@ -44,15 +44,36 @@ defmodule MvWeb.CustomFieldLive.FormComponent do
|
|||
>
|
||||
<.input field={@form[:name]} type="text" label={gettext("Name")} />
|
||||
|
||||
<.input
|
||||
field={@form[:value_type]}
|
||||
type="select"
|
||||
label={gettext("Value type")}
|
||||
options={
|
||||
Ash.Resource.Info.attribute(Mv.Membership.CustomField, :value_type).constraints[:one_of]
|
||||
|> Enum.map(fn type -> {MvWeb.Translations.FieldTypes.label(type), type} end)
|
||||
}
|
||||
/>
|
||||
<%= if @custom_field do %>
|
||||
<%!-- Show value_type as read-only text when editing --%>
|
||||
<div class="form-control">
|
||||
<label class="label">
|
||||
<span class="label-text">{gettext("Value type")}</span>
|
||||
</label>
|
||||
<div class="input input-bordered bg-base-200 text-base-content/70">
|
||||
{MvWeb.Translations.FieldTypes.label(@custom_field.value_type)}
|
||||
</div>
|
||||
<label class="label">
|
||||
<span class="label-text-alt text-base-content/70">
|
||||
{gettext("Value type cannot be changed after creation")}
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
<% else %>
|
||||
<%!-- Show value_type as select when creating --%>
|
||||
<.input
|
||||
field={@form[:value_type]}
|
||||
type="select"
|
||||
label={gettext("Value type")}
|
||||
options={
|
||||
Ash.Resource.Info.attribute(Mv.Membership.CustomField, :value_type).constraints[
|
||||
:one_of
|
||||
]
|
||||
|> Enum.map(fn type -> {MvWeb.Translations.FieldTypes.label(type), type} end)
|
||||
}
|
||||
/>
|
||||
<% end %>
|
||||
|
||||
<.input field={@form[:description]} type="text" label={gettext("Description")} />
|
||||
<.input field={@form[:required]} type="checkbox" label={gettext("Required")} />
|
||||
<.input
|
||||
|
|
@ -85,8 +106,16 @@ defmodule MvWeb.CustomFieldLive.FormComponent do
|
|||
|
||||
@impl true
|
||||
def handle_event("validate", %{"custom_field" => custom_field_params}, socket) do
|
||||
# Remove value_type from params when editing (it's immutable after creation)
|
||||
cleaned_params =
|
||||
if socket.assigns[:custom_field] do
|
||||
Map.delete(custom_field_params, "value_type")
|
||||
else
|
||||
custom_field_params
|
||||
end
|
||||
|
||||
{:noreply,
|
||||
assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, custom_field_params))}
|
||||
assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, cleaned_params))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
|
@ -94,7 +123,15 @@ defmodule MvWeb.CustomFieldLive.FormComponent do
|
|||
# Actor must be passed from parent (IndexComponent); component socket has no current_user
|
||||
actor = socket.assigns[:actor]
|
||||
|
||||
case MvWeb.LiveHelpers.submit_form(socket.assigns.form, custom_field_params, actor) do
|
||||
# Remove value_type from params when editing (it's immutable after creation)
|
||||
cleaned_params =
|
||||
if socket.assigns[:custom_field] do
|
||||
Map.delete(custom_field_params, "value_type")
|
||||
else
|
||||
custom_field_params
|
||||
end
|
||||
|
||||
case MvWeb.LiveHelpers.submit_form(socket.assigns.form, cleaned_params, actor) do
|
||||
{:ok, custom_field} ->
|
||||
action =
|
||||
case socket.assigns.form.source.type do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue