Skip to content

Commit

Permalink
🗑️ add dimiss all button
Browse files Browse the repository at this point in the history
  • Loading branch information
RustySnek committed Dec 6, 2024
1 parent 48a47f6 commit d14fa89
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 44 deletions.
4 changes: 4 additions & 0 deletions assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "phoenix_html"
import { Socket } from "phoenix"
import { LiveSocket } from "phoenix_live_view"
import { discard } from './hooks/storage_hooks';
import topbar from "../vendor/topbar"
import Hooks from './hooks';

Expand All @@ -25,6 +26,9 @@ window.addEventListener('beforeunload', (event) => {
event.returnValue = ''; // This prompts the user with a confirmation dialog
}
});
window.addEventListener('phx:discard-all', e => {
discard(e.detail)
})

liveSocket.connect()
window.liveSocket = liveSocket
Expand Down
31 changes: 31 additions & 0 deletions assets/js/hooks/storage_hooks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
let StorageHooks = {};

export function discard(events) {
let discarded_items = localStorage.getItem("discards");
try {
discarded_items = discarded_items ? JSON.parse(discarded_items) : {};
} catch {
discarded_items = {};
}
if (!(discarded_items instanceof Object)) {
discarded_items = {};
}

const updatedDiscardedItems = { ...discarded_items };

Object.entries(events).forEach(([_event, item_ids]) => {
const discards = updatedDiscardedItems[_event] || [];
const newDiscards = item_ids.filter(id => !discards.includes(id));

newDiscards.forEach(id => {
const elem = document.getElementById(id);
elem.remove()
})
if (newDiscards.length > 0) {
updatedDiscardedItems[_event] = [...discards, ...newDiscards];
}
});

localStorage.setItem("discards", JSON.stringify(updatedDiscardedItems));
};

StorageHooks.set_local_storage = {
updated() {
if (this.el.value !== "") {
localStorage.setItem(this.el.name, JSON.stringify(this.el.value))
}
}
}

StorageHooks.retrieve_local_storage = {
mounted() {
let name = this.el.getAttribute("name")
Expand Down
32 changes: 3 additions & 29 deletions assets/js/hooks/style_hooks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { discard } from './storage_hooks';

let StyleHooks = {};

StyleHooks.scroll_top = {
Expand Down Expand Up @@ -56,35 +58,7 @@ StyleHooks.swipe_discard = {
setTimeout(_ => {
this.el.remove()
if (typeof event === "string") {
let discarded_items = localStorage.getItem("discards")
if (discarded_items) {
try {
discarded_items = JSON.parse(discarded_items)

} catch {
discarded_items = {}
}
}
if (!(discarded_items instanceof Object)) {
discarded_items = {}
}
const discards = discarded_items[event]
if (Array.isArray(discards) && !discards.includes(item_id)) {
localStorage.setItem("discards", JSON.stringify(
{
...discarded_items,
[event]: [item_id, ...discards]
}
))
} else {
localStorage.setItem("discards", JSON.stringify(
{
...discarded_items,
[event]: [item_id]
}

))
}
discard({ event: [item_id] })
}

}, 200)
Expand Down
2 changes: 1 addition & 1 deletion lib/elixirus_web/live/student_live/attendance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ defmodule ElixirusWeb.StudentLive.Attendance do
end)
|> create_fetcher(user_id, subject_frequency, :subject_frequency, fn ->
SnakeArgs.from_params(:elixirus, :subject_frequency, [client])
|> Venomous.python!()
|> Venomous.python!(timeout: 15_000)
end)
|> create_fetcher(user_id, attendance, :attendance, fn ->
{SnakeArgs.from_params(:elixirus, :attendance, [client, true])
Expand Down
62 changes: 50 additions & 12 deletions lib/elixirus_web/live/student_live/index.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule ElixirusWeb.StudentLive.Index do
alias ElixirusWeb.LoginForm
require Logger
alias Elixirus.Types.Message
alias Elixirus.Types.Event
Expand Down Expand Up @@ -48,6 +49,41 @@ defmodule ElixirusWeb.StudentLive.Index do
{:noreply, assign(socket, :discards, %{})}
end

def handle_event("discard_all", _params, socket) do
discards = socket.assigns.discards

message_discards =
socket.assigns.messages
|> Enum.map(&Map.get(&1, :href))
|> Enum.reject(fn msg -> msg in Map.get(discards, "message", []) end)

attendance_discards =
socket.assigns.attendance
|> Enum.map(&Map.get(&1, :href))
|> Enum.reject(fn a -> a in Map.get(discards, "attendance", []) end)

grade_discards =
socket.assigns.grades
|> Enum.map(&Map.get(&1, :href))
|> Enum.reject(fn g -> g in Map.get(discards, "grade", []) end)

announcement_discards =
socket.assigns.announcements
|> Enum.map(&(Map.get(&1, :title) <> Map.get(&1, :date)))
|> Enum.reject(fn a -> a in Map.get(discards, "announcement", []) end)

discards =
%{
:grade => grade_discards,
:message => message_discards,
:attendance => attendance_discards,
:announcement => announcement_discards
}
|> dbg

{:noreply, push_event(socket, "discard-all", discards)}
end

def handle_async(task, {:ok, {:killed, _reason}}, socket) when task in @asyncs do
{:noreply, socket}
end
Expand Down Expand Up @@ -278,7 +314,7 @@ defmodule ElixirusWeb.StudentLive.Index do
end

def mount(_params, %{"token" => token}, socket) when map_size(token) == 0 do
{:ok, setup(socket) |> assign(:page_title, "Login") |> push_event("require-login", %{})}
{:ok, setup(socket) |> assign(:page_title, "Login") |> LoginForm.require_login()}
end

def mount(
Expand Down Expand Up @@ -551,20 +587,22 @@ defmodule ElixirusWeb.StudentLive.Index do
<div :if={@info != %{}} class="flex overflow-x-auto z-10">
<div
:for={
info <-
{key, val} <-
@info
|> Map.values()
|> Enum.filter(&Kernel.!=(&1, ""))
|> Map.to_list()
|> dbg
}
class="flex flex-row gap-x-1 h-full"
phx-hook="expand_click"
id="info"
class="line-clamp-3 flex-wrap break-all flex bg-lighterbg w-24 max-w-24 p-1 rounded-md my-1"
>
<div
:for={{value, idx} <- Map.values(info) |> Enum.with_index()}
phx-hook="expand_click"
id={"info-#{idx}"}
class="line-clamp-3 flex-wrap break-all flex bg-lighterbg w-24 max-w-24 p-1 rounded-md my-1"
>
<%= value %>
<div class="flex flex-col">
<span><%= key %></span>
<div class="flex flex-row gap-x-1">
<p :for={val <- Map.values(val)} :if={val != ""}>
<%= val %>
</p>
</div>
</div>
</div>
</div>
Expand Down
7 changes: 5 additions & 2 deletions lib/elixirus_web/live/student_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
<.link
:if={@final_avg}
navigate={~p"/student/subjects"}
class="font-semibold col-span-3 text-center bg-card rounded-md w-full"
class="font-semibold col-span-2 text-center bg-card rounded-md w-full"
>
<%= @final_avg |> to_string() |> String.slice(0..5) %>
<%= @final_avg |> to_string() |> String.slice(0..4) %>
</.link>
<button phx-click="discard_all">
<Heroicons.trash class="w-5 h-5" />
</button>
</div>

<div :if={@timetable}>
Expand Down

0 comments on commit d14fa89

Please sign in to comment.