add invite model
This commit is contained in:
		
							
								
								
									
										55
									
								
								lib/cannery_web/live/invite_live/form_component.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								lib/cannery_web/live/invite_live/form_component.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| defmodule CanneryWeb.InviteLive.FormComponent do | ||||
|   use CanneryWeb, :live_component | ||||
|  | ||||
|   alias Cannery.Invites | ||||
|  | ||||
|   @impl true | ||||
|   def update(%{invite: invite} = assigns, socket) do | ||||
|     changeset = Invites.change_invite(invite) | ||||
|  | ||||
|     {:ok, | ||||
|      socket | ||||
|      |> assign(assigns) | ||||
|      |> assign(:changeset, changeset)} | ||||
|   end | ||||
|  | ||||
|   @impl true | ||||
|   def handle_event("validate", %{"invite" => invite_params}, socket) do | ||||
|     changeset = | ||||
|       socket.assigns.invite | ||||
|       |> Invites.change_invite(invite_params) | ||||
|       |> Map.put(:action, :validate) | ||||
|  | ||||
|     {:noreply, assign(socket, :changeset, changeset)} | ||||
|   end | ||||
|  | ||||
|   def handle_event("save", %{"invite" => invite_params}, socket) do | ||||
|     save_invite(socket, socket.assigns.action, invite_params) | ||||
|   end | ||||
|  | ||||
|   defp save_invite(socket, :edit, invite_params) do | ||||
|     case Invites.update_invite(socket.assigns.invite, invite_params) do | ||||
|       {:ok, _invite} -> | ||||
|         {:noreply, | ||||
|          socket | ||||
|          |> put_flash(:info, "Invite updated successfully") | ||||
|          |> push_redirect(to: socket.assigns.return_to)} | ||||
|  | ||||
|       {:error, %Ecto.Changeset{} = changeset} -> | ||||
|         {:noreply, assign(socket, :changeset, changeset)} | ||||
|     end | ||||
|   end | ||||
|  | ||||
|   defp save_invite(socket, :new, invite_params) do | ||||
|     case Invites.create_invite(socket.assigns.current_user, invite_params) do | ||||
|       {:ok, _invite} -> | ||||
|         {:noreply, | ||||
|          socket | ||||
|          |> put_flash(:info, "Invite created successfully") | ||||
|          |> push_redirect(to: socket.assigns.return_to)} | ||||
|  | ||||
|       {:error, %Ecto.Changeset{} = changeset} -> | ||||
|         {:noreply, assign(socket, changeset: changeset)} | ||||
|     end | ||||
|   end | ||||
| end | ||||
							
								
								
									
										14
									
								
								lib/cannery_web/live/invite_live/form_component.html.leex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								lib/cannery_web/live/invite_live/form_component.html.leex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| <h2><%= @title %></h2> | ||||
|  | ||||
| <%= f = form_for @changeset, "#", | ||||
|   id: "invite-form", | ||||
|   phx_target: @myself, | ||||
|   phx_change: "validate", | ||||
|   phx_submit: "save" %> | ||||
|  | ||||
|   <%= label f, :name, class: "title text-lg text-primary-500" %> | ||||
|   <%= text_input f, :name, class: "input input-primary" %> | ||||
|   <%= error_tag f, :name %> | ||||
|  | ||||
|   <%= submit "Save", phx_disable_with: "Saving..." %> | ||||
| </form> | ||||
							
								
								
									
										46
									
								
								lib/cannery_web/live/invite_live/index.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								lib/cannery_web/live/invite_live/index.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,46 @@ | ||||
| defmodule CanneryWeb.InviteLive.Index do | ||||
|   use CanneryWeb, :live_view | ||||
|  | ||||
|   alias Cannery.Invites | ||||
|   alias Cannery.Invites.Invite | ||||
|  | ||||
|   @impl true | ||||
|   def mount(_params, session, socket) do | ||||
|     {:ok, socket |> assign_defaults(session) |> assign(invites: list_invites())} | ||||
|   end | ||||
|  | ||||
|   @impl true | ||||
|   def handle_params(params, _url, socket) do | ||||
|     {:noreply, apply_action(socket, socket.assigns.live_action, params)} | ||||
|   end | ||||
|  | ||||
|   defp apply_action(socket, :edit, %{"id" => id}) do | ||||
|     socket | ||||
|     |> assign(:page_title, "Edit Invite") | ||||
|     |> assign(:invite, Invites.get_invite!(id)) | ||||
|   end | ||||
|  | ||||
|   defp apply_action(socket, :new, _params) do | ||||
|     socket | ||||
|     |> assign(:page_title, "New Invite") | ||||
|     |> assign(:invite, %Invite{}) | ||||
|   end | ||||
|  | ||||
|   defp apply_action(socket, :index, _params) do | ||||
|     socket | ||||
|     |> assign(:page_title, "Listing Invites") | ||||
|     |> assign(:invite, nil) | ||||
|   end | ||||
|  | ||||
|   @impl true | ||||
|   def handle_event("delete", %{"id" => id}, socket) do | ||||
|     invite = Invites.get_invite!(id) | ||||
|     {:ok, _} = Invites.delete_invite(invite) | ||||
|  | ||||
|     {:noreply, assign(socket, :invites, list_invites())} | ||||
|   end | ||||
|  | ||||
|   defp list_invites do | ||||
|     Invites.list_invites() | ||||
|   end | ||||
| end | ||||
							
								
								
									
										38
									
								
								lib/cannery_web/live/invite_live/index.html.leex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								lib/cannery_web/live/invite_live/index.html.leex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | ||||
| <h1>Listing Invites</h1> | ||||
|  | ||||
| <%= if @live_action in [:new, :edit] do %> | ||||
|   <%= live_modal CanneryWeb.InviteLive.FormComponent, | ||||
|     id: @invite.id || :new, | ||||
|     title: @page_title, | ||||
|     action: @live_action, | ||||
|     invite: @invite, | ||||
|     return_to: Routes.invite_index_path(@socket, :index) %> | ||||
| <% end %> | ||||
|  | ||||
| <table> | ||||
|   <thead> | ||||
|     <tr> | ||||
|       <th>Name</th> | ||||
|       <th>Token</th> | ||||
|       <th>Uses left</th> | ||||
|       <th></th> | ||||
|     </tr> | ||||
|   </thead> | ||||
|   <tbody id="invites"> | ||||
|     <%= for invite <- @invites do %> | ||||
|       <tr id="invite-<%= invite.id %>"> | ||||
|         <td><%= invite.name %></td> | ||||
|         <td><%= invite.token %></td> | ||||
|         <td><%= invite.uses_left || "Unlimited" %></td> | ||||
|  | ||||
|         <td> | ||||
|           <span><%= live_redirect "Show", to: Routes.invite_show_path(@socket, :show, invite) %></span> | ||||
|           <span><%= live_patch "Edit", to: Routes.invite_index_path(@socket, :edit, invite) %></span> | ||||
|           <span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: invite.id, data: [confirm: "Are you sure?"] %></span> | ||||
|         </td> | ||||
|       </tr> | ||||
|     <% end %> | ||||
|   </tbody> | ||||
| </table> | ||||
|  | ||||
| <span><%= live_patch "New Invite", to: Routes.invite_index_path(@socket, :new) %></span> | ||||
							
								
								
									
										21
									
								
								lib/cannery_web/live/invite_live/show.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								lib/cannery_web/live/invite_live/show.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| defmodule CanneryWeb.InviteLive.Show do | ||||
|   use CanneryWeb, :live_view | ||||
|  | ||||
|   alias Cannery.Invites | ||||
|  | ||||
|   @impl true | ||||
|   def mount(_params, session, socket) do | ||||
|     {:ok, socket |> assign_defaults(session)} | ||||
|   end | ||||
|  | ||||
|   @impl true | ||||
|   def handle_params(%{"id" => id}, _, socket) do | ||||
|     {:noreply, | ||||
|      socket | ||||
|      |> assign(:page_title, page_title(socket.assigns.live_action)) | ||||
|      |> assign(:invite, Invites.get_invite!(id))} | ||||
|   end | ||||
|  | ||||
|   defp page_title(:show), do: "Show Invite" | ||||
|   defp page_title(:edit), do: "Edit Invite" | ||||
| end | ||||
							
								
								
									
										27
									
								
								lib/cannery_web/live/invite_live/show.html.leex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								lib/cannery_web/live/invite_live/show.html.leex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| <h1>Show Invite</h1> | ||||
|  | ||||
| <%= if @live_action in [:edit] do %> | ||||
|   <%= live_modal CanneryWeb.InviteLive.FormComponent, | ||||
|     id: @invite.id, | ||||
|     title: @page_title, | ||||
|     action: @live_action, | ||||
|     invite: @invite, | ||||
|     return_to: Routes.invite_show_path(@socket, :show, @invite) %> | ||||
| <% end %> | ||||
|  | ||||
| <ul> | ||||
|  | ||||
|   <li> | ||||
|     <strong>Name:</strong> | ||||
|     <%= @invite.name %> | ||||
|   </li> | ||||
|  | ||||
|   <li> | ||||
|     <strong>Token:</strong> | ||||
|     <%= @invite.token %> | ||||
|   </li> | ||||
|  | ||||
| </ul> | ||||
|  | ||||
| <span><%= live_patch "Edit", to: Routes.invite_show_path(@socket, :edit, @invite), class: "button" %></span> | ||||
| <span><%= live_redirect "Back", to: Routes.invite_index_path(@socket, :index) %></span> | ||||
		Reference in New Issue
	
	Block a user