ics output seems to be working
This commit is contained in:
parent
8fe1bc6322
commit
15c50f0114
6 changed files with 40 additions and 12 deletions
lib/shift73k_web
controllers
live/user/settings
views
|
@ -9,8 +9,6 @@ defmodule Shift73kWeb.UserShiftsCsvController do
|
|||
end
|
||||
|
||||
def export(conn, %{"csv_export" => request_params}) do
|
||||
IO.inspect(request_params, label: "csv request params :")
|
||||
|
||||
case Map.get(request_params, "user_id") == conn.assigns.current_user.id do
|
||||
true ->
|
||||
export_csv(conn, request_params)
|
||||
|
@ -39,7 +37,6 @@ defmodule Shift73kWeb.UserShiftsCsvController do
|
|||
[csv_headers() | csv_data(user_id, date_range)]
|
||||
|> NimbleCSV.RFC4180.dump_to_iodata()
|
||||
|> to_string()
|
||||
|> IO.inspect()
|
||||
end
|
||||
|
||||
def csv_data(user_id, date_range) do
|
||||
|
|
|
@ -3,14 +3,46 @@ defmodule Shift73kWeb.UserShiftsIcsController do
|
|||
|
||||
alias Shift73k.Accounts
|
||||
alias Shift73k.Accounts.User
|
||||
alias Shift73k.Shifts
|
||||
alias Shift73k.Shifts.Shift
|
||||
|
||||
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)
|
||||
%User{} = user -> render_ics(conn, user.id)
|
||||
|
||||
_ -> send_not_found(conn)
|
||||
|
||||
_ ->
|
||||
send_resp(conn, 404, "Not found")
|
||||
end
|
||||
end
|
||||
|
||||
defp send_not_found(conn), do: send_resp(conn, 404, "Not found")
|
||||
|
||||
defp render_ics(conn, user_id) do
|
||||
user_id
|
||||
|> Shifts.list_shifts_by_user()
|
||||
|> Enum.map(&event_from_shift/1)
|
||||
|> render_ics(conn, user_id)
|
||||
end
|
||||
|
||||
defp render_ics([], conn, _user_id), do: send_not_found(conn)
|
||||
|
||||
defp render_ics(events, conn, user_id) do
|
||||
conn
|
||||
|> put_resp_content_type("text/calendar")
|
||||
|> put_resp_header("content-disposition", "attachment; filename=\"#{user_id}.ics\"")
|
||||
|> send_resp(200, ICalendar.to_ics(%ICalendar{events: events}))
|
||||
end
|
||||
|
||||
defp event_from_shift(%Shift{} = s) do
|
||||
dt_start = DateTime.new!(s.date, s.time_start, s.time_zone)
|
||||
shift_length_s = Shifts.shift_length(s) * 60
|
||||
|
||||
%ICalendar.Event{
|
||||
summary: s.subject,
|
||||
dtstart: dt_start,
|
||||
dtend: DateTime.add(dt_start, shift_length_s),
|
||||
description: s.description,
|
||||
location: s.location
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
<p><%= @current_user.calendar_slug %></p>
|
||||
|
||||
<%= form_for :calendar_slug, "#", [phx_submit: :save, phx_target: @myself], fn csf -> %>
|
||||
<%= form_for :calendar_slug, "#", [phx_submit: :save, phx_target: @myself], fn _csf -> %>
|
||||
<%= submit "Generate new", class: "btn btn-primary" %>
|
||||
<% end %>
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
defmodule Shift73kWeb.UserShiftsIcsView do
|
||||
use Shift73kWeb, :view
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue