8000 Simplify by mwoods79 · Pull Request #104 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
32 changes: 0 additions & 32 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,3 @@ html {
text-transform: uppercase;
}

10BC8 /* Scanline overlay */
@utility scanlines {
position: relative;
&::after {
content: '';
position: absolute;
inset: 0;
pointer-events: none;
background: repeating-linear-gradient(
0deg,
oklch(0% 0 0 / 0.03) 0px,
oklch(0% 0 0 / 0.03) 1px,
transparent 1px,
transparent 3px
);
}
}

/* Subtle glitch on hover */
@keyframes cb-glitch {
0%, 100% { transform: translate(0); }
20% { transform: translate(-1px, 1px); }
40% { transform: translate(1px, -1px); }
60% { transform: translate(-1px, 0); }
80% { transform: translate(1px, 0); }
}

@utility glitch-hover {
&:hover {
animation: cb-glitch 0.3s ease-in-out;
}
}
2 changes: 1 addition & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if File.exists?(env_file) do
end

# Server mode (only parse if set, nil would raise)
if phx_server = optional("PHX_SERVER") do
if optional("PHX_SERVER") do
config :huddlz, HuddlzWeb.Endpoint, server: true
end

Expand Down
6 changes: 0 additions & 6 deletions lib/huddlz/communities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ defmodule Huddlz.Communities do
resources do
resource Huddlz.Communities.Huddl do
define :get_huddl, action: :read, get_by: [:id]
define :get_upcoming, action: :upcoming

define :search_huddlz,
action: :search,
Expand All @@ -24,7 +23,6 @@ defmodule Huddlz.Communities do
],
get?: false

define :get_by_status, action: :by_status, args: [:status]
define :get_group_huddlz, action: :by_group, args: [:group_id]
define :get_past_group_huddlz, action: :past_by_group, args: [:group_id]
define :rsvp_huddl, action: :rsvp, args: [:user_id]
Expand Down Expand Up @@ -76,10 +74,6 @@ defmodule Huddlz.Communities do
end

resource Huddlz.Communities.HuddlAttendee do
define :rsvp_to_huddl, action: :rsvp, args: [:huddl_id, :user_id]
define :cancel_huddl_rsvp, action: :cancel_rsvp
define :get_huddl_attendees, action: :by_huddl, args: [:huddl_id]
define :get_user_rsvps, action: :by_user, args: [:user_id]
define :check_user_rsvp, action: :check_rsvp, args: [:huddl_id, :user_id]
end

Expand Down
14 changes: 3 additions & 11 deletions lib/huddlz/communities/group_member/checks/group_member.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@ defmodule Huddlz.Communities.GroupMember.Checks.GroupMember do
@impl true
def match?(actor, %{subject: %{arguments: %{group_id: group_id}}}, _opts)
when not is_nil(actor) and not is_nil(group_id) do
# Check if the actor is a member of the group

query =
GroupMember
|> Ash.Query.filter(group_id: group_id, user_id: actor.id)
|> Ash.Query.limit(1)

case Ash.read(query, authorize?: false) do
{:ok, [%GroupMember{}]} -> true
_ -> false
end
GroupMember
|> Ash.Query.filter(group_id: group_id, user_id: actor.id)
|> Ash.exists?(authorize?: false)
end

def match?(_actor, _params, _opts), do: false
Expand Down
12 changes: 3 additions & 9 deletions lib/huddlz/communities/group_member/checks/group_organizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ defmodule Huddlz.Communities.GroupMember.Checks.GroupOrganizer do
@impl true
def match?(actor, %{group_id: group_id}, _opts)
when not is_nil(actor) and not is_nil(group_id) do
query =
GroupMember
|> Ash.Query.filter(group_id: group_id, user_id: actor.id, role: :organizer)
|> Ash.Query.limit(1)

case Ash.read(query, actor: actor) do
{:ok, [%GroupMember{}]} -> true
_ -> false
end
GroupMember
|> Ash.Query.filter(group_id: group_id, user_id: actor.id, role: :organizer)
|> Ash.exists?(authorize?: false)
end

def match?(_actor, _params, _opts), do: false
Expand Down
5 changes: 0 additions & 5 deletions lib/huddlz/communities/huddl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,6 @@ defmodule Huddlz.Communities.Huddl do
)
end

calculate :actor_is_member, :boolean do
description "Whether the current actor is a member of the huddl's group"
calculation expr(exists(group.members, user_id == ^actor(:id)))
end

calculate :is_publicly_visible, :boolean do
description "Whether the huddl is visible to everyone (public huddl in public group)"
calculation expr(is_private == false and group.is_public == true)
Expand Down
60 changes: 2 additions & 58 deletions lib/huddlz/communities/huddl/changes/add_huddl_template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Huddlz.Communities.Huddl.Changes.AddHuddlTemplate do
"""
use Ash.Resource.Change

alias Huddlz.Communities.Huddl
alias Huddlz.Communities.Huddl.RecurrenceHelper
alias Huddlz.Communities.HuddlTemplate

def change(changeset, _opts, _context) do
Expand All @@ -27,7 +27,7 @@ defmodule Huddlz.Communities.Huddl.Changes.AddHuddlTemplate do
|> Ash.Changeset.for_update(:update, %{huddl_template_id: huddl_template.id})
|> Ash.update!(authorize?: false)

generate_huddlz_from_template(huddl_template, huddl)
RecurrenceHelper.generate_huddlz_from_template(huddl_template, huddl)

{:ok, huddl}

Expand All @@ -36,60 +36,4 @@ defmodule Huddlz.Communities.Huddl.Changes.AddHuddlTemplate do
end
end)
end

defp generate_huddlz_from_template(huddl_template, huddl) do
case huddl_template.frequency do
:weekly ->
starts_at = DateTime.add(huddl.starts_at, 7, :day)
ends_at = DateTime.add(huddl.ends_at, 7, :day)

start_at_date = DateTime.to_date(starts_at)

if Date.before?(start_at_date, huddl_template.repeat_until) do
new_huddl =
Ash.Changeset.for_create(Huddl, :create, %{
starts_at: starts_at,
ends_at: ends_at,
event_type: huddl.event_type,
title: huddl.title,
description: huddl.description,
physical_location: huddl.physical_location,
is_private: huddl.is_private,
thumbnail_url: huddl.thumbnail_url,
creator_id: huddl.creator_id,
group_id: huddl.group_id,
huddl_template_id: huddl_template.id
})
|> Ash.create!(authorize?: false)

generate_huddlz_from_template(huddl_template, new_huddl)
end

:monthly ->
starts_at = DateTime.add(huddl.starts_at, 30, :day)
ends_at = DateTime.add(huddl.ends_at, 30, :day)

start_at_date = DateTime.to_date(starts_at)

if Date.before?(start_at_date, huddl_template.repeat_until) do
new_huddl =
Ash.Changeset.for_create(Huddl, :create, %{
starts_at: starts_at,
ends_at: ends_at,
event_type: huddl.event_type,
title: huddl.title,
description: huddl.description,
physical_location: huddl.physical_location,
is_private: huddl.is_private,
thumbnail_url: huddl.thumbnail_url,
creator_id: huddl.creator_id,
group_id: huddl.group_id,
huddl_template_id: huddl_template.id
})
|> Ash.create!(authorize?: false)

generate_huddlz_from_template(huddl_template, new_huddl)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,31 @@ defmodule Huddlz.Communities.Huddl.Changes.DefaultLocationFromGroup do
end

defp inherit_group_location(changeset) do
group_id = Ash.Changeset.get_attribute(changeset, :group_id)
group = get_group(changeset)

case Ash.get(Group, group_id, authorize?: false) do
{:ok, group} when not is_nil(group.latitude) and not is_nil(group.longitude) ->
case group do
%{latitude: lat, longitude: lng} when not is_nil(lat) and not is_nil(lng) ->
changeset
|> Ash.Changeset.force_change_attribute(:latitude, group.latitude)
|> Ash.Changeset.force_change_attribute(:longitude, group.longitude)
|> Ash.Changeset.force_change_attribute(:latitude, lat)
|> Ash.Changeset.force_change_attribute(:longitude, lng)

_ ->
changeset
end
end

defp get_group(changeset) do
case changeset.context do
%{group: group} when not is_nil(group) ->
group

_ ->
group_id = Ash.Changeset.get_attribute(changeset, :group_id)

case Ash.get(Group, group_id, authorize?: false) do
{:ok, group} -> group
_ -> nil
end
end
end
end
59 changes: 2 additions & 57 deletions lib/huddlz/communities/huddl/changes/edit_recurring_huddlz.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Huddlz.Communities.Huddl.Changes.EditRecurringHuddlz do
require Ash.Query

alias Huddlz.Communities.Huddl
alias Huddlz.Communities.Huddl.RecurrenceHelper

def change(changeset, _opts, _context) do
Ash.Changeset.after_action(changeset, fn _changeset, huddl ->
Expand Down Expand Up @@ -35,64 +36,8 @@ defmodule Huddlz.Communities.Huddl.Changes.EditRecurringHuddlz do
|> Ash.read!()
|> Enum.each(&Ash.destroy(&1, authorize?: false))

generate_huddlz_from_template(huddl_template, huddl)
RecurrenceHelper.generate_huddlz_from_template(huddl_template, huddl)

{:ok, huddl}
end

defp generate_huddlz_from_template(huddl_template, huddl) do
case huddl_template.frequency do
:weekly ->
starts_at = DateTime.add(huddl.starts_at, 7, :day)
ends_at = DateTime.add(huddl.ends_at, 7, :day)

start_at_date = DateTime.to_date(starts_at)

if Date.before?(start_at_date, huddl_template.repeat_until) do
new_huddl =
Ash.Changeset.for_create(Huddl, :create, %{
starts_at: starts_at,
ends_at: ends_at,
event_type: huddl.event_type,
title: huddl.title,
description: huddl.description,
physical_location: huddl.physical_location,
is_private: huddl.is_private,
thumbnail_url: huddl.thumbnail_url,
creator_id: huddl.creator_id,
group_id: huddl.group_id,
huddl_template_id: huddl_template.id
})
|> Ash.create!(authorize?: false)

generate_huddlz_from_template(huddl_template, new_huddl)
end

:monthly ->
starts_at = DateTime.add(huddl.starts_at, 30, :day)
ends_at = DateTime.add(huddl.ends_at, 30, :day)

start_at_date = DateTime.to_date(starts_at)

if Date.before?(start_at_date, huddl_template.repeat_until) do
new_huddl =
Ash.Changeset.for_create(Huddl, :create, %{
starts_at: starts_at,
ends_at: ends_at,
event_type: huddl.event_type,
title: huddl.title,
description: huddl.description,
physical_location: huddl.physical_location,
is_private: huddl.is_private,
thumbnail_url: huddl.thumbnail_url,
creator_id: huddl.creator_id,
group_id: huddl.group_id,
huddl_template_id: huddl_template.id
})
|> Ash.create!(authorize?: false)

generate_huddlz_from_template(huddl_template, new_huddl)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ defmodule Huddlz.Communities.Huddl.Changes.ForcePrivateForPrivateGroups do

def change(changeset, _opts, _context) do
with group_id when not is_nil(group_id) <- Ash.Changeset.get_attribute(changeset, :group_id),
{:ok, group} <- Ash.get(Group, group_id, authorize?: false),
false <- group.is_public do
Ash.Changeset.force_change_attribute(changeset, :is_private, true)
{:ok, group} <- Ash.get(Group, group_id, authorize?: false) do
changeset = Ash.Changeset.set_context(changeset, %{group: group})

if group.is_public do
changeset
else
Ash.Changeset.force_change_attribute(changeset, :is_private, true)
end
else
_ -> changeset
end
Expand Down
42 changes: 42 additions & 0 deletions lib/huddlz/communities/huddl/recurrence_helper.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule Huddlz.Communities.Huddl.RecurrenceHelper do
@moduledoc """
Shared logic for generating recurring huddlz from a template.
"""

alias Huddlz.Communities.Huddl

@doc """
Recursively generates future huddlz based on the template's frequency
and repeat_until date. Each new huddl copies the source huddl's properties
and advances the start/end times by the appropriate interval.
"""
def generate_huddlz_from_template(huddl_template, huddl) do
interval_days = frequency_days(huddl_template.frequency)
starts_at = DateTime.add(huddl.starts_at, interval_days, :day)
ends_at = DateTime.add(huddl.ends_at, interval_days, :day)
start_at_date = DateTime.to_date(starts_at)

if Date.before?(start_at_date, huddl_template.repeat_until) do
new_huddl =
Ash.Changeset.for_create(Huddl, :create, %{
starts_at: starts_at,
ends_at: ends_at,
event_type: huddl.event_type,
title: huddl.title,
description: huddl.description,
physical_location: huddl.physical_location,
is_private: huddl.is_private,
thumbnail_url: huddl.thumbnail_url,
creator_id: huddl.creator_id,
group_id: huddl.group_id,
huddl_template_id: huddl_template.id
})
|> Ash.create!(authorize?: false)

generate_huddlz_from_template(huddl_template, new_huddl)
end
end

defp frequency_days(:weekly), do: 7
defp frequency_days(:monthly), do: 30
end
16 changes: 8 additions & 8 deletions lib/huddlz_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,14 @@ defmodule HuddlzWeb.CoreComponents do
{@rest}
>
<option value="">Select duration...</option>
<option value="30" selected={@value == 30}>30 minutes</option>
<option value="60" selected={@value == 60}>1 hour</option>
<option value="90" selected={@value == 90}>1.5 hours</option>
<option value="120" selected={@value == 120}>2 hours</option>
<option value="150" selected={@value == 150}>2.5 hours</option>
<option value="180" selected={@value == 180}>3 hours</option>
<option value="240" selected={@value == 240}>4 hours</option>
<option value="360" selected={@value == 360}>6 hours</option>
<option value="30" selected={to_string(@value) == "30"}>30 minutes</option>
<option value="60" selected={to_string(@value) == "60"}>1 hour</option>
<option value="90" selected={to_string(@value) == "90"}>1.5 hours</option>
<option value="120" selected={to_string(@value) == "120"}>2 hours</option>
<option value="150" selected={to_string(@value) == "150"}>2.5 hours</option>
<option value="180" selected={to_string(@value) == "180"}>3 hours</option>
<option value="240" selected={to_string(@value) == "240"}>4 hours</option>
<option value="360" selected={to_string(@value) == "360"}>6 hours</option>
</select>
<.error :for={msg <- @errors}>{msg}</.error>
</fieldset>
Expand Down
Loading
Loading
0