Skip to content

Commit

Permalink
Merge branch 'main' into ff/add-bosts
Browse files Browse the repository at this point in the history
  • Loading branch information
ruilopesm committed Mar 28, 2023
2 parents 9be17c1 + 1107d70 commit d31a7de
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
erlang 25.0
erlang 25.1.2
elixir 1.13.4-otp-24
nodejs 16.15.0
2 changes: 1 addition & 1 deletion lib/parzival/gamification/mission.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Parzival.Gamification.Mission do

belongs_to :difficulty, Difficulty

has_many :tasks, Task, on_replace: :delete_if_exists
has_many :tasks, Task, on_replace: :delete

many_to_many :users, User, join_through: MissionUser, preload_order: [desc: :inserted_at]

Expand Down
19 changes: 17 additions & 2 deletions lib/parzival/gamification/mission/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule Parzival.Gamification.Mission.Task do

@optional_fields [
:tokens,
:exp
:exp,
:delete
]

schema "tasks" do
Expand All @@ -22,9 +23,11 @@ defmodule Parzival.Gamification.Mission.Task do
field :tokens, :integer
field :exp, :integer

field :delete, :boolean, virtual: true

belongs_to :mission, Mission

many_to_many :users, User, join_through: TaskUser
many_to_many :users, User, join_through: TaskUser, on_delete: :delete_all

timestamps()
end
Expand All @@ -34,11 +37,23 @@ defmodule Parzival.Gamification.Mission.Task do
task
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> maybe_mark_for_deletion()
end

def task_changeset(task, attrs) do
task
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields -- [:mission_id])
|> maybe_mark_for_deletion()
end

defp maybe_mark_for_deletion(%{data: %{id: nil}} = changeset), do: changeset

defp maybe_mark_for_deletion(changeset) do
if get_change(changeset, :delete) do
%{changeset | action: :delete}
else
changeset
end
end
end
10 changes: 5 additions & 5 deletions lib/parzival_web/live/app/company_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
<div class="flex items-center">
<div>
<%= if is_nil(Uploaders.ProfilePicture.url({company.picture, company}, :medium)) do %>
<div class="relative flex-shrink-0 rounded-full ring-4 ring-white min-w-24 min-h-24 sm:min-w-32 sm:min-h-32">
<span class="inline-flex justify-center items-center w-24 h-24 rounded-full sm:w-32 sm:h-32 bg-secondary">
<div class="relative flex-shrink-0 rounded-2xl ring-4 ring-white min-w-24 min-h-24 sm:min-w-32 sm:min-h-32">
<span class="inline-flex justify-center items-center w-24 h-24 rounded-2xl sm:w-32 sm:h-32 bg-secondary">
<span class="text-4xl font-medium leading-none text-white select-none sm:text-5xl">
<%= extract_initials(company.name) %>
</span>
</span>
</div>
<% else %>
<div class="relative flex-shrink-0 w-24 h-24 bg-white rounded-full ring-4 ring-white sm:w-32 sm:h-32">
<img src={Uploaders.ProfilePicture.url({company.picture, company}, :medium)} class="object-center absolute rounded-full w-[98px] h-[98px] sm:w-[130px] sm:h-[130px]" />
<div class="relative flex-shrink-0 w-24 h-24 bg-white rounded-2xl ring-4 ring-white sm:w-32 sm:h-32">
<img src={Uploaders.ProfilePicture.url({company.picture, company}, :medium)} class="object-center absolute rounded-2xl w-[98px] h-[98px] sm:w-[130px] sm:h-[130px]" />
</div>
<% end %>
</div>
Expand All @@ -58,7 +58,7 @@
</div>
</div>
<div>
<span class={"inline-flex items-center justify-center px-2 text-xs font-semibold leading-5 border #{border_200(company.level.color)} #{bg_100(company.level.color)} rounded-full"}>
<span class={"inline-flex items-center justify-center px-2 text-xs font-semibold leading-5 border #{border_200(company.level.color)} #{bg_100(company.level.color)} rounded-2xl"}>
<%= company.level.name %>
</span>
</div>
Expand Down
8 changes: 4 additions & 4 deletions lib/parzival_web/live/app/company_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
<div class="flex flex-col gap-2 mt-6 sm:flex-row sm:items-center">
<div>
<%= if is_nil(Uploaders.ProfilePicture.url({@company.picture, @company}, :medium)) do %>
<div class="relative flex-shrink-0 rounded-full min-w-24 min-h-24 sm:min-w-32 sm:min-h-32">
<span class="inline-flex justify-center items-center w-24 h-24 rounded-full sm:w-32 sm:h-32 bg-secondary">
<div class="relative flex-shrink-0 rounded-2xl min-w-24 min-h-24 sm:min-w-32 sm:min-h-32">
<span class="inline-flex justify-center items-center w-24 h-24 rounded-2xl sm:w-32 sm:h-32 bg-secondary">
<span class="text-4xl font-medium leading-none text-white select-none sm:text-5xl">
<%= extract_initials(@company.name) %>
</span>
</span>
</div>
<% else %>
<div class="relative flex-shrink-0 w-24 h-24 bg-white rounded-full sm:w-32 sm:h-32">
<img src={Uploaders.ProfilePicture.url({@company.picture, @company}, :medium)} class="object-center absolute rounded-full w-[98px] h-[98px] sm:w-[130px] sm:h-[130px]" />
<div class="relative flex-shrink-0 w-24 h-24 bg-white rounded-2xl sm:w-32 sm:h-32">
<img src={Uploaders.ProfilePicture.url({@company.picture, @company}, :medium)} class="object-center absolute rounded-2xl w-[98px] h-[98px] sm:w-[130px] sm:h-[130px]" />
</div>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/parzival_web/live/app/jobs/offer_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<li>
<%= live_redirect to: Routes.offer_show_path(@socket, :show, offer), class: "block hover:bg-gray-50 border-b border-gray-200" do %>
<div class="flex flex-row py-4 px-4 sm:px-6">
<div class="inline-flex justify-center items-center h-10 rounded-full lg:h-16 aspect-square bg-secondary">
<div class="inline-flex justify-center items-center h-10 rounded-2xl lg:h-16 aspect-square bg-secondary">
<span class="text-xl font-medium leading-none text-white md:text-2xl">
<%= extract_initials(offer.company.name) %>
</span>
Expand Down
4 changes: 2 additions & 2 deletions lib/parzival_web/live/app/jobs/offer_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@
<div>
<%= if is_nil(@offer.company.picture) do %>
<div class="relative flex-shrink-0 rounded-full ring-4 ring-white min-w-24 min-h-24 sm:min-w-32 sm:min-h-32">
<span class="inline-flex justify-center items-center w-24 h-24 rounded-full sm:w-32 sm:h-32 bg-secondary">
<span class="inline-flex justify-center items-center w-24 h-24 rounded-2xl sm:w-32 sm:h-32 bg-secondary">
<span class="text-4xl font-medium leading-none text-white select-none sm:text-5xl">
<%= extract_initials(@offer.company.name) %>
</span>
</span>
</div>
<% else %>
<div class="relative flex-shrink-0 w-24 h-24 bg-white rounded-full ring-4 ring-white sm:w-32 sm:h-32">
<div class="relative flex-shrink-0 w-24 h-24 bg-white rounded-2xl ring-4 ring-white sm:w-32 sm:h-32">
<img src={Parzival.Uploaders.ProfilePicture.url({@offer.company.picture, @offer.company}, :medium)} class="object-center absolute rounded-full w-[98px] h-[98px] sm:w-[130px] sm:h-[130px]" />
</div>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,30 @@ defmodule ParzivalWeb.Backoffice.MissionLive.FormComponent do
end

def handle_event("add-task", _, socket) do
existing_tasks =
Map.get(
socket.assigns.changeset.changes,
:tasks,
socket.assigns.mission.tasks
)
changeset = socket.assigns.changeset

tasks =
existing_tasks
changeset
|> Ecto.Changeset.get_field(:tasks)
|> Enum.concat([Gamification.change_task(%Task{})])

changeset =
socket.assigns.changeset
changeset
|> Ecto.Changeset.put_assoc(:tasks, tasks)

{:noreply, assign(socket, changeset: changeset)}
end

def handle_event("rm-task", %{"index" => index}, socket) do
changeset = socket.assigns.changeset

tasks =
changeset
|> Ecto.Changeset.get_field(:tasks)
|> List.delete_at(String.to_integer(index))

changeset =
changeset
|> Ecto.Changeset.put_assoc(:tasks, tasks)

{:noreply, assign(socket, changeset: changeset)}
Expand Down Expand Up @@ -81,6 +92,13 @@ defmodule ParzivalWeb.Backoffice.MissionLive.FormComponent do
end

defp save_mission(socket, :new, mission_params) do
tasks =
mission_params
|> Map.get("tasks")
|> Enum.map(fn {_id, task} -> Map.drop(task, ["id", "mission_id"]) end)

mission_params = Map.replace!(mission_params, "tasks", tasks)

case Gamification.create_mission(mission_params) do
{:ok, _mission} ->
{:noreply,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,50 +106,67 @@
</div>
</div>
<div clas="flex flex-col">
<%= for {fs, index} <- Enum.with_index(inputs_for(f, :tasks)) do %>
<%= hidden_input(fs, :mission_id) %>
<div class="flex flex-col gap-2 py-3 px-4 border-b border-gray-200 sm:py-4 sm:px-6 lg:px-8">
<span class="text-lg font-bold sm:text-xl">
Task <%= index + 1 %>
</span>
<div>
<%= text_input(fs, :title, placeholder: "Choose title", phx_debounce: "blur", class: "leading-7 text-gray-700 text-base sm:text-lg sm:truncate mt-1 font-bold focus:ring-red-500 focus:border-red-500 block w-full shadow-sm border-gray-300 rounded-md") %>
<div class="flex text-red-500">
<%= error_tag(fs, :title) %>
<%= inputs_for f, :tasks, fn fs -> %>
<div x-bind:class="! show ? 'hidden' : 'block space-y-2'" x-data="{ show: true }">
<%= hidden_input(fs, :id) %>
<%= hidden_input(fs, :mission_id) %>
<div class="flex flex-col gap-2 py-3 px-4 border-b border-gray-200 sm:py-4 sm:px-6 lg:px-8">
<div class="flex flex-row justify-between">
<span class="text-lg font-bold sm:text-xl">
Task <%= fs.index + 1 %>
</span>
<%= if fs.index != 0 do %>
<%= if is_nil(fs.data.id) do %>
<a phx-click="rm-task" phx-target={@myself} phx-value-index={fs.index} class="justify-center py-2 px-4 text-sm font-medium bg-white rounded-md border border-gray-300 shadow-sm cursor-pointer hover:bg-gray-50 text-primary">
Delete
</a>
<% else %>
<a onclick={"document.getElementById('#{fs.data.id}').click()"} x-on:click="show = false" class="justify-center py-2 px-4 text-sm font-medium bg-white rounded-md border border-gray-300 shadow-sm cursor-pointer hover:bg-gray-50 text-primary">
Delete
</a>
<%= checkbox(fs, :delete, id: fs.data.id, class: "hidden") %>
<% end %>
<% end %>
</div>
</div>
<div class="flex flex-col gap-y-1">
<span class="pl-1 text-sm font-medium text-gray-800 sm:text-base">
Description
</span>
<span class="text-sm text-gray-700 line-clamp-3">
<%= textarea(fs, :description, placeholder: "Choose description", rows: 15, phx_debounce: "blur", class: "focus:ring-red-500 h-24 font-medium focus:border-red-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md") %>
</span>
<div class="flex text-red-500">
<%= error_tag(fs, :description) %>
<div>
<%= text_input(fs, :title, placeholder: "Choose title", phx_debounce: "blur", class: "leading-7 text-gray-700 text-base sm:text-lg sm:truncate mt-1 font-bold focus:ring-red-500 focus:border-red-500 block w-full shadow-sm border-gray-300 rounded-md") %>
<div class="flex text-red-500">
<%= error_tag(fs, :title) %>
</div>
</div>
</div>
<div class="flex flex-col gap-y-1">
<span class="pl-1 text-sm font-medium text-gray-800 sm:text-base">
Rewards
</span>
<div class="flex flex-col gap-4 sm:flex-row">
<div class="relative">
<div class="absolute ml-3 top-[22%]">
💰
</div>
<%= number_input(fs, :tokens, placeholder: "Choose tokens", phx_debounce: "blur", class: "w-64 pl-9 leading-7 text-gray-700 sm:truncate text-sm focus:ring-red-500 focus:border-red-500 block w-full shadow-sm border-gray-300 rounded-md") %>
<div class="text-red-500">
<%= error_tag(fs, :tokens) %>
</div>
<div class="flex flex-col gap-y-1">
<span class="pl-1 text-sm font-medium text-gray-800 sm:text-base">
Description
</span>
<span class="text-sm text-gray-700 line-clamp-3">
<%= textarea(fs, :description, placeholder: "Choose description", rows: 15, phx_debounce: "blur", class: "focus:ring-red-500 h-24 font-medium focus:border-red-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md") %>
</span>
<div class="flex text-red-500">
<%= error_tag(fs, :description) %>
</div>
<div class="relative">
<div class="absolute ml-2 top-[28%] text-gray-300 text-sm">
EXP
</div>
<div class="flex flex-col gap-y-1">
<span class="pl-1 text-sm font-medium text-gray-800 sm:text-base">
Rewards
</span>
<div class="flex flex-col gap-4 sm:flex-row">
<div class="relative">
<div class="absolute ml-3 top-[22%]">
💰
</div>
<%= number_input(fs, :tokens, placeholder: "Choose tokens", phx_debounce: "blur", class: "w-64 pl-9 leading-7 text-gray-700 sm:truncate text-sm focus:ring-red-500 focus:border-red-500 block w-full shadow-sm border-gray-300 rounded-md") %>
<div class="text-red-500">
<%= error_tag(fs, :tokens) %>
</div>
</div>
<%= number_input(fs, :exp, placeholder: "Choose experience", phx_debounce: "blur", class: "w-64 pl-9 leading-7 text-gray-700 sm:truncate text-sm focus:ring-red-500 focus:border-red-500 block w-full shadow-sm border-gray-300 rounded-md") %>
<div class="text-red-500">
<%= error_tag(fs, :exp) %>
<div class="relative">
<div class="absolute ml-2 top-[28%] text-gray-300 text-sm">
EXP
</div>
<%= number_input(fs, :exp, placeholder: "Choose experience", phx_debounce: "blur", class: "w-64 pl-9 leading-7 text-gray-700 sm:truncate text-sm focus:ring-red-500 focus:border-red-500 block w-full shadow-sm border-gray-300 rounded-md") %>
<div class="text-red-500">
<%= error_tag(fs, :exp) %>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit d31a7de

Please sign in to comment.