Skip to content

Commit

Permalink
gitignore redefined and some function changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav1924 committed Jul 31, 2024
1 parent 536f9b2 commit c13d488
Show file tree
Hide file tree
Showing 15 changed files with 533 additions and 88 deletions.
441 changes: 441 additions & 0 deletions .elixir-tools/next-ls.log

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions .github/workflows/check-format-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ jobs:
elixir-version: '1.14'
otp-version: '25'

- name: Debug Environment
run: |
mix --version
elixir --version
otp --version
env
- name: Install dependencies
run: mix deps.get

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ polly-*.tar
npm-debug.log
/assets/node_modules/

.elixir_ls/
*.plt
credo_report/
19 changes: 9 additions & 10 deletions lib/polly/polls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ defmodule Polly.Polls do
end

def create_poll(params) do
Poll.changeset(%Poll{},params)
|> Ecto.Changeset.apply_action(:insert)
|> case do
{:ok, poll} ->
:ets.insert(:polls, {poll.id, poll})
{:ok, poll}
Poll.changeset(%Poll{}, params)
|> Ecto.Changeset.apply_action(:insert)
|> case do
{:ok, poll} ->
:ets.insert(:polls, {poll.id, poll})
{:ok, poll}

{:error, changeset} ->
{:error, changeset}
{:error, changeset} ->
{:error, changeset}
end
end
end


defp do_create_poll({:ok, %Poll{} = poll}) do
:ok = Polly.PollsManager.add_poll(poll)
Expand Down
26 changes: 12 additions & 14 deletions lib/polly/polls_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Polly.PollsManager do
# alias Polly.StorageBehaviour

@storage_module Application.compile_env(:polly, :storage_module, Polly.ETSStorage)

def init() do
@storage_module.init()
end
Expand Down Expand Up @@ -31,9 +32,11 @@ defmodule Polly.PollsManager do

@spec get_poll!(binary(), boolean()) :: Poll.t()
def get_poll!(poll_id, with_option_votes \\ false) do
@storage_module.get_poll!(poll_id, with_option_votes)
|> Map.replace(:total_votes, get_poll_votes!(poll_id))
|> replace_option_votes(with_option_votes)
case @storage_module.get_poll!(poll_id, with_option_votes) do
nil -> raise ArgumentError, message: "Poll with ID #{poll_id} not found"
poll -> Map.replace(poll, :total_votes, get_poll_votes!(poll_id))
|> replace_option_votes(with_option_votes)
end
end

@spec get_poll_simple!(binary()) :: Poll.t()
Expand Down Expand Up @@ -72,21 +75,16 @@ defmodule Polly.PollsManager do
end

@spec update_poll(binary(), Poll.t()) :: :ok | {:error, atom()}
def update_poll(poll_id, %Poll{} = updated_poll) do
if @storage_module.get_poll!(poll_id) do
@storage_module.update_poll(poll_id, updated_poll)
else
{:error, :poll_not_found}
end
def update_poll(poll_id, %Poll{} = updated_poll) do
if @storage_module.get_poll!(poll_id) do
@storage_module.update_poll(poll_id, updated_poll)
else
{:error, :poll_not_found}
end
end

@spec change_poll(Poll.t(), map()) :: Ecto.Changeset.t()
def change_poll(%Poll{} = poll, attrs \\ %{}) do
Poll.changeset(poll, attrs)
end

@spec update_poll(Poll.t()) :: :ok | {:error, any()}
def update_poll(%Poll{} = _poll) do
# Your ETS or other storage logic to update the poll
end
end
1 change: 0 additions & 1 deletion lib/polly/schema/poll.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ defmodule Polly.Schema.Poll do
def has_option?(%Poll{options: options}, option_id) do
Enum.any?(options, fn option -> option.id == option_id end)
end

end
3 changes: 1 addition & 2 deletions lib/polly/storage/ets_storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ defmodule Polly.ETSStorage do
|> Map.replace(:total_votes, get_poll_votes!(poll_id))
|> replace_option_votes(with_option_votes)

[] ->
raise ArgumentError, message: "Poll with ID #{poll_id} not found"
[] -> nil
end
end

Expand Down
30 changes: 15 additions & 15 deletions lib/polly_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule PollyWeb.CoreComponents do
<.icon name="hero-x-mark-solid" class="h-5 w-5" />
</button>
</div>
<div id={"#{@id}-content"}>
<%= render_slot(@inner_block) %>
</div>
Expand Down Expand Up @@ -124,9 +124,9 @@ defmodule PollyWeb.CoreComponents do
<.icon :if={@kind == :info} name="hero-information-circle-mini" class="h-4 w-4" />
<.icon :if={@kind == :error} name="hero-exclamation-circle-mini" class="h-4 w-4" /> <%= @title %>
</p>
<p class="mt-2 text-sm leading-5"><%= msg %></p>
<button type="button" class="group absolute top-1 right-1 p-2" aria-label={gettext("close")}>
<.icon name="hero-x-mark-solid" class="h-5 w-5 opacity-40 group-hover:opacity-70" />
</button>
Expand Down Expand Up @@ -290,7 +290,7 @@ defmodule PollyWeb.CoreComponents do
{@rest}
/> <%= @label %>
</label>
<.error :for={msg <- @errors}><%= msg %></.error>
</div>
"""
Expand All @@ -300,7 +300,7 @@ defmodule PollyWeb.CoreComponents do
~H"""
<div phx-feedback-for={@name}>
<.label for={@id}><%= @label %></.label>
<select
id={@id}
name={@name}
Expand All @@ -309,9 +309,9 @@ defmodule PollyWeb.CoreComponents do
{@rest}
>
<option :if={@prompt} value=""><%= @prompt %></option>
<%= Phoenix.HTML.Form.options_for_select(@options, @value) %>
<%= Phoenix.HTML.Form.options_for_select(@options, @value) %>
</select>
<.error :for={msg <- @errors}><%= msg %></.error>
</div>
"""
Expand All @@ -321,7 +321,7 @@ defmodule PollyWeb.CoreComponents do
~H"""
<div phx-feedback-for={@name}>
<.label for={@id}><%= @label %></.label>
<textarea
<textarea
id={@id}
name={@name}
class={[
Expand All @@ -342,7 +342,7 @@ defmodule PollyWeb.CoreComponents do
~H"""
<div phx-feedback-for={@name}>
<.label for={@id}><%= @label %></.label>
<input
type={@type}
name={@name}
Expand Down Expand Up @@ -406,12 +406,12 @@ defmodule PollyWeb.CoreComponents do
<h1 class="text-xl font-semibold leading-8 text-zinc-800">
<%= render_slot(@inner_block) %>
</h1>
<p :if={@subtitle != []} class="mt-2 text-md leading-6 text-zinc-600">
<%= render_slot(@subtitle) %>
</p>
</div>
<div class="flex-none"><%= render_slot(@actions) %></div>
</header>
"""
Expand Down Expand Up @@ -454,11 +454,11 @@ defmodule PollyWeb.CoreComponents do
<thead class="text-sm text-left leading-6 text-zinc-500">
<tr>
<th :for={col <- @col} class="p-0 pr-6 pb-4 font-normal"><%= col[:label] %></th>
<th class="relative p-0 pb-4"><span class="sr-only"><%= gettext("Actions") %></span></th>
</tr>
</thead>
<tbody
id={@id}
phx-update={match?(%Phoenix.LiveView.LiveStream{}, @rows) && "stream"}
Expand All @@ -477,7 +477,7 @@ defmodule PollyWeb.CoreComponents do
</span>
</div>
</td>
<td :if={@action != []} class="relative w-14 p-0">
<div class="relative whitespace-nowrap py-4 text-right text-sm font-medium">
<span class="absolute -inset-y-px -right-4 left-0 group-hover:bg-zinc-50 sm:rounded-r-xl" />
Expand Down Expand Up @@ -516,7 +516,7 @@ defmodule PollyWeb.CoreComponents do
<dl class="-my-4 divide-y divide-zinc-100">
<div :for={item <- @item} class="flex gap-4 py-4 text-sm leading-6 sm:gap-8">
<dt class="w-1/4 flex-none text-zinc-500"><%= item.title %></dt>
<dd class="text-zinc-700"><%= render_slot(item) %></dd>
</div>
</dl>
Expand Down
12 changes: 6 additions & 6 deletions lib/polly_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<html>
<head>
<title>Polly</title>
<link rel="stylesheet" href="/assets/css/styles.css" />
<link rel="stylesheet" href="/assets/css/styles.css" />
</head>

<body>
<header class="px-4 sm:px-6 lg:px-8">
<div class="flex items-center justify-between border-b border-zinc-100 py-3 text-sm">
Expand All @@ -13,7 +13,7 @@
Polly
</a>
</div>

<div class="flex items-center gap-4 font-semibold leading-6 text-zinc-900">
<%= if assigns[:current_user] do %>
<.link
Expand All @@ -31,7 +31,7 @@
</div>
</div>
</header>

<main class="px-4 py-20 sm:px-6 lg:px-8">
<div class="mx-auto max-w-2xl">
<.flash_group flash={@flash} />
Expand All @@ -43,7 +43,7 @@
<.link navigate={~p"/polls/#{poll.id}/edit"} class="button-link">
<.button class="edit-button">Edit</.button>
</.link>

<div class="sr-only">
<.link navigate={~p"/polls/#{poll.id}"} class="text-blue-600 hover:underline">
Show
Expand All @@ -52,7 +52,7 @@
</div>
<% end %>
<% end %>
<%= @inner_content %>
<%= @inner_content %>
</div>
</div>
</main>
Expand Down
4 changes: 2 additions & 2 deletions lib/polly_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<.live_title suffix=" · Phoenix Framework">
<%= assigns[:page_title] || "Polly" %>
</.live_title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>

<body class="bg-white antialiased">
<%= @inner_content %>
</body>
Expand Down
55 changes: 27 additions & 28 deletions lib/polly_web/live/poll_live/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,44 @@ defmodule PollyWeb.PollLive.Edit do
end
end


defp get_user_by_token(user_token) do
case :ets.lookup(:users, user_token) do
[{^user_token, user}] -> user
[] -> nil
end
end

@impl true
def handle_params(%{"id" => id}, _url, socket) do
case Polls.get_poll!(id) do
%Poll{} = poll ->
changeset = Polls.change_poll(poll)
{:noreply, assign(socket, poll: poll, changeset: changeset, form: to_form(changeset))}

_ ->
{:noreply,
socket
|> put_flash(:error, "Poll not found")
|> push_redirect(to: "/polls")}
@impl true
def handle_params(%{"id" => id}, _url, socket) do
case Polls.get_poll!(id) do
%Poll{} = poll ->
changeset = Polls.change_poll(poll)
{:noreply, assign(socket, poll: poll, changeset: changeset, form: to_form(changeset))}

_ ->
{:noreply,
socket
|> put_flash(:error, "Poll not found")
|> push_redirect(to: "/polls")}
end
end
end

@impl true
def handle_event("save", %{"poll" => poll_params}, socket) do
case Polls.update_poll(socket.assigns.poll, poll_params) do
{:ok, poll} ->
socket =
socket
|> put_flash(:info, "Poll updated successfully")
|> assign(:poll, poll)
|> push_redirect(to: "/polls")
@impl true
def handle_event("save", %{"poll" => poll_params}, socket) do
case Polls.update_poll(socket.assigns.poll, poll_params) do
{:ok, poll} ->
socket =
socket
|> put_flash(:info, "Poll updated successfully")
|> assign(:poll, poll)
|> push_redirect(to: "/polls")

{:noreply, socket}
{:noreply, socket}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset, form: to_form(changeset))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset, form: to_form(changeset))}
end
end
end

@impl true
def handle_event("add-option", _, socket) do
Expand Down Expand Up @@ -105,7 +104,7 @@ end
/>
<fieldset>
<legend>Options</legend>
<%= hidden_input(@form, :options, value: "[]") %>
<%= hidden_input(@form, :options, value: "[]") %>
<%= for option_form <- inputs_for(@form, :options) do %>
<div class="m-4">
<%= hidden_inputs_for(option_form) %>
Expand Down
2 changes: 1 addition & 1 deletion lib/polly_web/live/poll_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule PollyWeb.PollLive.FormComponent do
<.input field={@form[:description]} type="textarea" label="Description" />
<fieldset>
<legend>Options</legend>
<%= hidden_input(@form, :options, value: "[]") %>
<%= hidden_input(@form, :options, value: "[]") %>
<%= for f_option <- inputs_for(@form, :options) do %>
<div class="m-4">
<%= hidden_inputs_for(f_option) %> <.input field={f_option[:text]} type="text" />
Expand Down
6 changes: 3 additions & 3 deletions lib/polly_web/live/poll_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ defmodule PollyWeb.PollLive.Index do

_ ->
{:noreply,
socket
|> put_flash(:error, "Poll not found")
|> push_redirect(to: socket.assigns.redirect_path || "/polls")}
socket
|> put_flash(:error, "Poll not found")
|> push_redirect(to: socket.assigns.redirect_path || "/polls")}
end
end

Expand Down
Loading

0 comments on commit c13d488

Please sign in to comment.