Skip to content

Commit

Permalink
wip implementation of getting UI result
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushmau5 committed Apr 27, 2024
1 parent b52835c commit 56a4429
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 109 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ npm-debug.log
.env
.env.fish
.lexical/
.elixir-tools/
.elixir-tools/

# Ignore the local uploads/ dir
priv/uploads/
3 changes: 3 additions & 0 deletions fiex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

fly ssh console --pty --select -C "/app/bin/accumulator remote"
40 changes: 38 additions & 2 deletions lib/accumulator/notes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,45 @@ defmodule Accumulator.Notes do
|> Repo.all()
end

def insert(params) do
changeset = Note.changeset(%Note{}, params)
def fake_insert(date) do
%Note{files: [], text: "meowwww", inserted_at: date, updated_at: date}
|> Repo.insert!()
end

# TODO: implement the strucutre of data in application layer
def implementation() do
# life would start becoming better if i just focus and control myself
# controlling myself is the ultimate test of mind

{date_tuple, _} = :calendar.local_time()
ending_date_time = NaiveDateTime.from_erl!({date_tuple, {0, 0, 0}})

starting_date_time =
NaiveDateTime.add(ending_date_time, -10, :day) |> NaiveDateTime.truncate(:second)

result =
from(n in Note, where: n.inserted_at >= ^starting_date_time, order_by: [desc: n.id])
|> Repo.all()
|> Enum.reduce(%{dates: []}, fn note, acc ->
string_date = note.inserted_at |> NaiveDateTime.to_date() |> Date.to_string()
grouped_data = Map.get(acc, string_date, [])
grouped_data = grouped_data ++ [note]
acc = Map.put(acc, string_date, grouped_data)

dates = Map.get(acc, :dates)

dates =
if Enum.member?(dates, string_date),
do: dates,
else: dates ++ [string_date]

Map.put(acc, :dates, dates)
end)

{result, starting_date_time}
end

def insert(changeset) do
Repo.insert(changeset)
end

Expand Down
17 changes: 17 additions & 0 deletions lib/accumulator/notes/file.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Accumulator.Notes.File do
use Ecto.Schema
import Ecto.Changeset

embedded_schema do
field(:name, :string)
field(:type, :string)
field(:access_path, :string)
field(:storage_path, :string)
end

def changeset(file_entry, attrs \\ %{}) do
file_entry
|> cast(attrs, [:name, :type, :access_path, :storage_path], empty_values: [[], nil])
|> validate_required([:name, :access_path, :storage_path])
end
end
8 changes: 4 additions & 4 deletions lib/accumulator/notes/note.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ defmodule Accumulator.Notes.Note do
import Ecto.Changeset

schema "notes" do
field(:heading, :string)
field(:text, :string)
field(:files, {:array, :string}, default: [])
timestamps()

embeds_many(:files, Accumulator.Notes.File, on_replace: :delete)
end

def changeset(note, params \\ %{}) do
note
|> cast(params, [:heading, :text, :files])
|> validate_length(:heading, max: 200)
|> cast(params, [:text])
|> cast_embed(:files)
end
end
100 changes: 0 additions & 100 deletions lib/accumulator_web/live/notes_live.ex

This file was deleted.

136 changes: 136 additions & 0 deletions lib/accumulator_web/live/notes_live/notes_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
defmodule AccumulatorWeb.NotesLive do
use AccumulatorWeb, :live_view

alias Accumulator.{Notes, Notes.Note}

@max_file_entries 20
@max_file_size 5_000_000_00

@impl true
def mount(_params, _session, socket) do
form =
%Note{}
|> Note.changeset()
|> to_form()

socket =
if connected?(socket) do
notes = Notes.get_by_ascending_order()
stream(socket, :notes, notes)
else
stream(socket, :notes, [])
end

{:ok,
socket
|> assign(page_title: "Notes")
|> assign(form: form)
|> assign(uploaded_files: [])
|> allow_upload(:files,
accept: :any,
max_entries: @max_file_entries,
max_file_size: @max_file_size
)
|> assign(search: to_form(%{"search" => ""}))}
end

@impl true
def handle_event("validate", %{"note" => note_params} = _params, socket) do
note_changeset = %Note{} |> Note.changeset(note_params)

form =
note_changeset
|> Map.put(:action, :validate)
|> to_form

{:noreply, assign(socket, form: form)}
end

@impl true
def handle_event("cancel-upload", %{"ref" => ref}, socket) do
{:noreply, cancel_upload(socket, :files, ref)}
end

@impl true
def handle_event("save", %{"note" => note_params} = _params, socket) do
storage_directory = if files_present?(socket), do: Accumulator.Pastes.create_storage_dir()

uploaded_files =
consume_uploaded_entries(socket, :files, fn %{path: path}, entry ->
%{client_name: file_name, client_type: file_type} = entry
santized_filename = Zarex.sanitize(file_name)

dest = Path.join([storage_directory, santized_filename])
File.cp!(path, dest)

{:ok,
%{
name: santized_filename,
type: file_type,
storage_path: dest,
access_path:
"/uploads/#{Path.join(Path.basename(storage_directory), santized_filename)}"
}}
end)

note_changeset =
%Note{}
|> Note.changeset(note_params)
|> Ecto.Changeset.put_embed(:files, uploaded_files)

socket =
case Notes.insert(note_changeset) do
{:ok, note} ->
form = %Note{} |> Note.changeset() |> to_form()

socket
|> assign(form: form)
|> stream_insert(:notes, note)

{:error, changeset} ->
assign(socket, form: to_form(changeset))
end

{:noreply, socket}
end

@impl true
def handle_event("search-change", _params, socket) do
# Do nothing with the search bar change rn
{:noreply, socket}
end

@impl true
def handle_event("search-submit", %{"search" => search} = _params, socket) do
# TODO: implement search functionality
IO.inspect(search, label: "Search")
{:noreply, socket}
end

defp error_to_string(:too_large), do: "Too large"
defp error_to_string(:not_accepted), do: "You have selected an unacceptable file type"
defp error_to_string(:too_many_files), do: "You have selected too many files"

defp files_present?(socket) do
case uploaded_entries(socket, :files) do
{[], []} -> false
_ -> true
end
end

def todos() do
~S"""
TODOs:
- need to think about the way to show datewise data. How to query such that the results come in a sorted way grouped by date?
- Using form or file uploads: drop/click + input + enter to save
- File and text UI
- Pagination and handling lots of data in the DOM(virtual lists?)
For today:
- Work: try to finish up the UI - 3 hours
- Personal: Notes project - 2 hours
- DSP - 2 hours
"""
end
end
Loading

0 comments on commit 56a4429

Please sign in to comment.