|  | 
| 1 | 1 | defmodule AlgoraWeb.Forms.ContractForm do | 
| 2 | 2 |   @moduledoc false | 
| 3 | 3 |   use Ecto.Schema | 
|  | 4 | +  use AlgoraWeb, :html | 
| 4 | 5 | 
 | 
| 5 | 6 |   import Ecto.Changeset | 
| 6 | 7 | 
 | 
|  | 8 | +  alias Algora.Accounts.User | 
| 7 | 9 |   alias Algora.Types.USD | 
| 8 | 10 |   alias Algora.Validations | 
| 9 | 11 | 
 | 
| 10 | 12 |   embedded_schema do | 
| 11 |  | -    field :hourly_rate, USD | 
|  | 13 | +    field :amount, USD | 
| 12 | 14 |     field :hours_per_week, :integer | 
|  | 15 | +    field :type, Ecto.Enum, values: [:fixed, :hourly], default: :fixed | 
|  | 16 | +    field :title, :string | 
|  | 17 | +    field :description, :string | 
|  | 18 | +    field :contractor_handle, :string | 
|  | 19 | + | 
|  | 20 | +    embeds_one :contractor, User | 
|  | 21 | +  end | 
|  | 22 | + | 
|  | 23 | +  def type_options do | 
|  | 24 | +    [{"Fixed", :fixed}, {"Hourly", :hourly}] | 
| 13 | 25 |   end | 
| 14 | 26 | 
 | 
| 15 | 27 |   def changeset(form, attrs) do | 
| 16 | 28 |     form | 
| 17 |  | -    |> cast(attrs, [:hourly_rate, :hours_per_week]) | 
| 18 |  | -    |> validate_required([:hourly_rate, :hours_per_week]) | 
| 19 |  | -    |> Validations.validate_money_positive(:hourly_rate) | 
| 20 |  | -    |> validate_number(:hours_per_week, greater_than: 0, less_than_or_equal_to: 40) | 
|  | 29 | +    |> cast(attrs, [:amount, :hours_per_week, :type, :title, :description, :contractor_handle]) | 
|  | 30 | +    |> validate_required([:contractor_handle]) | 
|  | 31 | +    |> validate_type_fields() | 
|  | 32 | +    |> Validations.validate_money_positive(:amount) | 
|  | 33 | +    |> Validations.validate_github_handle(:contractor_handle, :contractor) | 
|  | 34 | +  end | 
|  | 35 | + | 
|  | 36 | +  defp validate_type_fields(changeset) do | 
|  | 37 | +    case get_field(changeset, :type) do | 
|  | 38 | +      :hourly -> validate_required(changeset, [:amount, :hours_per_week]) | 
|  | 39 | +      _ -> validate_required(changeset, [:amount]) | 
|  | 40 | +    end | 
|  | 41 | +  end | 
|  | 42 | + | 
|  | 43 | +  def contract_form(assigns) do | 
|  | 44 | +    ~H""" | 
|  | 45 | +    <.form | 
|  | 46 | +      id="main-contract-form" | 
|  | 47 | +      for={@form} | 
|  | 48 | +      phx-submit="create_contract_main" | 
|  | 49 | +      phx-change="validate_contract_main" | 
|  | 50 | +    > | 
|  | 51 | +      <div class="space-y-4"> | 
|  | 52 | +        <div class="grid grid-cols-2 gap-4" phx-update="ignore" id="main-contract-form-tabs"> | 
|  | 53 | +          <%= for {label, value} <- type_options() do %> | 
|  | 54 | +            <label class={[ | 
|  | 55 | +              "group relative flex cursor-pointer rounded-lg px-3 py-2 shadow-sm focus:outline-none", | 
|  | 56 | +              "border-2 bg-background transition-all duration-200 hover:border-primary hover:bg-primary/10", | 
|  | 57 | +              "border-border has-[:checked]:border-primary has-[:checked]:bg-primary/10" | 
|  | 58 | +            ]}> | 
|  | 59 | +              <.input | 
|  | 60 | +                id={"main-contract-form-type-#{value}"} | 
|  | 61 | +                type="radio" | 
|  | 62 | +                field={@form[:type]} | 
|  | 63 | +                checked={@form[:type].value == value} | 
|  | 64 | +                value={value} | 
|  | 65 | +                class="sr-only" | 
|  | 66 | +                phx-click={ | 
|  | 67 | +                  %JS{} | 
|  | 68 | +                  |> JS.hide(to: "#main-contract-form [data-tab]:not([data-tab=#{value}])") | 
|  | 69 | +                  |> JS.show(to: "#main-contract-form [data-tab=#{value}]") | 
|  | 70 | +                } | 
|  | 71 | +              /> | 
|  | 72 | +              <span class="flex flex-1 items-center justify-between"> | 
|  | 73 | +                <span class="text-sm font-medium">{label}</span> | 
|  | 74 | +                <.icon | 
|  | 75 | +                  name="tabler-check" | 
|  | 76 | +                  class="invisible size-5 text-primary group-has-[:checked]:visible" | 
|  | 77 | +                /> | 
|  | 78 | +              </span> | 
|  | 79 | +            </label> | 
|  | 80 | +          <% end %> | 
|  | 81 | +        </div> | 
|  | 82 | +
 | 
|  | 83 | +        <.input label="Title" field={@form[:title]} placeholder="Brief description of the contract" /> | 
|  | 84 | +        <.input | 
|  | 85 | +          label="Description (optional)" | 
|  | 86 | +          field={@form[:description]} | 
|  | 87 | +          type="textarea" | 
|  | 88 | +          placeholder="Requirements and acceptance criteria" | 
|  | 89 | +        /> | 
|  | 90 | +        <.input label="Amount" icon="tabler-currency-dollar" field={@form[:amount]} /> | 
|  | 91 | +
 | 
|  | 92 | +        <div data-tab="hourly" class="hidden"> | 
|  | 93 | +          <.input label="Hours per week" field={@form[:hours_per_week]} /> | 
|  | 94 | +        </div> | 
|  | 95 | +
 | 
|  | 96 | +        <div class="relative"> | 
|  | 97 | +          <.input | 
|  | 98 | +            label="GitHub handle" | 
|  | 99 | +            field={@form[:contractor_handle]} | 
|  | 100 | +            phx-debounce="500" | 
|  | 101 | +            class="pl-10" | 
|  | 102 | +          /> | 
|  | 103 | +          <div class="pointer-events-none absolute left-0 top-9 flex items-center justify-center pl-3 h-7 w-7"> | 
|  | 104 | +            <.avatar :if={get_field(@form.source, :contractor)} class="h-7 w-7"> | 
|  | 105 | +              <.avatar_image src={get_field(@form.source, :contractor).avatar_url} /> | 
|  | 106 | +            </.avatar> | 
|  | 107 | +            <.icon name="github" class="h-7 w-7 text-muted-foreground" /> | 
|  | 108 | +          </div> | 
|  | 109 | +        </div> | 
|  | 110 | +      </div> | 
|  | 111 | +      <div class="pt-4 ml-auto flex gap-4"> | 
|  | 112 | +        <.button variant="secondary" phx-click="close_share_drawer" type="button"> | 
|  | 113 | +          Cancel | 
|  | 114 | +        </.button> | 
|  | 115 | +        <.button type="submit"> | 
|  | 116 | +          Share Contract <.icon name="tabler-arrow-right" class="-mr-1 ml-2 h-4 w-4" /> | 
|  | 117 | +        </.button> | 
|  | 118 | +      </div> | 
|  | 119 | +    </.form> | 
|  | 120 | +    """ | 
| 21 | 121 |   end | 
| 22 | 122 | end | 
0 commit comments