initial work on allowing import from ics/iCal
This commit is contained in:
parent
20d782ef62
commit
686bb489ba
7 changed files with 110 additions and 1 deletions
lib/shift73k_web
63
lib/shift73k_web/live/shift_import_live/index.ex
Normal file
63
lib/shift73k_web/live/shift_import_live/index.ex
Normal file
|
@ -0,0 +1,63 @@
|
|||
defmodule Shift73kWeb.ShiftImportLive.Index do
|
||||
use Shift73kWeb, :live_view
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
HTTPoison.start()
|
||||
|
||||
socket
|
||||
|> assign_defaults(session)
|
||||
|> live_okreply()
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("save", %{"ics_import" => %{"ics_url" => ics_url}}, socket) do
|
||||
ics_url
|
||||
|> IO.inspect(label: "given ical url :")
|
||||
|> HTTPoison.get!()
|
||||
|> handle_http_ics_response(socket)
|
||||
end
|
||||
|
||||
defp handle_http_ics_response(%HTTPoison.Response{status_code: 200} = resp, socket) do
|
||||
case content_type_calendar?(resp.headers) do
|
||||
false ->
|
||||
handle_http_ics_response(false, socket)
|
||||
|
||||
true ->
|
||||
resp.body
|
||||
|> ICalendar.from_ics()
|
||||
|> handle_parsed_ics_data(socket)
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_http_ics_response(_, socket) do
|
||||
socket
|
||||
|> put_flash(:error, "Bad data, bad URL, or some other error")
|
||||
|> live_noreply()
|
||||
end
|
||||
|
||||
defp handle_parsed_ics_data([], socket), do: handle_http_ics_response(false, socket)
|
||||
|
||||
defp handle_parsed_ics_data(events, socket) do
|
||||
IO.inspect(events, label: "We got some ical events! :")
|
||||
|
||||
socket
|
||||
|> put_flash(:success, "We got some ical events")
|
||||
|> live_noreply()
|
||||
end
|
||||
|
||||
def ical_request(ics_url) do
|
||||
# ics_url = "https://calendar.google.com/calendar/ical/l44mcggj2rsoqq7prlakvitqfo%40group.calendar.google.com/private-66f4cf8b340bdd6e9de8c60b2ae36528/basic.ics"
|
||||
end
|
||||
|
||||
defp content_type_calendar?(headers) do
|
||||
headers
|
||||
|> List.keyfind("Content-Type", 0)
|
||||
|> elem(1)
|
||||
|> String.contains?("text/calendar")
|
||||
end
|
||||
|
||||
def shift_from_event(%ICalendar.Event{} = event) do
|
||||
%{}
|
||||
end
|
||||
end
|
35
lib/shift73k_web/live/shift_import_live/index.html.leex
Normal file
35
lib/shift73k_web/live/shift_import_live/index.html.leex
Normal file
|
@ -0,0 +1,35 @@
|
|||
<div class="row justify-content-center">
|
||||
<div class="col-12 col-md-10 col-xl-8">
|
||||
|
||||
<h2>
|
||||
<%= icon_div @socket, "bi-box-arrow-in-left", [class: "icon baseline"] %>
|
||||
Import Shifts
|
||||
</h2>
|
||||
|
||||
<p class="lead">If you have an iCal/ics formatted calendar hosted elsewhere, provide its URL here to import its events.</p>
|
||||
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-12 col-sm-11 col-md-10 col-lg-9 col-xxl-8">
|
||||
|
||||
<%= form_for :ics_import, "#", [phx_submit: "save"], fn iimf -> %>
|
||||
|
||||
<div class="row">
|
||||
<div class="col mb-3">
|
||||
<%= label iimf, :ics_url, "iCal/ics URL", class: "form-label" %>
|
||||
<%= url_input iimf, :ics_url, class: "form-control" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col mb-3 text-end">
|
||||
<%= submit "Submit", class: "btn btn-primary" %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<% end %>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
|
@ -106,6 +106,8 @@ defmodule Shift73kWeb.Router do
|
|||
|
||||
get "/csv", UserShiftsCsvController, :new
|
||||
post "/csv", UserShiftsCsvController, :export
|
||||
|
||||
live "/import", ShiftImportLive.Index, :index
|
||||
end
|
||||
|
||||
# scope "/", Shift73kWeb do
|
||||
|
|
|
@ -34,6 +34,12 @@
|
|||
CSV Export
|
||||
<% end %>
|
||||
</li>
|
||||
<li>
|
||||
<%= link nav_link_opts(@conn, to: Routes.shift_import_index_path(@conn, :index), class: "dropdown-item") do %>
|
||||
<%= icon_div @conn, "bi-box-arrow-in-left", [class: "icon baseline me-1"] %>
|
||||
iCal Import
|
||||
<% end %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue