diff --git a/lib/shift73k/accounts.ex b/lib/shift73k/accounts.ex index 9edde8c4..199a52b3 100644 --- a/lib/shift73k/accounts.ex +++ b/lib/shift73k/accounts.ex @@ -39,6 +39,10 @@ defmodule Shift73k.Accounts do Repo.get_by(User, email: email) end + def get_user_by_calendar_slug(slug) when is_binary(slug) do + Repo.get_by(User, calendar_slug: slug) + end + @doc """ Gets a user by email and password. diff --git a/lib/shift73k_web/controllers/user_shifts_csv_controller.ex b/lib/shift73k_web/controllers/user_shifts_csv_controller.ex index 60783bfe..38a93651 100644 --- a/lib/shift73k_web/controllers/user_shifts_csv_controller.ex +++ b/lib/shift73k_web/controllers/user_shifts_csv_controller.ex @@ -5,7 +5,7 @@ defmodule Shift73kWeb.UserShiftsCsvController do alias Shift73k.Shifts.Shift def new(conn, _params) do - render(conn, "new.html", error_message: nil) + render(conn, "new.html") end def export(conn, %{"csv_export" => request_params}) do diff --git a/lib/shift73k_web/controllers/user_shifts_ics_controller.ex b/lib/shift73k_web/controllers/user_shifts_ics_controller.ex new file mode 100644 index 00000000..888a5230 --- /dev/null +++ b/lib/shift73k_web/controllers/user_shifts_ics_controller.ex @@ -0,0 +1,16 @@ +defmodule Shift73kWeb.UserShiftsIcsController do + use Shift73kWeb, :controller + + alias Shift73k.Accounts + alias Shift73k.Accounts.User + + def index(conn, %{"slug" => slug}) do + case Accounts.get_user_by_calendar_slug(slug) do + %User{} = user -> + render(conn, "index.html", slug: slug, user: user) + + _ -> + send_resp(conn, 404, "Not found") + end + end +end diff --git a/lib/shift73k_web/live/user/settings/calendar_url.ex b/lib/shift73k_web/live/user/settings/calendar_url.ex index ea621ca7..06e02ff3 100644 --- a/lib/shift73k_web/live/user/settings/calendar_url.ex +++ b/lib/shift73k_web/live/user/settings/calendar_url.ex @@ -9,24 +9,8 @@ defmodule Shift73kWeb.UserLive.Settings.CalendarUrl do |> assign(id: assigns.id) |> assign(current_user: user) |> live_okreply() - - # |> assign(form_week_start_at: user.week_start_at) end - # def week_start_options do - # week_start = Date.beginning_of_week(Date.utc_today(), hd(weekdays())) - - # week_start - # |> Date.range(Date.add(week_start, 6)) - # |> Enum.map(&Calendar.strftime(&1, "%A")) - # |> Enum.zip(weekdays()) - # end - - # @impl true - # def handle_event("changed", %{"calendar_view" => %{"week_start_at" => day}}, socket) do - # {:noreply, assign(socket, form_week_start_at: String.to_existing_atom(day))} - # end - @impl true def handle_event("save", _params, socket) do Accounts.change_user_calendar_slug(socket.assigns.current_user.id) diff --git a/lib/shift73k_web/live/user/settings/calendar_url.html.leex b/lib/shift73k_web/live/user/settings/calendar_url.html.leex index 2361caa7..a1f8b10f 100644 --- a/lib/shift73k_web/live/user/settings/calendar_url.html.leex +++ b/lib/shift73k_web/live/user/settings/calendar_url.html.leex @@ -8,26 +8,4 @@ <%= submit "Generate new", class: "btn btn-primary" %> <% end %> - <%#= form_for :calendar_view, "#", [phx_change: :changed, phx_submit: :save, phx_target: @myself], fn cvf -> %> - - <%#= label cvf, :week_start_at, class: "form-label" %> - <%#
%> - <%#= icon_div @socket, "bi-calendar2-range", [class: "icon is-left text-muted fs-5"] %> - <%#= select cvf, :week_start_at, week_start_options(), - value: @current_user.week_start_at, - class: "form-select" - %> - <%#
%> - - <%#
%> - <%#= submit "Save", - class: "btn btn-primary", - disabled: @form_week_start_at == @current_user.week_start_at, - aria_disabled: (@form_week_start_at == @current_user.week_start_at) && "true" || false, - phx_disable_with: "Saving..." - %> - <%#
%> - - <%# end %> - diff --git a/lib/shift73k_web/router.ex b/lib/shift73k_web/router.ex index 98af2bd8..a22c1a95 100644 --- a/lib/shift73k_web/router.ex +++ b/lib/shift73k_web/router.ex @@ -59,6 +59,7 @@ defmodule Shift73kWeb.Router do scope "/", Shift73kWeb do pipe_through([:browser, :redirect_if_user_is_authenticated]) + # session routes, irrelevant if user is authenticated get("/users/register", UserRegistrationController, :new) get("/users/log_in", UserSessionController, :new) post("/users/log_in", UserSessionController, :create) @@ -70,21 +71,25 @@ defmodule Shift73kWeb.Router do scope "/", Shift73kWeb do pipe_through([:browser, :require_authenticated_user]) - # # liveview user settings + # user settings (change email, password, calendar week start, etc) live("/users/settings", UserLive.Settings, :edit) - # original user routes from phx.gen.auth + # confirm email by token get("/users/settings/confirm_email/:token", UserSettingsController, :confirm_email) end scope "/", Shift73kWeb do pipe_through([:browser]) + # session paths delete("/users/log_out", UserSessionController, :delete) get("/users/force_logout", UserSessionController, :force_logout) get("/users/confirm", UserConfirmationController, :new) post("/users/confirm", UserConfirmationController, :create) get("/users/confirm/:token", UserConfirmationController, :confirm) + + # ics/ical route for user's shifts + get("/ics/:slug", UserShiftsIcsController, :index) end scope "/", Shift73kWeb do diff --git a/lib/shift73k_web/templates/user_shifts_ics/index.html.eex b/lib/shift73k_web/templates/user_shifts_ics/index.html.eex new file mode 100644 index 00000000..b6bd396f --- /dev/null +++ b/lib/shift73k_web/templates/user_shifts_ics/index.html.eex @@ -0,0 +1,13 @@ +
+
+ +

+ <%= icon_div @conn, "bi-calendar2", [class: "icon baseline"] %> + User Shifts ICS +

+

Shifts for user: <%= @user.email %>

+

Calendar slug: <%= @slug %>

+ + +
+
diff --git a/lib/shift73k_web/views/user_shifts_ics_view.ex b/lib/shift73k_web/views/user_shifts_ics_view.ex new file mode 100644 index 00000000..d1f8b774 --- /dev/null +++ b/lib/shift73k_web/views/user_shifts_ics_view.ex @@ -0,0 +1,3 @@ +defmodule Shift73kWeb.UserShiftsIcsView do + use Shift73kWeb, :view +end