Skip to content

Commit

Permalink
add inefficient way to delete notes
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushmau5 committed Apr 29, 2024
1 parent cfeb942 commit cf961bd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,3 @@ Whenever data is updated(a new user visits my website/blog), we send a pubsub me
High overview on how everything fits together:

![Illustration](dashboard-working.png)

## TODO:

- Think about settings up a "demo" page for view only access
- Move get and check calls to "upsert"(but will this incur more complexity?).
- Add admin tool(https://github.com/tfwright/live_admin or https://github.com/mojotech/torch or kaffy)
- Move dashboard and spotify data into stream(currently stream doesn't support filtering, sorting, etc without being hacky)
31 changes: 25 additions & 6 deletions lib/accumulator/notes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ defmodule Accumulator.Notes do
order_by: [asc: n.id]
)
|> Repo.all()
|> Enum.group_by(fn %{inserted_at: inserted_at} ->
inserted_at |> NaiveDateTime.to_date() |> Date.to_string()
end)
|> Enum.map(fn {date, notes} -> [date, notes] end)
|> Enum.sort_by(fn [date, _] -> date end)
|> group_and_sort_notes()

{result, NaiveDateTime.to_date(starting_date_time)}
end

def get_notes_grouped_and_ordered_till_date(date) do
date_tuple = date |> Date.add(1) |> Date.to_erl()

date =
NaiveDateTime.from_erl!({date_tuple, {0, 0, 0}}) |> NaiveDateTime.truncate(:second)

from(n in Note,
where: n.inserted_at >= ^date,
order_by: [asc: n.id]
)
|> Repo.all()
|> group_and_sort_notes()
end

def insert(changeset) do
Repo.insert(changeset)
end
Expand All @@ -52,7 +62,16 @@ defmodule Accumulator.Notes do
end

def delete(id) do
Repo.get(Note, id)
Repo.get!(Note, id)
|> Repo.delete()
end

defp group_and_sort_notes(notes) do
notes
|> Enum.group_by(fn %{inserted_at: inserted_at} ->
inserted_at |> NaiveDateTime.to_date() |> Date.to_string()
end)
|> Enum.map(fn {date, notes} -> [date, notes] end)
|> Enum.sort_by(fn [date, _] -> date end)
end
end
11 changes: 11 additions & 0 deletions lib/accumulator_web/live/notes_live/notes_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ defmodule AccumulatorWeb.NotesLive do
{:noreply, socket}
end

def handle_event("edit", %{"id" => _id} = _params, socket) do
# TODO: implement this
{:noreply, socket}
end

def handle_event("delete", %{"id" => id} = _params, socket) do
{:ok, _} = Notes.delete(id)
notes = Notes.get_notes_grouped_and_ordered_till_date(socket.assigns.pagination_date)
{:noreply, stream(socket, :notes, notes, reset: true)}
end

def handle_event("search-change", _params, socket) do
# Do nothing with the search bar change rn
{:noreply, socket}
Expand Down
6 changes: 6 additions & 0 deletions lib/accumulator_web/live/notes_live/notes_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<button phx-click="more-notes" class="text-sm opacity-60 text-right w-full">
Load more
</button>

<%!-- Notes UI --%>
<div
class="bg-black bg-opacity-20 p-2 rounded-md max-h-[500px] overflow-y-scroll"
id="notes"
Expand All @@ -24,9 +26,13 @@
<div :for={file <- note.files}>
<%= file.name %>
</div>
<button phx-click="delete" phx-value-id={note.id}>Delete</button>
<button phx-click="edit" phx-value-id={note.id}>Edit</button>
</div>
</div>
</div>

<%!-- Submission form --%>
<div>
<.notes_form for={@form} phx-change="validate" phx-submit="save">
<div class="flex gap-2 border rounded-lg border-zinc-700 focus:border-zinc-600 focus:ring-zinc-800/5">
Expand Down

0 comments on commit cf961bd

Please sign in to comment.