can now set fave shift template from shift template view

This commit is contained in:
Adam Piontek 2021-03-11 16:39:02 -05:00
parent 19b9bbfb0d
commit 243ebd8aa2
5 changed files with 36 additions and 11 deletions

View file

@ -43,6 +43,8 @@ import "../node_modules/bootstrap-icons/icons/geo.svg";
import "../node_modules/bootstrap-icons/icons/justify-left.svg";
import "../node_modules/bootstrap-icons/icons/plus-circle-dotted.svg";
import "../node_modules/bootstrap-icons/icons/clipboard-plus.svg";
import "../node_modules/bootstrap-icons/icons/star.svg";
import "../node_modules/bootstrap-icons/icons/star-fill.svg";
// webpack automatically bundles all modules in your
// entry points. Those entry points can be configured

View file

@ -449,21 +449,16 @@ defmodule Shift73k.Accounts do
@doc """
Sets a shift template as a user's favorite
"""
def set_user_fave_shift_template(%User{id: user_id}, %ShiftTemplate{
id: shift_template_id,
user_id: user_id
}) do
def set_user_fave_shift_template(user_id, shift_template_id) do
User
|> where(id: ^user_id)
|> Repo.update_all(set: [fave_shift_template_id: shift_template_id])
end
def set_user_fave_shift_template(_, _), do: {0, nil}
@doc """
Clears a user's favorite shift template
"""
def unset_user_fave_shift_template(%User{id: user_id}) do
def unset_user_fave_shift_template(user_id) do
User
|> where(id: ^user_id)
|> Repo.update_all(set: [fave_shift_template_id: nil])

View file

@ -19,7 +19,7 @@ defmodule Shift73k.Accounts.User do
@derive {Inspect, except: [:password]}
@primary_key {:id, :binary_id, autogenerate: true}
# @foreign_key_type :binary_id
@foreign_key_type :binary_id
schema "users" do
field(:email, :string)
field(:password, :string, virtual: true)

View file

@ -1,6 +1,7 @@
defmodule Shift73kWeb.ShiftTemplateLive.Index do
use Shift73kWeb, :live_view
alias Shift73k.Accounts
alias Shift73k.ShiftTemplates
alias Shift73k.ShiftTemplates.ShiftTemplate
alias Shift73kWeb.Roles
@ -76,6 +77,26 @@ defmodule Shift73kWeb.ShiftTemplateLive.Index do
{:noreply, assign(socket, :delete_shift_template, ShiftTemplates.get_shift_template!(id))}
end
def handle_event("set-user-fave-shift-template", %{"id" => shift_template_id}, socket) do
user_id = socket.assigns.current_user.id
Accounts.set_user_fave_shift_template(user_id, shift_template_id)
socket
|> assign(:current_user, Accounts.get_user!(user_id))
|> assign_shift_templates()
|> live_noreply()
end
def handle_event("unset-user-fave-shift-template", _params, socket) do
user_id = socket.assigns.current_user.id
Accounts.unset_user_fave_shift_template(user_id)
socket
|> assign(:current_user, Accounts.get_user!(user_id))
|> assign_shift_templates()
|> live_noreply()
end
# @impl true
# def handle_event("delete", %{"id" => id}, socket) do
# shift_template = ShiftTemplates.get_shift_template!(id)

View file

@ -38,9 +38,14 @@
<div class="col-12 col-lg-6">
<div class="card mt-4">
<h5 class="card-header">
<span class="visually-hidden">Subject:</span>
<h5 class="card-header d-flex justify-content-between align-items-start">
<div class="visually-hidden">Subject:</div>
<%= shift.subject %>
<%= if shift.id == @current_user.fave_shift_template_id do %>
<%= icon_div @socket, "bi-star-fill", [class: "icon baseline text-primary align-self-start ms-2", phx_click: "unset-user-fave-shift-template"] %>
<% else %>
<%= icon_div @socket, "bi-star", [class: "icon baseline text-primary align-self-start ms-2", phx_click: "set-user-fave-shift-template", phx_value_id: shift.id] %>
<% end %>
</h5>
<div class="card-body">
@ -48,7 +53,7 @@
<tbody>
<tr>
<th scope="row" class="text-end">
<%= icon_div @socket, "bi-hourglass", [class: "icon baseline text-muted"] %>
<%= icon_div @socket, "bi-hourglass", [class: "icon text-muted"] %>
<span class="visually-hidden">Hours:</span>
</th>
<td>
@ -107,6 +112,8 @@
<% end %>
<% end %>
<%#= button "" %>
<%= if Roles.can?(@current_user, shift, :delete) do %>
<button class="btn btn-outline-danger btn-sm text-nowrap" phx-click="delete-modal" phx-value-id="<%= shift.id %>">
<%= icon_div @socket, "bi-trash", [class: "icon baseline", style: "margin-right:0.125rem;"] %>