Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: link scheduling #75

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/cesium_link/links.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ defmodule CesiumLink.Links do

"""
def list_unarchived_links_by_index do
Repo.all(from l in Link, where: l.archived == false, order_by: [asc: l.index])
Link
|> where([l], fragment("? <= now() OR ? IS NULL", l.publish_at, l.publish_at))
|> where([l], not l.archived)
|> order_by([l], asc: l.index)
|> Repo.all()
end

@doc """
Expand Down
3 changes: 2 additions & 1 deletion lib/cesium_link/links/link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule CesiumLink.Links.Link do
use CesiumLink.Schema

@required_fields ~w(name emoji url attention edited_at)a
@optional_fields ~w(index archived visits)a
@optional_fields ~w(index archived visits publish_at)a

schema "links" do
field :archived, :boolean, default: false
Expand All @@ -16,6 +16,7 @@ defmodule CesiumLink.Links.Link do
field :url, :string
field :visits, :integer, default: 0
field :edited_at, :utc_datetime
field :publish_at, :utc_datetime

timestamps(type: :utc_datetime)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/cesium_link_web/live/link_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ defmodule CesiumLinkWeb.LinkLive.FormComponent do
<.input field={@form[:emoji]} type="emoji" label="Emoji" />
<.input field={@form[:url]} type="text" label="URL" />
<.input field={@form[:attention]} type="checkbox" label="Attention" />
<%= if @action == :new do %>
<.input field={@form[:publish_at]} type="datetime-local" label="Publish At" />
<% end %>
<:actions>
<.button phx-disable-with="Saving...">Save Link</.button>
</:actions>
Expand Down
32 changes: 24 additions & 8 deletions lib/cesium_link_web/live/link_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,46 @@
</.header>

<.table id="links" rows={@streams.links} phx-hook="Sorting">
<:col :let={{_id, _link}}><.icon name="hero-bars-3 cursor-pointer ml-4" class="handle w-5 h-5" /></:col>
<:col :let={{_id, _link}}>
<% publish_future = _link.publish_at && DateTime.compare(_link.publish_at, DateTime.utc_now()) == :gt %>

Check warning on line 19 in lib/cesium_link_web/live/link_live/index.html.heex

View workflow job for this annotation

GitHub Actions / OTP 26.x / Elixir 1.14.x

the underscored variable "_link" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore

Check warning on line 19 in lib/cesium_link_web/live/link_live/index.html.heex

View workflow job for this annotation

GitHub Actions / OTP 26.x / Elixir 1.14.x

the underscored variable "_link" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore

Check warning on line 19 in lib/cesium_link_web/live/link_live/index.html.heex

View workflow job for this annotation

GitHub Actions / Code Quality (26.x, 1.14.x)

the underscored variable "_link" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore

Check warning on line 19 in lib/cesium_link_web/live/link_live/index.html.heex

View workflow job for this annotation

GitHub Actions / Code Quality (26.x, 1.14.x)

the underscored variable "_link" is used after being set. A leading underscore indicates that the value of the variable should be ignored. If this is intended please rename the variable to remove the underscore
Comment on lines +18 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<:col :let={{_id, _link}}>
<% publish_future = _link.publish_at && DateTime.compare(_link.publish_at, DateTime.utc_now()) == :gt %>
<:col :let={{_id, link}}>
<% publish_future = link.publish_at && DateTime.compare(link.publish_at, DateTime.utc_now()) == :gt %>

<.icon name="hero-bars-3 cursor-pointer ml-4" class={"handle w-5 h-5 #{if publish_future, do: "opacity-50"}"} />
</:col>
<:col :let={{_id, link}} label="Name">
<p class="font-semibold text-zinc-900"><%= link.name %></p>
<% publish_future = link.publish_at && DateTime.compare(link.publish_at, DateTime.utc_now()) == :gt %>
<p class={"font-semibold text-zinc-900 #{if publish_future, do: "opacity-50"}"}><%= link.name %></p>
</:col>
<:col :let={{_id, link}} label="Emoji">
<.emoji code={link.emoji} />
<% publish_future = link.publish_at && DateTime.compare(link.publish_at, DateTime.utc_now()) == :gt %>
<span class={if publish_future, do: "opacity-50"}>
<.emoji code={link.emoji} />
</span>
</:col>
<:col :let={{_id, link}} label="URL">
<.link target="_blank" class="hover:text-brand hover:underline" navigate={link.url}>
<% publish_future = link.publish_at && DateTime.compare(link.publish_at, DateTime.utc_now()) == :gt %>
<.link target="_blank" class={"hover:text-brand hover:underline #{if publish_future, do: "opacity-50"}"} navigate={link.url}>
<%= truncate_elipsis(link.url, 50) %>
</.link>
</:col>
<:col :let={{_id, link}} label="Clicks"><%= link.visits %></:col>
<:col :let={{_id, link}} label="Clicks">
<% publish_future = link.publish_at && DateTime.compare(link.publish_at, DateTime.utc_now()) == :gt %>
<p class={if publish_future, do: "opacity-50"}>
<%= link.visits %>
</p>
</:col>
<:col :let={{_id, link}} label="Attention">
<input type="checkbox" disabled={true} checked={link.attention} class="self-center block rounded-md text-gray-600" />
<% publish_future = link.publish_at && DateTime.compare(link.publish_at, DateTime.utc_now()) == :gt %>
<input type="checkbox" disabled={true} checked={link.attention} class={"self-center block rounded-md text-gray-600 #{if publish_future, do: "opacity-50"}"} />
</:col>
<:action :let={{_id, link}}>
<% publish_future = link.publish_at && DateTime.compare(link.publish_at, DateTime.utc_now()) == :gt %>
<.link patch={~p"/admin/links/#{link}/edit"}>
<.icon name="hero-pencil" class="w-5 h-5" />
<.icon name="hero-pencil" class={"w-5 h-5 #{if publish_future, do: "opacity-50"}"} />
</.link>
</:action>
<:action :let={{_id, link}}>
<% publish_future = link.publish_at && DateTime.compare(link.publish_at, DateTime.utc_now()) == :gt %>
<.link patch={~p"/admin/links/#{link}/archive"}>
<.icon name="hero-archive-box" class="w-5 h-5" />
<.icon name="hero-archive-box" class={"w-5 h-5 #{if publish_future, do: "opacity-50"}"} />
</.link>
</:action>
</.table>
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20240918181533_add_link_scheduling.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule CesiumLink.Repo.Migrations.AddLinkScheduling do
use Ecto.Migration

def change do
alter table(:links) do
add :publish_at, :utc_datetime
end
end
end
Loading