Implement shared RFC 5545 VEvent library with workspace restructuring
- Created calendar-models/ shared library with RFC 5545-compliant VEvent structures - Migrated backend to use shared VEvent with proper field mappings (dtstart/dtend, rrule, exdate, etc.) - Converted CalDAV client to parse into VEvent structures with structured types - Updated all CRUD handlers to use VEvent with CalendarUser, Attendee, VAlarm types - Restructured project as Cargo workspace with frontend/, backend/, calendar-models/ - Updated Trunk configuration for new directory structure - Fixed all compilation errors and field references throughout codebase - Updated documentation and build instructions for workspace structure 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
67
frontend/Cargo.toml
Normal file
67
frontend/Cargo.toml
Normal file
@@ -0,0 +1,67 @@
|
||||
[package]
|
||||
name = "calendar-app"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# Frontend binary only
|
||||
|
||||
[dependencies]
|
||||
calendar-models = { workspace = true, features = ["wasm"] }
|
||||
yew = { version = "0.21", features = ["csr"] }
|
||||
web-sys = { version = "0.3", features = [
|
||||
"console",
|
||||
"HtmlSelectElement",
|
||||
"HtmlInputElement",
|
||||
"HtmlTextAreaElement",
|
||||
"Event",
|
||||
"MouseEvent",
|
||||
"InputEvent",
|
||||
"Element",
|
||||
"Document",
|
||||
"Window",
|
||||
"Location",
|
||||
"Headers",
|
||||
"Request",
|
||||
"RequestInit",
|
||||
"RequestMode",
|
||||
"Response",
|
||||
"CssStyleDeclaration",
|
||||
] }
|
||||
wasm-bindgen = "0.2"
|
||||
|
||||
# HTTP client for CalDAV requests
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
# Calendar and iCal parsing
|
||||
ical = "0.7"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
# Date and time handling
|
||||
chrono = { version = "0.4", features = ["serde", "wasm-bindgen"] }
|
||||
chrono-tz = "0.8"
|
||||
|
||||
# Error handling
|
||||
anyhow = "1.0"
|
||||
thiserror = "1.0"
|
||||
|
||||
# Logging
|
||||
log = "0.4"
|
||||
console_log = "1.0"
|
||||
|
||||
# UUID generation for calendar events
|
||||
uuid = { version = "1.0", features = ["v4", "wasm-bindgen", "serde"] }
|
||||
getrandom = { version = "0.2", features = ["js"] }
|
||||
|
||||
# Environment variable handling
|
||||
dotenvy = "0.15"
|
||||
base64 = "0.21"
|
||||
|
||||
# XML/Regex parsing
|
||||
regex = "1.0"
|
||||
|
||||
# Yew routing and local storage (WASM only)
|
||||
yew-router = "0.18"
|
||||
gloo-storage = "0.3"
|
||||
gloo-timers = "0.3"
|
||||
wasm-bindgen-futures = "0.4"
|
||||
|
||||
16
frontend/Trunk.toml
Normal file
16
frontend/Trunk.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[build]
|
||||
target = "index.html"
|
||||
dist = "dist"
|
||||
|
||||
[env]
|
||||
BACKEND_API_URL = "http://localhost:3000/api"
|
||||
|
||||
[watch]
|
||||
watch = ["src", "Cargo.toml", "../calendar-models/src", "styles.css", "index.html"]
|
||||
ignore = ["../backend/", "../target/"]
|
||||
|
||||
[serve]
|
||||
addresses = ["127.0.0.1"]
|
||||
port = 8080
|
||||
open = false
|
||||
|
||||
10
frontend/index.html
Normal file
10
frontend/index.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Calendar App</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link data-trunk rel="css" href="styles.css">
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
||||
997
frontend/src/app.rs
Normal file
997
frontend/src/app.rs
Normal file
@@ -0,0 +1,997 @@
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::*;
|
||||
use gloo_storage::{LocalStorage, Storage};
|
||||
use web_sys::MouseEvent;
|
||||
use crate::components::{Sidebar, ViewMode, Theme, CreateCalendarModal, ContextMenu, EventContextMenu, CalendarContextMenu, CreateEventModal, EventCreationData, RouteHandler, EventStatus, EventClass, ReminderType, RecurrenceType, DeleteAction};
|
||||
use crate::services::{CalendarService, calendar_service::UserInfo};
|
||||
use crate::models::ical::VEvent;
|
||||
use chrono::NaiveDate;
|
||||
|
||||
fn get_theme_event_colors() -> Vec<String> {
|
||||
if let Some(window) = web_sys::window() {
|
||||
if let Some(document) = window.document() {
|
||||
if let Some(root) = document.document_element() {
|
||||
if let Ok(Some(computed_style)) = window.get_computed_style(&root) {
|
||||
if let Ok(colors_string) = computed_style.get_property_value("--event-colors") {
|
||||
if !colors_string.is_empty() {
|
||||
return colors_string
|
||||
.split(',')
|
||||
.map(|color| color.trim().to_string())
|
||||
.filter(|color| !color.is_empty())
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vec![
|
||||
"#3B82F6".to_string(), "#10B981".to_string(), "#F59E0B".to_string(), "#EF4444".to_string(),
|
||||
"#8B5CF6".to_string(), "#06B6D4".to_string(), "#84CC16".to_string(), "#F97316".to_string(),
|
||||
"#EC4899".to_string(), "#6366F1".to_string(), "#14B8A6".to_string(), "#F3B806".to_string(),
|
||||
"#8B5A2B".to_string(), "#6B7280".to_string(), "#DC2626".to_string(), "#7C3AED".to_string()
|
||||
]
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn App() -> Html {
|
||||
let auth_token = use_state(|| -> Option<String> {
|
||||
LocalStorage::get("auth_token").ok()
|
||||
});
|
||||
|
||||
let user_info = use_state(|| -> Option<UserInfo> { None });
|
||||
let color_picker_open = use_state(|| -> Option<String> { None });
|
||||
let create_modal_open = use_state(|| false);
|
||||
let context_menu_open = use_state(|| false);
|
||||
let context_menu_pos = use_state(|| (0i32, 0i32));
|
||||
let context_menu_calendar_path = use_state(|| -> Option<String> { None });
|
||||
let event_context_menu_open = use_state(|| false);
|
||||
let event_context_menu_pos = use_state(|| (0i32, 0i32));
|
||||
let event_context_menu_event = use_state(|| -> Option<VEvent> { None });
|
||||
let calendar_context_menu_open = use_state(|| false);
|
||||
let calendar_context_menu_pos = use_state(|| (0i32, 0i32));
|
||||
let calendar_context_menu_date = use_state(|| -> Option<NaiveDate> { None });
|
||||
let create_event_modal_open = use_state(|| false);
|
||||
let selected_date_for_event = use_state(|| -> Option<NaiveDate> { None });
|
||||
|
||||
// Calendar view state - load from localStorage if available
|
||||
let current_view = use_state(|| {
|
||||
// Try to load saved view mode from localStorage
|
||||
if let Ok(saved_view) = LocalStorage::get::<String>("calendar_view_mode") {
|
||||
match saved_view.as_str() {
|
||||
"week" => ViewMode::Week,
|
||||
_ => ViewMode::Month,
|
||||
}
|
||||
} else {
|
||||
ViewMode::Month // Default to month view
|
||||
}
|
||||
});
|
||||
|
||||
// Theme state - load from localStorage if available
|
||||
let current_theme = use_state(|| {
|
||||
// Try to load saved theme from localStorage
|
||||
if let Ok(saved_theme) = LocalStorage::get::<String>("calendar_theme") {
|
||||
Theme::from_value(&saved_theme)
|
||||
} else {
|
||||
Theme::Default // Default theme
|
||||
}
|
||||
});
|
||||
|
||||
let available_colors = use_state(|| get_theme_event_colors());
|
||||
|
||||
let on_login = {
|
||||
let auth_token = auth_token.clone();
|
||||
Callback::from(move |token: String| {
|
||||
auth_token.set(Some(token));
|
||||
})
|
||||
};
|
||||
|
||||
let on_logout = {
|
||||
let auth_token = auth_token.clone();
|
||||
let user_info = user_info.clone();
|
||||
Callback::from(move |_| {
|
||||
let _ = LocalStorage::delete("auth_token");
|
||||
auth_token.set(None);
|
||||
user_info.set(None);
|
||||
})
|
||||
};
|
||||
|
||||
let on_view_change = {
|
||||
let current_view = current_view.clone();
|
||||
Callback::from(move |new_view: ViewMode| {
|
||||
// Save view mode to localStorage
|
||||
let view_string = match new_view {
|
||||
ViewMode::Month => "month",
|
||||
ViewMode::Week => "week",
|
||||
};
|
||||
let _ = LocalStorage::set("calendar_view_mode", view_string);
|
||||
|
||||
// Update state
|
||||
current_view.set(new_view);
|
||||
})
|
||||
};
|
||||
|
||||
let on_theme_change = {
|
||||
let current_theme = current_theme.clone();
|
||||
let available_colors = available_colors.clone();
|
||||
Callback::from(move |new_theme: Theme| {
|
||||
// Save theme to localStorage
|
||||
let _ = LocalStorage::set("calendar_theme", new_theme.value());
|
||||
|
||||
// Apply theme to document root
|
||||
if let Some(document) = web_sys::window().and_then(|w| w.document()) {
|
||||
if let Some(root) = document.document_element() {
|
||||
let _ = root.set_attribute("data-theme", new_theme.value());
|
||||
}
|
||||
}
|
||||
|
||||
// Update state
|
||||
current_theme.set(new_theme);
|
||||
|
||||
// Update available colors after theme change
|
||||
available_colors.set(get_theme_event_colors());
|
||||
})
|
||||
};
|
||||
|
||||
// Apply initial theme on mount
|
||||
{
|
||||
let current_theme = current_theme.clone();
|
||||
use_effect_with((), move |_| {
|
||||
let theme = (*current_theme).clone();
|
||||
if let Some(document) = web_sys::window().and_then(|w| w.document()) {
|
||||
if let Some(root) = document.document_element() {
|
||||
let _ = root.set_attribute("data-theme", theme.value());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Fetch user info when token is available
|
||||
{
|
||||
let user_info = user_info.clone();
|
||||
let auth_token = auth_token.clone();
|
||||
|
||||
use_effect_with((*auth_token).clone(), move |token| {
|
||||
if let Some(token) = token {
|
||||
let user_info = user_info.clone();
|
||||
let token = token.clone();
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
if !password.is_empty() {
|
||||
match calendar_service.fetch_user_info(&token, &password).await {
|
||||
Ok(mut info) => {
|
||||
if let Ok(saved_colors_json) = LocalStorage::get::<String>("calendar_colors") {
|
||||
if let Ok(saved_info) = serde_json::from_str::<UserInfo>(&saved_colors_json) {
|
||||
for saved_cal in &saved_info.calendars {
|
||||
for cal in &mut info.calendars {
|
||||
if cal.path == saved_cal.path {
|
||||
cal.color = saved_cal.color.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
user_info.set(Some(info));
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::log_1(&format!("Failed to fetch user info: {}", err).into());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
user_info.set(None);
|
||||
}
|
||||
|
||||
|| ()
|
||||
});
|
||||
}
|
||||
|
||||
let on_outside_click = {
|
||||
let color_picker_open = color_picker_open.clone();
|
||||
let context_menu_open = context_menu_open.clone();
|
||||
let event_context_menu_open = event_context_menu_open.clone();
|
||||
let calendar_context_menu_open = calendar_context_menu_open.clone();
|
||||
Callback::from(move |e: MouseEvent| {
|
||||
// Check if any context menu or color picker is open
|
||||
let any_menu_open = color_picker_open.is_some() ||
|
||||
*context_menu_open ||
|
||||
*event_context_menu_open ||
|
||||
*calendar_context_menu_open;
|
||||
|
||||
if any_menu_open {
|
||||
// Prevent the default action and stop event propagation
|
||||
e.prevent_default();
|
||||
e.stop_propagation();
|
||||
}
|
||||
|
||||
// Close all open menus/pickers
|
||||
color_picker_open.set(None);
|
||||
context_menu_open.set(false);
|
||||
event_context_menu_open.set(false);
|
||||
calendar_context_menu_open.set(false);
|
||||
})
|
||||
};
|
||||
|
||||
// Compute if any context menu is open
|
||||
let any_context_menu_open = color_picker_open.is_some() ||
|
||||
*context_menu_open ||
|
||||
*event_context_menu_open ||
|
||||
*calendar_context_menu_open;
|
||||
|
||||
let on_color_change = {
|
||||
let user_info = user_info.clone();
|
||||
let color_picker_open = color_picker_open.clone();
|
||||
Callback::from(move |(calendar_path, color): (String, String)| {
|
||||
if let Some(mut info) = (*user_info).clone() {
|
||||
for calendar in &mut info.calendars {
|
||||
if calendar.path == calendar_path {
|
||||
calendar.color = color.clone();
|
||||
break;
|
||||
}
|
||||
}
|
||||
user_info.set(Some(info.clone()));
|
||||
|
||||
if let Ok(json) = serde_json::to_string(&info) {
|
||||
let _ = LocalStorage::set("calendar_colors", json);
|
||||
}
|
||||
}
|
||||
color_picker_open.set(None);
|
||||
})
|
||||
};
|
||||
|
||||
let on_color_picker_toggle = {
|
||||
let color_picker_open = color_picker_open.clone();
|
||||
Callback::from(move |calendar_path: String| {
|
||||
if color_picker_open.as_ref() == Some(&calendar_path) {
|
||||
color_picker_open.set(None);
|
||||
} else {
|
||||
color_picker_open.set(Some(calendar_path));
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_calendar_context_menu = {
|
||||
let context_menu_open = context_menu_open.clone();
|
||||
let context_menu_pos = context_menu_pos.clone();
|
||||
let context_menu_calendar_path = context_menu_calendar_path.clone();
|
||||
Callback::from(move |(event, calendar_path): (MouseEvent, String)| {
|
||||
context_menu_open.set(true);
|
||||
context_menu_pos.set((event.client_x(), event.client_y()));
|
||||
context_menu_calendar_path.set(Some(calendar_path));
|
||||
})
|
||||
};
|
||||
|
||||
let on_event_context_menu = {
|
||||
let event_context_menu_open = event_context_menu_open.clone();
|
||||
let event_context_menu_pos = event_context_menu_pos.clone();
|
||||
let event_context_menu_event = event_context_menu_event.clone();
|
||||
Callback::from(move |(event, calendar_event): (MouseEvent, VEvent)| {
|
||||
event_context_menu_open.set(true);
|
||||
event_context_menu_pos.set((event.client_x(), event.client_y()));
|
||||
event_context_menu_event.set(Some(calendar_event));
|
||||
})
|
||||
};
|
||||
|
||||
let on_calendar_date_context_menu = {
|
||||
let calendar_context_menu_open = calendar_context_menu_open.clone();
|
||||
let calendar_context_menu_pos = calendar_context_menu_pos.clone();
|
||||
let calendar_context_menu_date = calendar_context_menu_date.clone();
|
||||
Callback::from(move |(event, date): (MouseEvent, NaiveDate)| {
|
||||
calendar_context_menu_open.set(true);
|
||||
calendar_context_menu_pos.set((event.client_x(), event.client_y()));
|
||||
calendar_context_menu_date.set(Some(date));
|
||||
})
|
||||
};
|
||||
|
||||
let on_create_event_click = {
|
||||
let create_event_modal_open = create_event_modal_open.clone();
|
||||
let selected_date_for_event = selected_date_for_event.clone();
|
||||
let calendar_context_menu_date = calendar_context_menu_date.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
create_event_modal_open.set(true);
|
||||
selected_date_for_event.set((*calendar_context_menu_date).clone());
|
||||
})
|
||||
};
|
||||
|
||||
let on_event_create = {
|
||||
let create_event_modal_open = create_event_modal_open.clone();
|
||||
let auth_token = auth_token.clone();
|
||||
Callback::from(move |event_data: EventCreationData| {
|
||||
web_sys::console::log_1(&format!("Creating event: {:?}", event_data).into());
|
||||
create_event_modal_open.set(false);
|
||||
|
||||
if let Some(_token) = (*auth_token).clone() {
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let _calendar_service = CalendarService::new();
|
||||
|
||||
// Get CalDAV password from storage
|
||||
let _password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
let params = event_data.to_create_event_params();
|
||||
let create_result = _calendar_service.create_event(
|
||||
&_token,
|
||||
&_password,
|
||||
params.0, // title
|
||||
params.1, // description
|
||||
params.2, // start_date
|
||||
params.3, // start_time
|
||||
params.4, // end_date
|
||||
params.5, // end_time
|
||||
params.6, // location
|
||||
params.7, // all_day
|
||||
params.8, // status
|
||||
params.9, // class
|
||||
params.10, // priority
|
||||
params.11, // organizer
|
||||
params.12, // attendees
|
||||
params.13, // categories
|
||||
params.14, // reminder
|
||||
params.15, // recurrence
|
||||
params.16, // recurrence_days
|
||||
params.17 // calendar_path
|
||||
).await;
|
||||
match create_result {
|
||||
Ok(_) => {
|
||||
web_sys::console::log_1(&"Event created successfully".into());
|
||||
// Trigger a page reload to refresh events from all calendars
|
||||
// TODO: This could be improved to do a more targeted refresh
|
||||
web_sys::window().unwrap().location().reload().unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::error_1(&format!("Failed to create event: {}", err).into());
|
||||
web_sys::window().unwrap().alert_with_message(&format!("Failed to create event: {}", err)).unwrap();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_event_update = {
|
||||
let auth_token = auth_token.clone();
|
||||
Callback::from(move |(original_event, new_start, new_end, preserve_rrule, until_date): (VEvent, chrono::NaiveDateTime, chrono::NaiveDateTime, bool, Option<chrono::DateTime<chrono::Utc>>)| {
|
||||
web_sys::console::log_1(&format!("Updating event: {} to new times: {} - {}",
|
||||
original_event.uid,
|
||||
new_start.format("%Y-%m-%d %H:%M"),
|
||||
new_end.format("%Y-%m-%d %H:%M")).into());
|
||||
|
||||
// Use the original UID for all updates
|
||||
let backend_uid = original_event.uid.clone();
|
||||
|
||||
if let Some(token) = (*auth_token).clone() {
|
||||
let original_event = original_event.clone();
|
||||
let backend_uid = backend_uid.clone();
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
// Get CalDAV password from storage
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
// Convert local times to UTC for backend storage
|
||||
let start_utc = new_start.and_local_timezone(chrono::Local).unwrap().to_utc();
|
||||
let end_utc = new_end.and_local_timezone(chrono::Local).unwrap().to_utc();
|
||||
|
||||
// Format UTC date and time strings for backend
|
||||
let start_date = start_utc.format("%Y-%m-%d").to_string();
|
||||
let start_time = start_utc.format("%H:%M").to_string();
|
||||
let end_date = end_utc.format("%Y-%m-%d").to_string();
|
||||
let end_time = end_utc.format("%H:%M").to_string();
|
||||
|
||||
// Convert existing event data to string formats for the API
|
||||
let status_str = match original_event.status {
|
||||
Some(crate::models::ical::EventStatus::Tentative) => "TENTATIVE".to_string(),
|
||||
Some(crate::models::ical::EventStatus::Confirmed) => "CONFIRMED".to_string(),
|
||||
Some(crate::models::ical::EventStatus::Cancelled) => "CANCELLED".to_string(),
|
||||
None => "CONFIRMED".to_string(), // Default status
|
||||
};
|
||||
|
||||
let class_str = match original_event.class {
|
||||
Some(crate::models::ical::EventClass::Public) => "PUBLIC".to_string(),
|
||||
Some(crate::models::ical::EventClass::Private) => "PRIVATE".to_string(),
|
||||
Some(crate::models::ical::EventClass::Confidential) => "CONFIDENTIAL".to_string(),
|
||||
None => "PUBLIC".to_string(), // Default class
|
||||
};
|
||||
|
||||
// Convert reminders to string format
|
||||
let reminder_str = if !original_event.alarms.is_empty() {
|
||||
// Convert from VAlarm to minutes before
|
||||
"15".to_string() // TODO: Convert VAlarm trigger to minutes
|
||||
} else {
|
||||
"".to_string()
|
||||
};
|
||||
|
||||
// Handle recurrence (keep existing)
|
||||
let recurrence_str = original_event.rrule.unwrap_or_default();
|
||||
let recurrence_days = vec![false; 7]; // Default - could be enhanced to parse existing recurrence
|
||||
|
||||
match calendar_service.update_event(
|
||||
&token,
|
||||
&password,
|
||||
backend_uid,
|
||||
original_event.summary.unwrap_or_default(),
|
||||
original_event.description.unwrap_or_default(),
|
||||
start_date,
|
||||
start_time,
|
||||
end_date,
|
||||
end_time,
|
||||
original_event.location.unwrap_or_default(),
|
||||
original_event.all_day,
|
||||
status_str,
|
||||
class_str,
|
||||
original_event.priority,
|
||||
original_event.organizer.as_ref().map(|o| o.cal_address.clone()).unwrap_or_default(),
|
||||
original_event.attendees.iter().map(|a| a.cal_address.clone()).collect::<Vec<_>>().join(","),
|
||||
original_event.categories.join(","),
|
||||
reminder_str,
|
||||
recurrence_str,
|
||||
recurrence_days,
|
||||
original_event.calendar_path,
|
||||
original_event.exdate.clone(),
|
||||
if preserve_rrule {
|
||||
Some("update_series".to_string())
|
||||
} else {
|
||||
Some("this_and_future".to_string())
|
||||
},
|
||||
until_date
|
||||
).await {
|
||||
Ok(_) => {
|
||||
web_sys::console::log_1(&"Event updated successfully".into());
|
||||
// Add small delay before reload to let any pending requests complete
|
||||
wasm_bindgen_futures::spawn_local(async {
|
||||
gloo_timers::future::sleep(std::time::Duration::from_millis(100)).await;
|
||||
web_sys::window().unwrap().location().reload().unwrap();
|
||||
});
|
||||
}
|
||||
Err(err) => {
|
||||
// Check if this is a network error that occurred after success
|
||||
let err_str = format!("{}", err);
|
||||
if err_str.contains("Failed to fetch") || err_str.contains("Network request failed") {
|
||||
web_sys::console::log_1(&"Update may have succeeded despite network error, reloading...".into());
|
||||
// Still reload as the update likely succeeded
|
||||
wasm_bindgen_futures::spawn_local(async {
|
||||
gloo_timers::future::sleep(std::time::Duration::from_millis(200)).await;
|
||||
web_sys::window().unwrap().location().reload().unwrap();
|
||||
});
|
||||
} else {
|
||||
web_sys::console::error_1(&format!("Failed to update event: {}", err).into());
|
||||
web_sys::window().unwrap().alert_with_message(&format!("Failed to update event: {}", err)).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let refresh_calendars = {
|
||||
let auth_token = auth_token.clone();
|
||||
let user_info = user_info.clone();
|
||||
Callback::from(move |_| {
|
||||
if let Some(token) = (*auth_token).clone() {
|
||||
let user_info = user_info.clone();
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
match calendar_service.fetch_user_info(&token, &password).await {
|
||||
Ok(mut info) => {
|
||||
if let Ok(saved_colors_json) = LocalStorage::get::<String>("calendar_colors") {
|
||||
if let Ok(saved_info) = serde_json::from_str::<UserInfo>(&saved_colors_json) {
|
||||
for saved_cal in &saved_info.calendars {
|
||||
for cal in &mut info.calendars {
|
||||
if cal.path == saved_cal.path {
|
||||
cal.color = saved_cal.color.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
user_info.set(Some(info));
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::log_1(&format!("Failed to refresh calendars: {}", err).into());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<BrowserRouter>
|
||||
<div class="app" onclick={on_outside_click}>
|
||||
{
|
||||
if auth_token.is_some() {
|
||||
html! {
|
||||
<>
|
||||
<Sidebar
|
||||
user_info={(*user_info).clone()}
|
||||
on_logout={on_logout}
|
||||
on_create_calendar={Callback::from({
|
||||
let create_modal_open = create_modal_open.clone();
|
||||
move |_| create_modal_open.set(true)
|
||||
})}
|
||||
color_picker_open={(*color_picker_open).clone()}
|
||||
on_color_change={on_color_change}
|
||||
on_color_picker_toggle={on_color_picker_toggle}
|
||||
available_colors={(*available_colors).clone()}
|
||||
on_calendar_context_menu={on_calendar_context_menu}
|
||||
current_view={(*current_view).clone()}
|
||||
on_view_change={on_view_change}
|
||||
current_theme={(*current_theme).clone()}
|
||||
on_theme_change={on_theme_change}
|
||||
/>
|
||||
<main class="app-main">
|
||||
<RouteHandler
|
||||
auth_token={(*auth_token).clone()}
|
||||
user_info={(*user_info).clone()}
|
||||
on_login={on_login.clone()}
|
||||
on_event_context_menu={Some(on_event_context_menu.clone())}
|
||||
on_calendar_context_menu={Some(on_calendar_date_context_menu.clone())}
|
||||
view={(*current_view).clone()}
|
||||
on_create_event_request={Some(on_event_create.clone())}
|
||||
on_event_update_request={Some(on_event_update.clone())}
|
||||
context_menus_open={any_context_menu_open}
|
||||
/>
|
||||
</main>
|
||||
</>
|
||||
}
|
||||
} else {
|
||||
html! {
|
||||
<div class="login-layout">
|
||||
<RouteHandler
|
||||
auth_token={(*auth_token).clone()}
|
||||
user_info={(*user_info).clone()}
|
||||
on_login={on_login.clone()}
|
||||
on_event_context_menu={Some(on_event_context_menu.clone())}
|
||||
on_calendar_context_menu={Some(on_calendar_date_context_menu.clone())}
|
||||
on_event_update_request={Some(on_event_update.clone())}
|
||||
on_create_event_request={Some(on_event_create.clone())}
|
||||
context_menus_open={any_context_menu_open}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<CreateCalendarModal
|
||||
is_open={*create_modal_open}
|
||||
on_close={Callback::from({
|
||||
let create_modal_open = create_modal_open.clone();
|
||||
move |_| create_modal_open.set(false)
|
||||
})}
|
||||
on_create={Callback::from({
|
||||
let auth_token = auth_token.clone();
|
||||
let refresh_calendars = refresh_calendars.clone();
|
||||
let create_modal_open = create_modal_open.clone();
|
||||
move |(name, description, color): (String, Option<String>, Option<String>)| {
|
||||
if let Some(token) = (*auth_token).clone() {
|
||||
let refresh_calendars = refresh_calendars.clone();
|
||||
let create_modal_open = create_modal_open.clone();
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
match calendar_service.create_calendar(&token, &password, name, description, color).await {
|
||||
Ok(_) => {
|
||||
web_sys::console::log_1(&"Calendar created successfully!".into());
|
||||
refresh_calendars.emit(());
|
||||
create_modal_open.set(false);
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::log_1(&format!("Failed to create calendar: {}", err).into());
|
||||
create_modal_open.set(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
})}
|
||||
available_colors={available_colors.iter().map(|c| c.to_string()).collect::<Vec<_>>()}
|
||||
/>
|
||||
|
||||
<ContextMenu
|
||||
is_open={*context_menu_open}
|
||||
x={context_menu_pos.0}
|
||||
y={context_menu_pos.1}
|
||||
on_close={Callback::from({
|
||||
let context_menu_open = context_menu_open.clone();
|
||||
move |_| context_menu_open.set(false)
|
||||
})}
|
||||
on_delete={Callback::from({
|
||||
let auth_token = auth_token.clone();
|
||||
let context_menu_calendar_path = context_menu_calendar_path.clone();
|
||||
let refresh_calendars = refresh_calendars.clone();
|
||||
move |_: MouseEvent| {
|
||||
if let (Some(token), Some(calendar_path)) = ((*auth_token).clone(), (*context_menu_calendar_path).clone()) {
|
||||
let refresh_calendars = refresh_calendars.clone();
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
match calendar_service.delete_calendar(&token, &password, calendar_path).await {
|
||||
Ok(_) => {
|
||||
web_sys::console::log_1(&"Calendar deleted successfully!".into());
|
||||
refresh_calendars.emit(());
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::log_1(&format!("Failed to delete calendar: {}", err).into());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
})}
|
||||
/>
|
||||
|
||||
<EventContextMenu
|
||||
is_open={*event_context_menu_open}
|
||||
x={event_context_menu_pos.0}
|
||||
y={event_context_menu_pos.1}
|
||||
event={(*event_context_menu_event).clone()}
|
||||
on_close={Callback::from({
|
||||
let event_context_menu_open = event_context_menu_open.clone();
|
||||
move |_| event_context_menu_open.set(false)
|
||||
})}
|
||||
on_edit={Callback::from({
|
||||
let _event_context_menu_event = event_context_menu_event.clone();
|
||||
let event_context_menu_open = event_context_menu_open.clone();
|
||||
let create_event_modal_open = create_event_modal_open.clone();
|
||||
move |_| {
|
||||
// Close the context menu and open the edit modal
|
||||
event_context_menu_open.set(false);
|
||||
create_event_modal_open.set(true);
|
||||
}
|
||||
})}
|
||||
on_delete={Callback::from({
|
||||
let auth_token = auth_token.clone();
|
||||
let event_context_menu_event = event_context_menu_event.clone();
|
||||
let event_context_menu_open = event_context_menu_open.clone();
|
||||
let refresh_calendars = refresh_calendars.clone();
|
||||
move |delete_action: DeleteAction| {
|
||||
if let (Some(token), Some(event)) = ((*auth_token).clone(), (*event_context_menu_event).clone()) {
|
||||
let _refresh_calendars = refresh_calendars.clone();
|
||||
let event_context_menu_open = event_context_menu_open.clone();
|
||||
|
||||
// Log the delete action for now - we'll implement different behaviors later
|
||||
match delete_action {
|
||||
DeleteAction::DeleteThis => web_sys::console::log_1(&"Delete this event".into()),
|
||||
DeleteAction::DeleteFollowing => web_sys::console::log_1(&"Delete following events".into()),
|
||||
DeleteAction::DeleteSeries => web_sys::console::log_1(&"Delete entire series".into()),
|
||||
}
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
if let (Some(calendar_path), Some(event_href)) = (&event.calendar_path, &event.href) {
|
||||
// Convert delete action to string and get occurrence date
|
||||
let action_str = match delete_action {
|
||||
DeleteAction::DeleteThis => "delete_this".to_string(),
|
||||
DeleteAction::DeleteFollowing => "delete_following".to_string(),
|
||||
DeleteAction::DeleteSeries => "delete_series".to_string(),
|
||||
};
|
||||
|
||||
// Get the occurrence date from the clicked event
|
||||
let occurrence_date = Some(event.dtstart.date_naive().format("%Y-%m-%d").to_string());
|
||||
|
||||
web_sys::console::log_1(&format!("🔄 Delete action: {}", action_str).into());
|
||||
web_sys::console::log_1(&format!("🔄 Event UID: {}", event.uid).into());
|
||||
web_sys::console::log_1(&format!("🔄 Event start: {}", event.dtstart).into());
|
||||
web_sys::console::log_1(&format!("🔄 Occurrence date: {:?}", occurrence_date).into());
|
||||
|
||||
match calendar_service.delete_event(
|
||||
&token,
|
||||
&password,
|
||||
calendar_path.clone(),
|
||||
event_href.clone(),
|
||||
action_str,
|
||||
occurrence_date
|
||||
).await {
|
||||
Ok(message) => {
|
||||
web_sys::console::log_1(&format!("Delete response: {}", message).into());
|
||||
|
||||
// Show the message to the user to explain what actually happened
|
||||
if message.contains("Warning") {
|
||||
web_sys::window().unwrap().alert_with_message(&message).unwrap();
|
||||
}
|
||||
|
||||
// Close the context menu
|
||||
event_context_menu_open.set(false);
|
||||
// Force a page reload to refresh the calendar events
|
||||
web_sys::window().unwrap().location().reload().unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::log_1(&format!("Failed to delete event: {}", err).into());
|
||||
web_sys::window().unwrap().alert_with_message(&format!("Failed to delete event: {}", err)).unwrap();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
web_sys::console::log_1(&"Missing calendar_path or href - cannot delete event".into());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
})}
|
||||
/>
|
||||
|
||||
<CalendarContextMenu
|
||||
is_open={*calendar_context_menu_open}
|
||||
x={calendar_context_menu_pos.0}
|
||||
y={calendar_context_menu_pos.1}
|
||||
on_close={Callback::from({
|
||||
let calendar_context_menu_open = calendar_context_menu_open.clone();
|
||||
move |_| calendar_context_menu_open.set(false)
|
||||
})}
|
||||
on_create_event={on_create_event_click}
|
||||
/>
|
||||
|
||||
<CreateEventModal
|
||||
is_open={*create_event_modal_open}
|
||||
selected_date={(*selected_date_for_event).clone()}
|
||||
event_to_edit={(*event_context_menu_event).clone()}
|
||||
on_close={Callback::from({
|
||||
let create_event_modal_open = create_event_modal_open.clone();
|
||||
let event_context_menu_event = event_context_menu_event.clone();
|
||||
move |_| {
|
||||
create_event_modal_open.set(false);
|
||||
// Clear the event being edited
|
||||
event_context_menu_event.set(None);
|
||||
}
|
||||
})}
|
||||
on_create={on_event_create}
|
||||
on_update={Callback::from({
|
||||
let auth_token = auth_token.clone();
|
||||
let create_event_modal_open = create_event_modal_open.clone();
|
||||
let event_context_menu_event = event_context_menu_event.clone();
|
||||
move |(original_event, updated_data): (VEvent, EventCreationData)| {
|
||||
web_sys::console::log_1(&format!("Updating event: {:?}", updated_data).into());
|
||||
create_event_modal_open.set(false);
|
||||
event_context_menu_event.set(None);
|
||||
|
||||
if let Some(token) = (*auth_token).clone() {
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
// Get CalDAV password from storage
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
// Convert local times to UTC for backend storage
|
||||
let start_local = updated_data.start_date.and_time(updated_data.start_time);
|
||||
let end_local = updated_data.end_date.and_time(updated_data.end_time);
|
||||
|
||||
let start_utc = start_local.and_local_timezone(chrono::Local).unwrap().to_utc();
|
||||
let end_utc = end_local.and_local_timezone(chrono::Local).unwrap().to_utc();
|
||||
|
||||
// Format UTC date and time strings for backend
|
||||
let start_date = start_utc.format("%Y-%m-%d").to_string();
|
||||
let start_time = start_utc.format("%H:%M").to_string();
|
||||
let end_date = end_utc.format("%Y-%m-%d").to_string();
|
||||
let end_time = end_utc.format("%H:%M").to_string();
|
||||
|
||||
// Convert enums to strings for backend
|
||||
let status_str = match updated_data.status {
|
||||
EventStatus::Tentative => "tentative",
|
||||
EventStatus::Cancelled => "cancelled",
|
||||
_ => "confirmed",
|
||||
}.to_string();
|
||||
|
||||
let class_str = match updated_data.class {
|
||||
EventClass::Private => "private",
|
||||
EventClass::Confidential => "confidential",
|
||||
_ => "public",
|
||||
}.to_string();
|
||||
|
||||
let reminder_str = match updated_data.reminder {
|
||||
ReminderType::Minutes15 => "15min",
|
||||
ReminderType::Minutes30 => "30min",
|
||||
ReminderType::Hour1 => "1hour",
|
||||
ReminderType::Hours2 => "2hours",
|
||||
ReminderType::Day1 => "1day",
|
||||
ReminderType::Days2 => "2days",
|
||||
ReminderType::Week1 => "1week",
|
||||
_ => "none",
|
||||
}.to_string();
|
||||
|
||||
let recurrence_str = match updated_data.recurrence {
|
||||
RecurrenceType::Daily => "daily",
|
||||
RecurrenceType::Weekly => "weekly",
|
||||
RecurrenceType::Monthly => "monthly",
|
||||
RecurrenceType::Yearly => "yearly",
|
||||
_ => "none",
|
||||
}.to_string();
|
||||
|
||||
// Check if the calendar has changed
|
||||
let calendar_changed = original_event.calendar_path.as_ref() != updated_data.selected_calendar.as_ref();
|
||||
|
||||
if calendar_changed {
|
||||
// Calendar changed - need to delete from original and create in new
|
||||
web_sys::console::log_1(&"Calendar changed - performing delete + create".into());
|
||||
|
||||
// First delete from original calendar
|
||||
if let Some(original_calendar_path) = &original_event.calendar_path {
|
||||
if let Some(event_href) = &original_event.href {
|
||||
match calendar_service.delete_event(
|
||||
&token,
|
||||
&password,
|
||||
original_calendar_path.clone(),
|
||||
event_href.clone(),
|
||||
"single".to_string(), // delete single occurrence
|
||||
None
|
||||
).await {
|
||||
Ok(_) => {
|
||||
web_sys::console::log_1(&"Original event deleted successfully".into());
|
||||
|
||||
// Now create the event in the new calendar
|
||||
match calendar_service.create_event(
|
||||
&token,
|
||||
&password,
|
||||
updated_data.title,
|
||||
updated_data.description,
|
||||
start_date,
|
||||
start_time,
|
||||
end_date,
|
||||
end_time,
|
||||
updated_data.location,
|
||||
updated_data.all_day,
|
||||
status_str,
|
||||
class_str,
|
||||
updated_data.priority,
|
||||
updated_data.organizer,
|
||||
updated_data.attendees,
|
||||
updated_data.categories,
|
||||
reminder_str,
|
||||
recurrence_str,
|
||||
updated_data.recurrence_days,
|
||||
updated_data.selected_calendar
|
||||
).await {
|
||||
Ok(_) => {
|
||||
web_sys::console::log_1(&"Event moved to new calendar successfully".into());
|
||||
// Trigger a page reload to refresh events from all calendars
|
||||
web_sys::window().unwrap().location().reload().unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::error_1(&format!("Failed to create event in new calendar: {}", err).into());
|
||||
web_sys::window().unwrap().alert_with_message(&format!("Failed to move event to new calendar: {}", err)).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::error_1(&format!("Failed to delete original event: {}", err).into());
|
||||
web_sys::window().unwrap().alert_with_message(&format!("Failed to delete original event: {}", err)).unwrap();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
web_sys::console::error_1(&"Original event missing href for deletion".into());
|
||||
web_sys::window().unwrap().alert_with_message("Cannot move event - original event missing href").unwrap();
|
||||
}
|
||||
} else {
|
||||
web_sys::console::error_1(&"Original event missing calendar_path for deletion".into());
|
||||
web_sys::window().unwrap().alert_with_message("Cannot move event - original event missing calendar path").unwrap();
|
||||
}
|
||||
} else {
|
||||
// Calendar hasn't changed - normal update
|
||||
match calendar_service.update_event(
|
||||
&token,
|
||||
&password,
|
||||
original_event.uid,
|
||||
updated_data.title,
|
||||
updated_data.description,
|
||||
start_date,
|
||||
start_time,
|
||||
end_date,
|
||||
end_time,
|
||||
updated_data.location,
|
||||
updated_data.all_day,
|
||||
status_str,
|
||||
class_str,
|
||||
updated_data.priority,
|
||||
updated_data.organizer,
|
||||
updated_data.attendees,
|
||||
updated_data.categories,
|
||||
reminder_str,
|
||||
recurrence_str,
|
||||
updated_data.recurrence_days,
|
||||
updated_data.selected_calendar,
|
||||
original_event.exdate.clone(),
|
||||
Some("update_series".to_string()), // This is for event edit modal, preserve original RRULE
|
||||
None // No until_date for edit modal
|
||||
).await {
|
||||
Ok(_) => {
|
||||
web_sys::console::log_1(&"Event updated successfully".into());
|
||||
// Trigger a page reload to refresh events from all calendars
|
||||
web_sys::window().unwrap().location().reload().unwrap();
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::error_1(&format!("Failed to update event: {}", err).into());
|
||||
web_sys::window().unwrap().alert_with_message(&format!("Failed to update event: {}", err)).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
})}
|
||||
available_calendars={user_info.as_ref().map(|ui| ui.calendars.clone()).unwrap_or_default()}
|
||||
/>
|
||||
</div>
|
||||
</BrowserRouter>
|
||||
}
|
||||
}
|
||||
95
frontend/src/auth.rs
Normal file
95
frontend/src/auth.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
// Frontend authentication module - connects to backend API
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::JsCast;
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
use web_sys::{Request, RequestInit, RequestMode, Response};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CalDAVLoginRequest {
|
||||
pub server_url: String,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct AuthResponse {
|
||||
pub token: String,
|
||||
pub username: String,
|
||||
pub server_url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ApiErrorResponse {
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
// Frontend auth service - connects to backend API
|
||||
pub struct AuthService {
|
||||
base_url: String,
|
||||
}
|
||||
|
||||
impl AuthService {
|
||||
pub fn new() -> Self {
|
||||
// Get backend URL from environment variable at compile time, fallback to localhost
|
||||
let base_url = option_env!("BACKEND_API_URL")
|
||||
.unwrap_or("http://localhost:3000/api")
|
||||
.to_string();
|
||||
|
||||
Self { base_url }
|
||||
}
|
||||
|
||||
pub async fn login(&self, request: CalDAVLoginRequest) -> Result<AuthResponse, String> {
|
||||
self.post_json("/auth/login", &request).await
|
||||
}
|
||||
|
||||
// Helper method for POST requests with JSON body
|
||||
async fn post_json<T: Serialize, R: for<'de> Deserialize<'de>>(
|
||||
&self,
|
||||
endpoint: &str,
|
||||
body: &T,
|
||||
) -> Result<R, String> {
|
||||
let window = web_sys::window().ok_or("No global window exists")?;
|
||||
|
||||
let json_body = serde_json::to_string(body)
|
||||
.map_err(|e| format!("JSON serialization failed: {}", e))?;
|
||||
|
||||
let opts = RequestInit::new();
|
||||
opts.set_method("POST");
|
||||
opts.set_mode(RequestMode::Cors);
|
||||
opts.set_body(&wasm_bindgen::JsValue::from_str(&json_body));
|
||||
|
||||
let url = format!("{}{}", self.base_url, endpoint);
|
||||
let request = Request::new_with_str_and_init(&url, &opts)
|
||||
.map_err(|e| format!("Request creation failed: {:?}", e))?;
|
||||
|
||||
request.headers().set("Content-Type", "application/json")
|
||||
.map_err(|e| format!("Header setting failed: {:?}", e))?;
|
||||
|
||||
let resp_value = JsFuture::from(window.fetch_with_request(&request))
|
||||
.await
|
||||
.map_err(|e| format!("Network request failed: {:?}", e))?;
|
||||
|
||||
let resp: Response = resp_value.dyn_into()
|
||||
.map_err(|e| format!("Response cast failed: {:?}", e))?;
|
||||
|
||||
let text = JsFuture::from(resp.text()
|
||||
.map_err(|e| format!("Text extraction failed: {:?}", e))?)
|
||||
.await
|
||||
.map_err(|e| format!("Text promise failed: {:?}", e))?;
|
||||
|
||||
let text_string = text.as_string()
|
||||
.ok_or("Response text is not a string")?;
|
||||
|
||||
if resp.ok() {
|
||||
serde_json::from_str::<R>(&text_string)
|
||||
.map_err(|e| format!("JSON parsing failed: {}", e))
|
||||
} else {
|
||||
// Try to parse error response
|
||||
if let Ok(error_response) = serde_json::from_str::<ApiErrorResponse>(&text_string) {
|
||||
Err(error_response.error)
|
||||
} else {
|
||||
Err(format!("Request failed with status {}", resp.status()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
639
frontend/src/calendar.rs
Normal file
639
frontend/src/calendar.rs
Normal file
@@ -0,0 +1,639 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Represents a calendar event with all its properties
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct CalendarEvent {
|
||||
/// Unique identifier for the event (UID field in iCal)
|
||||
pub uid: String,
|
||||
|
||||
/// Summary/title of the event
|
||||
pub summary: Option<String>,
|
||||
|
||||
/// Detailed description of the event
|
||||
pub description: Option<String>,
|
||||
|
||||
/// Start date and time of the event
|
||||
pub start: DateTime<Utc>,
|
||||
|
||||
/// End date and time of the event
|
||||
pub end: Option<DateTime<Utc>>,
|
||||
|
||||
/// Location where the event takes place
|
||||
pub location: Option<String>,
|
||||
|
||||
/// Event status (TENTATIVE, CONFIRMED, CANCELLED)
|
||||
pub status: EventStatus,
|
||||
|
||||
/// Event classification (PUBLIC, PRIVATE, CONFIDENTIAL)
|
||||
pub class: EventClass,
|
||||
|
||||
/// Event priority (0-9, where 0 is undefined, 1 is highest, 9 is lowest)
|
||||
pub priority: Option<u8>,
|
||||
|
||||
/// Organizer of the event
|
||||
pub organizer: Option<String>,
|
||||
|
||||
/// List of attendees
|
||||
pub attendees: Vec<String>,
|
||||
|
||||
/// Categories/tags for the event
|
||||
pub categories: Vec<String>,
|
||||
|
||||
/// Date and time when the event was created
|
||||
pub created: Option<DateTime<Utc>>,
|
||||
|
||||
/// Date and time when the event was last modified
|
||||
pub last_modified: Option<DateTime<Utc>>,
|
||||
|
||||
/// Recurrence rule (RRULE)
|
||||
pub recurrence_rule: Option<String>,
|
||||
|
||||
/// All-day event flag
|
||||
pub all_day: bool,
|
||||
|
||||
/// ETag from CalDAV server for conflict detection
|
||||
pub etag: Option<String>,
|
||||
|
||||
/// URL/href of this event on the CalDAV server
|
||||
pub href: Option<String>,
|
||||
}
|
||||
|
||||
/// Event status enumeration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum EventStatus {
|
||||
Tentative,
|
||||
Confirmed,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
impl Default for EventStatus {
|
||||
fn default() -> Self {
|
||||
EventStatus::Confirmed
|
||||
}
|
||||
}
|
||||
|
||||
/// Event classification enumeration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum EventClass {
|
||||
Public,
|
||||
Private,
|
||||
Confidential,
|
||||
}
|
||||
|
||||
impl Default for EventClass {
|
||||
fn default() -> Self {
|
||||
EventClass::Public
|
||||
}
|
||||
}
|
||||
|
||||
/// CalDAV client for fetching and parsing calendar events
|
||||
pub struct CalDAVClient {
|
||||
config: crate::config::CalDAVConfig,
|
||||
http_client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl CalDAVClient {
|
||||
/// Create a new CalDAV client with the given configuration
|
||||
pub fn new(config: crate::config::CalDAVConfig) -> Self {
|
||||
Self {
|
||||
config,
|
||||
http_client: reqwest::Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch calendar events from the CalDAV server
|
||||
///
|
||||
/// This method performs a REPORT request to get calendar data and parses
|
||||
/// the returned iCalendar format into CalendarEvent structs.
|
||||
pub async fn fetch_events(&self, calendar_path: &str) -> Result<Vec<CalendarEvent>, CalDAVError> {
|
||||
// CalDAV REPORT request to get calendar events
|
||||
let report_body = r#"<?xml version="1.0" encoding="utf-8" ?>
|
||||
<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
||||
<d:prop>
|
||||
<d:getetag/>
|
||||
<c:calendar-data/>
|
||||
</d:prop>
|
||||
<c:filter>
|
||||
<c:comp-filter name="VCALENDAR">
|
||||
<c:comp-filter name="VEVENT"/>
|
||||
</c:comp-filter>
|
||||
</c:filter>
|
||||
</c:calendar-query>"#;
|
||||
|
||||
let url = if calendar_path.starts_with("http") {
|
||||
calendar_path.to_string()
|
||||
} else {
|
||||
format!("{}{}", self.config.server_url.trim_end_matches('/'), calendar_path)
|
||||
};
|
||||
|
||||
let response = self.http_client
|
||||
.request(reqwest::Method::from_bytes(b"REPORT").unwrap(), &url)
|
||||
.header("Authorization", format!("Basic {}", self.config.get_basic_auth()))
|
||||
.header("Content-Type", "application/xml")
|
||||
.header("Depth", "1")
|
||||
.header("User-Agent", "calendar-app/0.1.0")
|
||||
.body(report_body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(CalDAVError::RequestError)?;
|
||||
|
||||
if !response.status().is_success() && response.status().as_u16() != 207 {
|
||||
return Err(CalDAVError::ServerError(response.status().as_u16()));
|
||||
}
|
||||
|
||||
let body = response.text().await.map_err(CalDAVError::RequestError)?;
|
||||
self.parse_calendar_response(&body)
|
||||
}
|
||||
|
||||
/// Parse CalDAV XML response containing calendar data
|
||||
fn parse_calendar_response(&self, xml_response: &str) -> Result<Vec<CalendarEvent>, CalDAVError> {
|
||||
let mut events = Vec::new();
|
||||
|
||||
// Extract calendar data from XML response
|
||||
// This is a simplified parser - in production, you'd want a proper XML parser
|
||||
let calendar_data_sections = self.extract_calendar_data(xml_response);
|
||||
|
||||
for calendar_data in calendar_data_sections {
|
||||
if let Ok(parsed_events) = self.parse_ical_data(&calendar_data.data) {
|
||||
for mut event in parsed_events {
|
||||
event.etag = calendar_data.etag.clone();
|
||||
event.href = calendar_data.href.clone();
|
||||
events.push(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
/// Extract calendar data sections from CalDAV XML response
|
||||
fn extract_calendar_data(&self, xml_response: &str) -> Vec<CalendarDataSection> {
|
||||
let mut sections = Vec::new();
|
||||
|
||||
// Simple regex-based extraction (in production, use a proper XML parser)
|
||||
// Look for <d:response> blocks containing calendar data
|
||||
for response_block in xml_response.split("<d:response>").skip(1) {
|
||||
if let Some(end_pos) = response_block.find("</d:response>") {
|
||||
let response_content = &response_block[..end_pos];
|
||||
|
||||
let href = self.extract_xml_content(response_content, "href").unwrap_or_default();
|
||||
let etag = self.extract_xml_content(response_content, "getetag").unwrap_or_default();
|
||||
|
||||
if let Some(calendar_data) = self.extract_xml_content(response_content, "calendar-data") {
|
||||
sections.push(CalendarDataSection {
|
||||
href: if href.is_empty() { None } else { Some(href) },
|
||||
etag: if etag.is_empty() { None } else { Some(etag) },
|
||||
data: calendar_data,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sections
|
||||
}
|
||||
|
||||
/// Extract content from XML tags (simple implementation)
|
||||
fn extract_xml_content(&self, xml: &str, tag: &str) -> Option<String> {
|
||||
// Handle both with and without namespace prefixes
|
||||
let patterns = [
|
||||
format!("<{}>(.*?)</{}>", tag, tag),
|
||||
format!("<{}>(.*?)</.*:{}>", tag, tag),
|
||||
format!("<.*:{}>(.*?)</{}>", tag, tag),
|
||||
format!("<.*:{}>(.*?)</.*:{}>", tag, tag),
|
||||
];
|
||||
|
||||
for pattern in &patterns {
|
||||
if let Ok(re) = regex::Regex::new(pattern) {
|
||||
if let Some(captures) = re.captures(xml) {
|
||||
if let Some(content) = captures.get(1) {
|
||||
return Some(content.as_str().trim().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Parse iCalendar data into CalendarEvent structs
|
||||
fn parse_ical_data(&self, ical_data: &str) -> Result<Vec<CalendarEvent>, CalDAVError> {
|
||||
let mut events = Vec::new();
|
||||
|
||||
// Parse the iCal data using the ical crate
|
||||
let reader = ical::IcalParser::new(ical_data.as_bytes());
|
||||
|
||||
for calendar in reader {
|
||||
let calendar = calendar.map_err(|e| CalDAVError::ParseError(e.to_string()))?;
|
||||
|
||||
for event in calendar.events {
|
||||
if let Ok(calendar_event) = self.parse_ical_event(event) {
|
||||
events.push(calendar_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
/// Parse a single iCal event into a CalendarEvent struct
|
||||
fn parse_ical_event(&self, event: ical::parser::ical::component::IcalEvent) -> Result<CalendarEvent, CalDAVError> {
|
||||
let mut properties: HashMap<String, String> = HashMap::new();
|
||||
|
||||
// Extract all properties from the event
|
||||
for property in event.properties {
|
||||
properties.insert(property.name.to_uppercase(), property.value.unwrap_or_default());
|
||||
}
|
||||
|
||||
// Required UID field
|
||||
let uid = properties.get("UID")
|
||||
.ok_or_else(|| CalDAVError::ParseError("Missing UID field".to_string()))?
|
||||
.clone();
|
||||
|
||||
// Parse start time (required)
|
||||
let start = properties.get("DTSTART")
|
||||
.ok_or_else(|| CalDAVError::ParseError("Missing DTSTART field".to_string()))?;
|
||||
let start = self.parse_datetime(start, properties.get("DTSTART"))?;
|
||||
|
||||
// Parse end time (optional - use start time if not present)
|
||||
let end = if let Some(dtend) = properties.get("DTEND") {
|
||||
Some(self.parse_datetime(dtend, properties.get("DTEND"))?)
|
||||
} else if let Some(duration) = properties.get("DURATION") {
|
||||
// TODO: Parse duration and add to start time
|
||||
Some(start)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Determine if it's an all-day event
|
||||
let all_day = properties.get("DTSTART")
|
||||
.map(|s| !s.contains("T"))
|
||||
.unwrap_or(false);
|
||||
|
||||
// Parse status
|
||||
let status = properties.get("STATUS")
|
||||
.map(|s| match s.to_uppercase().as_str() {
|
||||
"TENTATIVE" => EventStatus::Tentative,
|
||||
"CANCELLED" => EventStatus::Cancelled,
|
||||
_ => EventStatus::Confirmed,
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
// Parse classification
|
||||
let class = properties.get("CLASS")
|
||||
.map(|s| match s.to_uppercase().as_str() {
|
||||
"PRIVATE" => EventClass::Private,
|
||||
"CONFIDENTIAL" => EventClass::Confidential,
|
||||
_ => EventClass::Public,
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
// Parse priority
|
||||
let priority = properties.get("PRIORITY")
|
||||
.and_then(|s| s.parse::<u8>().ok())
|
||||
.filter(|&p| p <= 9);
|
||||
|
||||
// Parse categories
|
||||
let categories = properties.get("CATEGORIES")
|
||||
.map(|s| s.split(',').map(|c| c.trim().to_string()).collect())
|
||||
.unwrap_or_default();
|
||||
|
||||
// Parse dates
|
||||
let created = properties.get("CREATED")
|
||||
.and_then(|s| self.parse_datetime(s, None).ok());
|
||||
|
||||
let last_modified = properties.get("LAST-MODIFIED")
|
||||
.and_then(|s| self.parse_datetime(s, None).ok());
|
||||
|
||||
Ok(CalendarEvent {
|
||||
uid,
|
||||
summary: properties.get("SUMMARY").cloned(),
|
||||
description: properties.get("DESCRIPTION").cloned(),
|
||||
start,
|
||||
end,
|
||||
location: properties.get("LOCATION").cloned(),
|
||||
status,
|
||||
class,
|
||||
priority,
|
||||
organizer: properties.get("ORGANIZER").cloned(),
|
||||
attendees: Vec::new(), // TODO: Parse attendees
|
||||
categories,
|
||||
created,
|
||||
last_modified,
|
||||
recurrence_rule: properties.get("RRULE").cloned(),
|
||||
all_day,
|
||||
etag: None, // Set by caller
|
||||
href: None, // Set by caller
|
||||
})
|
||||
}
|
||||
|
||||
/// Discover available calendar collections on the server
|
||||
pub async fn discover_calendars(&self) -> Result<Vec<String>, CalDAVError> {
|
||||
// First, try to discover user calendars if we have a calendar path in config
|
||||
if let Some(calendar_path) = &self.config.calendar_path {
|
||||
println!("Using configured calendar path: {}", calendar_path);
|
||||
return Ok(vec![calendar_path.clone()]);
|
||||
}
|
||||
|
||||
println!("No calendar path configured, discovering calendars...");
|
||||
|
||||
// Try different common CalDAV discovery paths
|
||||
let user_calendar_path = format!("/calendars/{}/", self.config.username);
|
||||
let user_dav_calendar_path = format!("/dav.php/calendars/{}/", self.config.username);
|
||||
|
||||
let discovery_paths = vec![
|
||||
"/calendars/",
|
||||
user_calendar_path.as_str(),
|
||||
user_dav_calendar_path.as_str(),
|
||||
"/dav.php/calendars/",
|
||||
];
|
||||
|
||||
let mut all_calendars = Vec::new();
|
||||
|
||||
for path in discovery_paths {
|
||||
println!("Trying discovery path: {}", path);
|
||||
if let Ok(calendars) = self.discover_calendars_at_path(&path).await {
|
||||
println!("Found {} calendar(s) at {}", calendars.len(), path);
|
||||
all_calendars.extend(calendars);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove duplicates
|
||||
all_calendars.sort();
|
||||
all_calendars.dedup();
|
||||
|
||||
Ok(all_calendars)
|
||||
}
|
||||
|
||||
/// Discover calendars at a specific path
|
||||
async fn discover_calendars_at_path(&self, path: &str) -> Result<Vec<String>, CalDAVError> {
|
||||
let propfind_body = r#"<?xml version="1.0" encoding="utf-8" ?>
|
||||
<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
||||
<d:prop>
|
||||
<d:resourcetype />
|
||||
<d:displayname />
|
||||
<c:supported-calendar-component-set />
|
||||
</d:prop>
|
||||
</d:propfind>"#;
|
||||
|
||||
let url = format!("{}{}", self.config.server_url.trim_end_matches('/'), path);
|
||||
|
||||
let response = self.http_client
|
||||
.request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), &url)
|
||||
.header("Authorization", format!("Basic {}", self.config.get_basic_auth()))
|
||||
.header("Content-Type", "application/xml")
|
||||
.header("Depth", "2") // Deeper search to find actual calendars
|
||||
.header("User-Agent", "calendar-app/0.1.0")
|
||||
.body(propfind_body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(CalDAVError::RequestError)?;
|
||||
|
||||
if response.status().as_u16() != 207 {
|
||||
return Err(CalDAVError::ServerError(response.status().as_u16()));
|
||||
}
|
||||
|
||||
let body = response.text().await.map_err(CalDAVError::RequestError)?;
|
||||
println!("Discovery response for {}: {}", path, body);
|
||||
|
||||
let mut calendar_paths = Vec::new();
|
||||
|
||||
// Extract calendar collection URLs from the response
|
||||
for response_block in body.split("<d:response>").skip(1) {
|
||||
if let Some(end_pos) = response_block.find("</d:response>") {
|
||||
let response_content = &response_block[..end_pos];
|
||||
|
||||
// Look for actual calendar collections (not just containers)
|
||||
if response_content.contains("<c:supported-calendar-component-set") ||
|
||||
(response_content.contains("<d:collection/>") &&
|
||||
response_content.contains("calendar")) {
|
||||
if let Some(href) = self.extract_xml_content(response_content, "href") {
|
||||
// Only include actual calendar paths, not container directories
|
||||
if href.ends_with('/') && href.contains("calendar") && !href.ends_with("/calendars/") {
|
||||
calendar_paths.push(href);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(calendar_paths)
|
||||
}
|
||||
|
||||
/// Parse iCal datetime format
|
||||
fn parse_datetime(&self, datetime_str: &str, _original_property: Option<&String>) -> Result<DateTime<Utc>, CalDAVError> {
|
||||
use chrono::TimeZone;
|
||||
|
||||
// Handle different iCal datetime formats
|
||||
let cleaned = datetime_str.replace("TZID=", "").trim().to_string();
|
||||
|
||||
// Try different parsing formats
|
||||
let formats = [
|
||||
"%Y%m%dT%H%M%SZ", // UTC format: 20231225T120000Z
|
||||
"%Y%m%dT%H%M%S", // Local format: 20231225T120000
|
||||
"%Y%m%d", // Date only: 20231225
|
||||
];
|
||||
|
||||
for format in &formats {
|
||||
if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(&cleaned, format) {
|
||||
return Ok(Utc.from_utc_datetime(&dt));
|
||||
}
|
||||
if let Ok(date) = chrono::NaiveDate::parse_from_str(&cleaned, format) {
|
||||
return Ok(Utc.from_utc_datetime(&date.and_hms_opt(0, 0, 0).unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
Err(CalDAVError::ParseError(format!("Unable to parse datetime: {}", datetime_str)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper struct for extracting calendar data from XML responses
|
||||
#[derive(Debug)]
|
||||
struct CalendarDataSection {
|
||||
pub href: Option<String>,
|
||||
pub etag: Option<String>,
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
/// CalDAV-specific error types
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum CalDAVError {
|
||||
#[error("HTTP request failed: {0}")]
|
||||
RequestError(#[from] reqwest::Error),
|
||||
|
||||
#[error("CalDAV server returned error: {0}")]
|
||||
ServerError(u16),
|
||||
|
||||
#[error("Failed to parse calendar data: {0}")]
|
||||
ParseError(String),
|
||||
|
||||
#[error("Configuration error: {0}")]
|
||||
ConfigError(String),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::config::CalDAVConfig;
|
||||
|
||||
/// Integration test that fetches real calendar events from the Baikal server
|
||||
///
|
||||
/// This test requires a valid .env file and a calendar with some events
|
||||
#[tokio::test]
|
||||
async fn test_fetch_calendar_events() {
|
||||
let config = CalDAVConfig::from_env()
|
||||
.expect("Failed to load CalDAV config from environment");
|
||||
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// First discover available calendars using PROPFIND
|
||||
println!("Discovering calendars...");
|
||||
let discovery_result = client.discover_calendars().await;
|
||||
|
||||
match discovery_result {
|
||||
Ok(calendar_paths) => {
|
||||
println!("Found {} calendar collection(s)", calendar_paths.len());
|
||||
|
||||
if calendar_paths.is_empty() {
|
||||
println!("No calendars found - this might be normal for a new server");
|
||||
return;
|
||||
}
|
||||
|
||||
// Try the first available calendar
|
||||
let calendar_path = &calendar_paths[0];
|
||||
println!("Trying to fetch events from: {}", calendar_path);
|
||||
|
||||
match client.fetch_events(calendar_path).await {
|
||||
Ok(events) => {
|
||||
println!("Successfully fetched {} calendar events", events.len());
|
||||
|
||||
for (i, event) in events.iter().take(3).enumerate() {
|
||||
println!("\n--- Event {} ---", i + 1);
|
||||
println!("UID: {}", event.uid);
|
||||
println!("Summary: {:?}", event.summary);
|
||||
println!("Start: {}", event.start);
|
||||
println!("End: {:?}", event.end);
|
||||
println!("All Day: {}", event.all_day);
|
||||
println!("Status: {:?}", event.status);
|
||||
println!("Location: {:?}", event.location);
|
||||
println!("Description: {:?}", event.description);
|
||||
println!("ETag: {:?}", event.etag);
|
||||
println!("HREF: {:?}", event.href);
|
||||
}
|
||||
|
||||
// Validate that events have required fields
|
||||
for event in &events {
|
||||
assert!(!event.uid.is_empty(), "Event UID should not be empty");
|
||||
// All events should have a start time
|
||||
assert!(event.start > DateTime::from_timestamp(0, 0).unwrap(), "Event should have valid start time");
|
||||
}
|
||||
|
||||
println!("\n✓ Calendar event fetching test passed!");
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error fetching events from {}: {:?}", calendar_path, e);
|
||||
println!("This might be normal if the calendar is empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error discovering calendars: {:?}", e);
|
||||
println!("This might be normal if no calendars are set up yet");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Test parsing a sample iCal event
|
||||
#[test]
|
||||
fn test_parse_ical_event() {
|
||||
let sample_ical = r#"BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
PRODID:-//Test//Test//EN
|
||||
BEGIN:VEVENT
|
||||
UID:test-event-123@example.com
|
||||
DTSTART:20231225T120000Z
|
||||
DTEND:20231225T130000Z
|
||||
SUMMARY:Test Event
|
||||
DESCRIPTION:This is a test event
|
||||
LOCATION:Test Location
|
||||
STATUS:CONFIRMED
|
||||
CLASS:PUBLIC
|
||||
PRIORITY:5
|
||||
CREATED:20231220T100000Z
|
||||
LAST-MODIFIED:20231221T150000Z
|
||||
CATEGORIES:work,important
|
||||
END:VEVENT
|
||||
END:VCALENDAR"#;
|
||||
|
||||
let config = CalDAVConfig {
|
||||
server_url: "https://example.com".to_string(),
|
||||
username: "test".to_string(),
|
||||
password: "test".to_string(),
|
||||
calendar_path: None,
|
||||
tasks_path: None,
|
||||
};
|
||||
|
||||
let client = CalDAVClient::new(config);
|
||||
let events = client.parse_ical_data(sample_ical)
|
||||
.expect("Should be able to parse sample iCal data");
|
||||
|
||||
assert_eq!(events.len(), 1);
|
||||
|
||||
let event = &events[0];
|
||||
assert_eq!(event.uid, "test-event-123@example.com");
|
||||
assert_eq!(event.summary, Some("Test Event".to_string()));
|
||||
assert_eq!(event.description, Some("This is a test event".to_string()));
|
||||
assert_eq!(event.location, Some("Test Location".to_string()));
|
||||
assert_eq!(event.status, EventStatus::Confirmed);
|
||||
assert_eq!(event.class, EventClass::Public);
|
||||
assert_eq!(event.priority, Some(5));
|
||||
assert_eq!(event.categories, vec!["work", "important"]);
|
||||
assert!(!event.all_day);
|
||||
|
||||
println!("✓ iCal parsing test passed!");
|
||||
}
|
||||
|
||||
/// Test datetime parsing
|
||||
#[test]
|
||||
fn test_datetime_parsing() {
|
||||
let config = CalDAVConfig {
|
||||
server_url: "https://example.com".to_string(),
|
||||
username: "test".to_string(),
|
||||
password: "test".to_string(),
|
||||
calendar_path: None,
|
||||
tasks_path: None,
|
||||
};
|
||||
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Test UTC format
|
||||
let dt1 = client.parse_datetime("20231225T120000Z", None)
|
||||
.expect("Should parse UTC datetime");
|
||||
println!("Parsed UTC datetime: {}", dt1);
|
||||
|
||||
// Test date-only format (should be treated as all-day)
|
||||
let dt2 = client.parse_datetime("20231225", None)
|
||||
.expect("Should parse date-only");
|
||||
println!("Parsed date-only: {}", dt2);
|
||||
|
||||
// Test local format
|
||||
let dt3 = client.parse_datetime("20231225T120000", None)
|
||||
.expect("Should parse local datetime");
|
||||
println!("Parsed local datetime: {}", dt3);
|
||||
|
||||
println!("✓ Datetime parsing test passed!");
|
||||
}
|
||||
|
||||
/// Test event status parsing
|
||||
#[test]
|
||||
fn test_event_enums() {
|
||||
// Test status parsing
|
||||
assert_eq!(EventStatus::default(), EventStatus::Confirmed);
|
||||
|
||||
// Test class parsing
|
||||
assert_eq!(EventClass::default(), EventClass::Public);
|
||||
|
||||
println!("✓ Event enum tests passed!");
|
||||
}
|
||||
}
|
||||
316
frontend/src/components/calendar.rs
Normal file
316
frontend/src/components/calendar.rs
Normal file
@@ -0,0 +1,316 @@
|
||||
use yew::prelude::*;
|
||||
use chrono::{Datelike, Local, NaiveDate, Duration};
|
||||
use std::collections::HashMap;
|
||||
use web_sys::MouseEvent;
|
||||
use crate::services::calendar_service::UserInfo;
|
||||
use crate::models::ical::VEvent;
|
||||
use crate::components::{EventModal, ViewMode, CalendarHeader, MonthView, WeekView, CreateEventModal, EventCreationData};
|
||||
use gloo_storage::{LocalStorage, Storage};
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct CalendarProps {
|
||||
#[prop_or_default]
|
||||
pub events: HashMap<NaiveDate, Vec<VEvent>>,
|
||||
pub on_event_click: Callback<VEvent>,
|
||||
#[prop_or_default]
|
||||
pub refreshing_event_uid: Option<String>,
|
||||
#[prop_or_default]
|
||||
pub user_info: Option<UserInfo>,
|
||||
#[prop_or_default]
|
||||
pub on_event_context_menu: Option<Callback<(web_sys::MouseEvent, VEvent)>>,
|
||||
#[prop_or_default]
|
||||
pub on_calendar_context_menu: Option<Callback<(web_sys::MouseEvent, NaiveDate)>>,
|
||||
#[prop_or_default]
|
||||
pub view: ViewMode,
|
||||
#[prop_or_default]
|
||||
pub on_create_event_request: Option<Callback<EventCreationData>>,
|
||||
#[prop_or_default]
|
||||
pub on_event_update_request: Option<Callback<(VEvent, chrono::NaiveDateTime, chrono::NaiveDateTime, bool, Option<chrono::DateTime<chrono::Utc>>)>>,
|
||||
#[prop_or_default]
|
||||
pub context_menus_open: bool,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn Calendar(props: &CalendarProps) -> Html {
|
||||
let today = Local::now().date_naive();
|
||||
// Track the currently selected date (the actual day the user has selected)
|
||||
let selected_date = use_state(|| {
|
||||
// Try to load saved selected date from localStorage
|
||||
if let Ok(saved_date_str) = LocalStorage::get::<String>("calendar_selected_date") {
|
||||
if let Ok(saved_date) = NaiveDate::parse_from_str(&saved_date_str, "%Y-%m-%d") {
|
||||
saved_date
|
||||
} else {
|
||||
today
|
||||
}
|
||||
} else {
|
||||
// Check for old key for backward compatibility
|
||||
if let Ok(saved_date_str) = LocalStorage::get::<String>("calendar_current_month") {
|
||||
if let Ok(saved_date) = NaiveDate::parse_from_str(&saved_date_str, "%Y-%m-%d") {
|
||||
saved_date
|
||||
} else {
|
||||
today
|
||||
}
|
||||
} else {
|
||||
today
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Track the display date (what to show in the view)
|
||||
let current_date = use_state(|| {
|
||||
match props.view {
|
||||
ViewMode::Month => selected_date.with_day(1).unwrap_or(*selected_date),
|
||||
ViewMode::Week => *selected_date,
|
||||
}
|
||||
});
|
||||
let selected_event = use_state(|| None::<VEvent>);
|
||||
|
||||
// State for create event modal
|
||||
let show_create_modal = use_state(|| false);
|
||||
let create_event_data = use_state(|| None::<(chrono::NaiveDate, chrono::NaiveTime, chrono::NaiveTime)>);
|
||||
|
||||
// State for time increment snapping (15 or 30 minutes)
|
||||
let time_increment = use_state(|| {
|
||||
// Try to load saved time increment from localStorage
|
||||
if let Ok(saved_increment) = LocalStorage::get::<u32>("calendar_time_increment") {
|
||||
if saved_increment == 15 || saved_increment == 30 {
|
||||
saved_increment
|
||||
} else {
|
||||
15
|
||||
}
|
||||
} else {
|
||||
15
|
||||
}
|
||||
});
|
||||
|
||||
// Handle view mode changes - adjust current_date format when switching between month/week
|
||||
{
|
||||
let current_date = current_date.clone();
|
||||
let selected_date = selected_date.clone();
|
||||
let view = props.view.clone();
|
||||
use_effect_with(view, move |view_mode| {
|
||||
let selected = *selected_date;
|
||||
let new_display_date = match view_mode {
|
||||
ViewMode::Month => selected.with_day(1).unwrap_or(selected),
|
||||
ViewMode::Week => selected, // Show the week containing the selected date
|
||||
};
|
||||
current_date.set(new_display_date);
|
||||
|| {}
|
||||
});
|
||||
}
|
||||
|
||||
let on_prev = {
|
||||
let current_date = current_date.clone();
|
||||
let selected_date = selected_date.clone();
|
||||
let view = props.view.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
let (new_selected, new_display) = match view {
|
||||
ViewMode::Month => {
|
||||
// Go to previous month, select the 1st day
|
||||
let prev_month = *current_date - Duration::days(1);
|
||||
let first_of_prev = prev_month.with_day(1).unwrap();
|
||||
(first_of_prev, first_of_prev)
|
||||
},
|
||||
ViewMode::Week => {
|
||||
// Go to previous week
|
||||
let prev_week = *selected_date - Duration::weeks(1);
|
||||
(prev_week, prev_week)
|
||||
},
|
||||
};
|
||||
selected_date.set(new_selected);
|
||||
current_date.set(new_display);
|
||||
let _ = LocalStorage::set("calendar_selected_date", new_selected.format("%Y-%m-%d").to_string());
|
||||
})
|
||||
};
|
||||
|
||||
let on_next = {
|
||||
let current_date = current_date.clone();
|
||||
let selected_date = selected_date.clone();
|
||||
let view = props.view.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
let (new_selected, new_display) = match view {
|
||||
ViewMode::Month => {
|
||||
// Go to next month, select the 1st day
|
||||
let next_month = if current_date.month() == 12 {
|
||||
NaiveDate::from_ymd_opt(current_date.year() + 1, 1, 1).unwrap()
|
||||
} else {
|
||||
NaiveDate::from_ymd_opt(current_date.year(), current_date.month() + 1, 1).unwrap()
|
||||
};
|
||||
(next_month, next_month)
|
||||
},
|
||||
ViewMode::Week => {
|
||||
// Go to next week
|
||||
let next_week = *selected_date + Duration::weeks(1);
|
||||
(next_week, next_week)
|
||||
},
|
||||
};
|
||||
selected_date.set(new_selected);
|
||||
current_date.set(new_display);
|
||||
let _ = LocalStorage::set("calendar_selected_date", new_selected.format("%Y-%m-%d").to_string());
|
||||
})
|
||||
};
|
||||
|
||||
let on_today = {
|
||||
let current_date = current_date.clone();
|
||||
let selected_date = selected_date.clone();
|
||||
let view = props.view.clone();
|
||||
Callback::from(move |_| {
|
||||
let today = Local::now().date_naive();
|
||||
let (new_selected, new_display) = match view {
|
||||
ViewMode::Month => {
|
||||
let first_of_today = today.with_day(1).unwrap();
|
||||
(today, first_of_today) // Select today, but display the month
|
||||
},
|
||||
ViewMode::Week => (today, today), // Select and display today
|
||||
};
|
||||
selected_date.set(new_selected);
|
||||
current_date.set(new_display);
|
||||
let _ = LocalStorage::set("calendar_selected_date", new_selected.format("%Y-%m-%d").to_string());
|
||||
})
|
||||
};
|
||||
|
||||
// Handle time increment toggle
|
||||
let on_time_increment_toggle = {
|
||||
let time_increment = time_increment.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
let current = *time_increment;
|
||||
let next = if current == 15 { 30 } else { 15 };
|
||||
time_increment.set(next);
|
||||
let _ = LocalStorage::set("calendar_time_increment", next);
|
||||
})
|
||||
};
|
||||
|
||||
// Handle drag-to-create event
|
||||
let on_create_event = {
|
||||
let show_create_modal = show_create_modal.clone();
|
||||
let create_event_data = create_event_data.clone();
|
||||
Callback::from(move |(_date, start_datetime, end_datetime): (NaiveDate, chrono::NaiveDateTime, chrono::NaiveDateTime)| {
|
||||
// For drag-to-create, we don't need the temporary event approach
|
||||
// Instead, just pass the local times directly via initial_time props
|
||||
create_event_data.set(Some((start_datetime.date(), start_datetime.time(), end_datetime.time())));
|
||||
show_create_modal.set(true);
|
||||
})
|
||||
};
|
||||
|
||||
// Handle drag-to-move event
|
||||
let on_event_update = {
|
||||
let on_event_update_request = props.on_event_update_request.clone();
|
||||
Callback::from(move |(event, new_start, new_end, preserve_rrule, until_date): (VEvent, chrono::NaiveDateTime, chrono::NaiveDateTime, bool, Option<chrono::DateTime<chrono::Utc>>)| {
|
||||
if let Some(callback) = &on_event_update_request {
|
||||
callback.emit((event, new_start, new_end, preserve_rrule, until_date));
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div class={classes!("calendar", match props.view { ViewMode::Week => Some("week-view"), _ => None })}>
|
||||
<CalendarHeader
|
||||
current_date={*current_date}
|
||||
view_mode={props.view.clone()}
|
||||
on_prev={on_prev}
|
||||
on_next={on_next}
|
||||
on_today={on_today}
|
||||
time_increment={Some(*time_increment)}
|
||||
on_time_increment_toggle={Some(on_time_increment_toggle)}
|
||||
/>
|
||||
|
||||
{
|
||||
match props.view {
|
||||
ViewMode::Month => {
|
||||
let on_day_select = {
|
||||
let selected_date = selected_date.clone();
|
||||
Callback::from(move |date: NaiveDate| {
|
||||
selected_date.set(date);
|
||||
let _ = LocalStorage::set("calendar_selected_date", date.format("%Y-%m-%d").to_string());
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<MonthView
|
||||
current_month={*current_date}
|
||||
today={today}
|
||||
events={props.events.clone()}
|
||||
on_event_click={props.on_event_click.clone()}
|
||||
refreshing_event_uid={props.refreshing_event_uid.clone()}
|
||||
user_info={props.user_info.clone()}
|
||||
on_event_context_menu={props.on_event_context_menu.clone()}
|
||||
on_calendar_context_menu={props.on_calendar_context_menu.clone()}
|
||||
selected_date={Some(*selected_date)}
|
||||
on_day_select={Some(on_day_select)}
|
||||
/>
|
||||
}
|
||||
},
|
||||
ViewMode::Week => html! {
|
||||
<WeekView
|
||||
current_date={*current_date}
|
||||
today={today}
|
||||
events={props.events.clone()}
|
||||
on_event_click={props.on_event_click.clone()}
|
||||
refreshing_event_uid={props.refreshing_event_uid.clone()}
|
||||
user_info={props.user_info.clone()}
|
||||
on_event_context_menu={props.on_event_context_menu.clone()}
|
||||
on_calendar_context_menu={props.on_calendar_context_menu.clone()}
|
||||
on_create_event={Some(on_create_event)}
|
||||
on_create_event_request={props.on_create_event_request.clone()}
|
||||
on_event_update={Some(on_event_update)}
|
||||
context_menus_open={props.context_menus_open}
|
||||
time_increment={*time_increment}
|
||||
/>
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Event details modal
|
||||
<EventModal
|
||||
event={(*selected_event).clone()}
|
||||
on_close={{
|
||||
let selected_event_clone = selected_event.clone();
|
||||
Callback::from(move |_| {
|
||||
selected_event_clone.set(None);
|
||||
})
|
||||
}}
|
||||
/>
|
||||
|
||||
// Create event modal
|
||||
<CreateEventModal
|
||||
is_open={*show_create_modal}
|
||||
selected_date={create_event_data.as_ref().map(|(date, _, _)| *date)}
|
||||
event_to_edit={None}
|
||||
available_calendars={props.user_info.as_ref().map(|info| info.calendars.clone()).unwrap_or_default()}
|
||||
initial_start_time={create_event_data.as_ref().map(|(_, start_time, _)| *start_time)}
|
||||
initial_end_time={create_event_data.as_ref().map(|(_, _, end_time)| *end_time)}
|
||||
on_close={{
|
||||
let show_create_modal = show_create_modal.clone();
|
||||
let create_event_data = create_event_data.clone();
|
||||
Callback::from(move |_| {
|
||||
show_create_modal.set(false);
|
||||
create_event_data.set(None);
|
||||
})
|
||||
}}
|
||||
on_create={{
|
||||
let show_create_modal = show_create_modal.clone();
|
||||
let create_event_data = create_event_data.clone();
|
||||
let on_create_event_request = props.on_create_event_request.clone();
|
||||
Callback::from(move |event_data: EventCreationData| {
|
||||
show_create_modal.set(false);
|
||||
create_event_data.set(None);
|
||||
|
||||
// Emit the create event request to parent
|
||||
if let Some(callback) = &on_create_event_request {
|
||||
callback.emit(event_data);
|
||||
}
|
||||
})
|
||||
}}
|
||||
on_update={{
|
||||
let show_create_modal = show_create_modal.clone();
|
||||
let create_event_data = create_event_data.clone();
|
||||
Callback::from(move |(_original_event, _updated_data): (VEvent, EventCreationData)| {
|
||||
show_create_modal.set(false);
|
||||
create_event_data.set(None);
|
||||
// TODO: Handle actual event update
|
||||
})
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
47
frontend/src/components/calendar_context_menu.rs
Normal file
47
frontend/src/components/calendar_context_menu.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use yew::prelude::*;
|
||||
use web_sys::MouseEvent;
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct CalendarContextMenuProps {
|
||||
pub is_open: bool,
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub on_close: Callback<()>,
|
||||
pub on_create_event: Callback<MouseEvent>,
|
||||
}
|
||||
|
||||
#[function_component(CalendarContextMenu)]
|
||||
pub fn calendar_context_menu(props: &CalendarContextMenuProps) -> Html {
|
||||
let menu_ref = use_node_ref();
|
||||
|
||||
if !props.is_open {
|
||||
return html! {};
|
||||
}
|
||||
|
||||
let style = format!(
|
||||
"position: fixed; left: {}px; top: {}px; z-index: 1001;",
|
||||
props.x, props.y
|
||||
);
|
||||
|
||||
let on_create_event_click = {
|
||||
let on_create_event = props.on_create_event.clone();
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |e: MouseEvent| {
|
||||
on_create_event.emit(e);
|
||||
on_close.emit(());
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div
|
||||
ref={menu_ref}
|
||||
class="context-menu"
|
||||
style={style}
|
||||
>
|
||||
<div class="context-menu-item context-menu-create" onclick={on_create_event_click}>
|
||||
<span class="context-menu-icon">{"+"}</span>
|
||||
{"Create Event"}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
64
frontend/src/components/calendar_header.rs
Normal file
64
frontend/src/components/calendar_header.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use yew::prelude::*;
|
||||
use chrono::{NaiveDate, Datelike};
|
||||
use crate::components::ViewMode;
|
||||
use web_sys::MouseEvent;
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct CalendarHeaderProps {
|
||||
pub current_date: NaiveDate,
|
||||
pub view_mode: ViewMode,
|
||||
pub on_prev: Callback<MouseEvent>,
|
||||
pub on_next: Callback<MouseEvent>,
|
||||
pub on_today: Callback<MouseEvent>,
|
||||
#[prop_or_default]
|
||||
pub time_increment: Option<u32>,
|
||||
#[prop_or_default]
|
||||
pub on_time_increment_toggle: Option<Callback<MouseEvent>>,
|
||||
}
|
||||
|
||||
#[function_component(CalendarHeader)]
|
||||
pub fn calendar_header(props: &CalendarHeaderProps) -> Html {
|
||||
let title = format!("{} {}", get_month_name(props.current_date.month()), props.current_date.year());
|
||||
|
||||
html! {
|
||||
<div class="calendar-header">
|
||||
<div class="header-left">
|
||||
<button class="nav-button" onclick={props.on_prev.clone()}>{"‹"}</button>
|
||||
{
|
||||
if let (Some(increment), Some(callback)) = (props.time_increment, &props.on_time_increment_toggle) {
|
||||
html! {
|
||||
<button class="time-increment-button" onclick={callback.clone()}>
|
||||
{format!("{}", increment)}
|
||||
</button>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
</div>
|
||||
<h2 class="month-year">{title}</h2>
|
||||
<div class="header-right">
|
||||
<button class="today-button" onclick={props.on_today.clone()}>{"Today"}</button>
|
||||
<button class="nav-button" onclick={props.on_next.clone()}>{"›"}</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
fn get_month_name(month: u32) -> &'static str {
|
||||
match month {
|
||||
1 => "January",
|
||||
2 => "February",
|
||||
3 => "March",
|
||||
4 => "April",
|
||||
5 => "May",
|
||||
6 => "June",
|
||||
7 => "July",
|
||||
8 => "August",
|
||||
9 => "September",
|
||||
10 => "October",
|
||||
11 => "November",
|
||||
12 => "December",
|
||||
_ => "Invalid"
|
||||
}
|
||||
}
|
||||
75
frontend/src/components/calendar_list_item.rs
Normal file
75
frontend/src/components/calendar_list_item.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use yew::prelude::*;
|
||||
use web_sys::MouseEvent;
|
||||
use crate::services::calendar_service::CalendarInfo;
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct CalendarListItemProps {
|
||||
pub calendar: CalendarInfo,
|
||||
pub color_picker_open: bool,
|
||||
pub on_color_change: Callback<(String, String)>, // (calendar_path, color)
|
||||
pub on_color_picker_toggle: Callback<String>, // calendar_path
|
||||
pub available_colors: Vec<String>,
|
||||
pub on_context_menu: Callback<(MouseEvent, String)>, // (event, calendar_path)
|
||||
}
|
||||
|
||||
#[function_component(CalendarListItem)]
|
||||
pub fn calendar_list_item(props: &CalendarListItemProps) -> Html {
|
||||
let on_color_click = {
|
||||
let cal_path = props.calendar.path.clone();
|
||||
let on_color_picker_toggle = props.on_color_picker_toggle.clone();
|
||||
Callback::from(move |e: MouseEvent| {
|
||||
e.stop_propagation();
|
||||
on_color_picker_toggle.emit(cal_path.clone());
|
||||
})
|
||||
};
|
||||
|
||||
let on_context_menu = {
|
||||
let cal_path = props.calendar.path.clone();
|
||||
let on_context_menu = props.on_context_menu.clone();
|
||||
Callback::from(move |e: MouseEvent| {
|
||||
e.prevent_default();
|
||||
on_context_menu.emit((e, cal_path.clone()));
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<li key={props.calendar.path.clone()} oncontextmenu={on_context_menu}>
|
||||
<span class="calendar-color"
|
||||
style={format!("background-color: {}", props.calendar.color)}
|
||||
onclick={on_color_click}>
|
||||
{
|
||||
if props.color_picker_open {
|
||||
html! {
|
||||
<div class="color-picker">
|
||||
{
|
||||
props.available_colors.iter().map(|color| {
|
||||
let color_str = color.clone();
|
||||
let cal_path = props.calendar.path.clone();
|
||||
let on_color_change = props.on_color_change.clone();
|
||||
|
||||
let on_color_select = Callback::from(move |_: MouseEvent| {
|
||||
on_color_change.emit((cal_path.clone(), color_str.clone()));
|
||||
});
|
||||
|
||||
let is_selected = props.calendar.color == *color;
|
||||
let class_name = if is_selected { "color-option selected" } else { "color-option" };
|
||||
|
||||
html! {
|
||||
<div class={class_name}
|
||||
style={format!("background-color: {}", color)}
|
||||
onclick={on_color_select}>
|
||||
</div>
|
||||
}
|
||||
}).collect::<Html>()
|
||||
}
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
</span>
|
||||
<span class="calendar-name">{&props.calendar.display_name}</span>
|
||||
</li>
|
||||
}
|
||||
}
|
||||
49
frontend/src/components/context_menu.rs
Normal file
49
frontend/src/components/context_menu.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use yew::prelude::*;
|
||||
use web_sys::MouseEvent;
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct ContextMenuProps {
|
||||
pub is_open: bool,
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub on_delete: Callback<MouseEvent>,
|
||||
pub on_close: Callback<()>,
|
||||
}
|
||||
|
||||
#[function_component(ContextMenu)]
|
||||
pub fn context_menu(props: &ContextMenuProps) -> Html {
|
||||
let menu_ref = use_node_ref();
|
||||
|
||||
// Close menu when clicking outside (handled by parent component)
|
||||
|
||||
if !props.is_open {
|
||||
return html! {};
|
||||
}
|
||||
|
||||
let style = format!(
|
||||
"position: fixed; left: {}px; top: {}px; z-index: 1001;",
|
||||
props.x, props.y
|
||||
);
|
||||
|
||||
let on_delete_click = {
|
||||
let on_delete = props.on_delete.clone();
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |e: MouseEvent| {
|
||||
on_delete.emit(e);
|
||||
on_close.emit(());
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div
|
||||
ref={menu_ref}
|
||||
class="context-menu"
|
||||
style={style}
|
||||
>
|
||||
<div class="context-menu-item context-menu-delete" onclick={on_delete_click}>
|
||||
<span class="context-menu-icon">{"🗑️"}</span>
|
||||
{"Delete Calendar"}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
196
frontend/src/components/create_calendar_modal.rs
Normal file
196
frontend/src/components/create_calendar_modal.rs
Normal file
@@ -0,0 +1,196 @@
|
||||
use yew::prelude::*;
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct CreateCalendarModalProps {
|
||||
pub is_open: bool,
|
||||
pub on_close: Callback<()>,
|
||||
pub on_create: Callback<(String, Option<String>, Option<String>)>, // name, description, color
|
||||
pub available_colors: Vec<String>,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn CreateCalendarModal(props: &CreateCalendarModalProps) -> Html {
|
||||
let calendar_name = use_state(|| String::new());
|
||||
let description = use_state(|| String::new());
|
||||
let selected_color = use_state(|| None::<String>);
|
||||
let error_message = use_state(|| None::<String>);
|
||||
let is_creating = use_state(|| false);
|
||||
|
||||
let on_name_change = {
|
||||
let calendar_name = calendar_name.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
let input: web_sys::HtmlInputElement = e.target_unchecked_into();
|
||||
calendar_name.set(input.value());
|
||||
})
|
||||
};
|
||||
|
||||
let on_description_change = {
|
||||
let description = description.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
let input: web_sys::HtmlTextAreaElement = e.target_unchecked_into();
|
||||
description.set(input.value());
|
||||
})
|
||||
};
|
||||
|
||||
let on_submit = {
|
||||
let calendar_name = calendar_name.clone();
|
||||
let description = description.clone();
|
||||
let selected_color = selected_color.clone();
|
||||
let error_message = error_message.clone();
|
||||
let is_creating = is_creating.clone();
|
||||
let on_create = props.on_create.clone();
|
||||
|
||||
Callback::from(move |e: SubmitEvent| {
|
||||
e.prevent_default();
|
||||
|
||||
let name = (*calendar_name).trim();
|
||||
if name.is_empty() {
|
||||
error_message.set(Some("Calendar name is required".to_string()));
|
||||
return;
|
||||
}
|
||||
|
||||
if name.len() > 100 {
|
||||
error_message.set(Some("Calendar name too long (max 100 characters)".to_string()));
|
||||
return;
|
||||
}
|
||||
|
||||
error_message.set(None);
|
||||
is_creating.set(true);
|
||||
|
||||
let desc = if (*description).trim().is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some((*description).clone())
|
||||
};
|
||||
|
||||
on_create.emit((name.to_string(), desc, (*selected_color).clone()));
|
||||
})
|
||||
};
|
||||
|
||||
let on_backdrop_click = {
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |e: MouseEvent| {
|
||||
// Only close if clicking the backdrop, not the modal content
|
||||
if e.target() == e.current_target() {
|
||||
on_close.emit(());
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
if !props.is_open {
|
||||
return html! {};
|
||||
}
|
||||
|
||||
html! {
|
||||
<div class="modal-backdrop" onclick={on_backdrop_click}>
|
||||
<div class="create-calendar-modal">
|
||||
<div class="modal-header">
|
||||
<h2>{"Create New Calendar"}</h2>
|
||||
<button class="close-button" onclick={props.on_close.reform(|_| ())}>
|
||||
{"×"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<form class="modal-body" onsubmit={on_submit}>
|
||||
{
|
||||
if let Some(ref error) = *error_message {
|
||||
html! {
|
||||
<div class="error-message">
|
||||
{error}
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
<div class="form-group">
|
||||
<label for="calendar-name">{"Calendar Name *"}</label>
|
||||
<input
|
||||
id="calendar-name"
|
||||
type="text"
|
||||
value={(*calendar_name).clone()}
|
||||
oninput={on_name_change}
|
||||
placeholder="Enter calendar name"
|
||||
maxlength="100"
|
||||
disabled={*is_creating}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="calendar-description">{"Description"}</label>
|
||||
<textarea
|
||||
id="calendar-description"
|
||||
value={(*description).clone()}
|
||||
oninput={on_description_change}
|
||||
placeholder="Optional calendar description"
|
||||
rows="3"
|
||||
disabled={*is_creating}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>{"Calendar Color"}</label>
|
||||
<div class="color-grid">
|
||||
{
|
||||
props.available_colors.iter().enumerate().map(|(index, color)| {
|
||||
let color = color.clone();
|
||||
let selected_color = selected_color.clone();
|
||||
let is_selected = selected_color.as_ref() == Some(&color);
|
||||
let on_color_select = {
|
||||
let color = color.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
selected_color.set(Some(color.clone()));
|
||||
})
|
||||
};
|
||||
|
||||
let class_name = if is_selected {
|
||||
"color-option selected"
|
||||
} else {
|
||||
"color-option"
|
||||
};
|
||||
|
||||
html! {
|
||||
<button
|
||||
key={index}
|
||||
type="button"
|
||||
class={class_name}
|
||||
style={format!("background-color: {}", color)}
|
||||
onclick={on_color_select}
|
||||
disabled={*is_creating}
|
||||
/>
|
||||
}
|
||||
}).collect::<Html>()
|
||||
}
|
||||
</div>
|
||||
<p class="color-help-text">{"Optional: Choose a color for your calendar"}</p>
|
||||
</div>
|
||||
|
||||
<div class="modal-actions">
|
||||
<button
|
||||
type="button"
|
||||
class="cancel-button"
|
||||
onclick={props.on_close.reform(|_| ())}
|
||||
disabled={*is_creating}
|
||||
>
|
||||
{"Cancel"}
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
class="create-button"
|
||||
disabled={*is_creating}
|
||||
>
|
||||
{
|
||||
if *is_creating {
|
||||
"Creating..."
|
||||
} else {
|
||||
"Create Calendar"
|
||||
}
|
||||
}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
857
frontend/src/components/create_event_modal.rs
Normal file
857
frontend/src/components/create_event_modal.rs
Normal file
@@ -0,0 +1,857 @@
|
||||
use yew::prelude::*;
|
||||
use web_sys::{HtmlInputElement, HtmlTextAreaElement, HtmlSelectElement};
|
||||
use chrono::{NaiveDate, NaiveTime, Local, TimeZone, Utc};
|
||||
use crate::services::calendar_service::CalendarInfo;
|
||||
use crate::models::ical::VEvent;
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct CreateEventModalProps {
|
||||
pub is_open: bool,
|
||||
pub selected_date: Option<NaiveDate>,
|
||||
pub event_to_edit: Option<VEvent>,
|
||||
pub on_close: Callback<()>,
|
||||
pub on_create: Callback<EventCreationData>,
|
||||
pub on_update: Callback<(VEvent, EventCreationData)>, // (original_event, updated_data)
|
||||
pub available_calendars: Vec<CalendarInfo>,
|
||||
#[prop_or_default]
|
||||
pub initial_start_time: Option<NaiveTime>,
|
||||
#[prop_or_default]
|
||||
pub initial_end_time: Option<NaiveTime>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum EventStatus {
|
||||
Tentative,
|
||||
Confirmed,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
impl Default for EventStatus {
|
||||
fn default() -> Self {
|
||||
EventStatus::Confirmed
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum EventClass {
|
||||
Public,
|
||||
Private,
|
||||
Confidential,
|
||||
}
|
||||
|
||||
impl Default for EventClass {
|
||||
fn default() -> Self {
|
||||
EventClass::Public
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum ReminderType {
|
||||
None,
|
||||
Minutes15,
|
||||
Minutes30,
|
||||
Hour1,
|
||||
Hours2,
|
||||
Day1,
|
||||
Days2,
|
||||
Week1,
|
||||
}
|
||||
|
||||
impl Default for ReminderType {
|
||||
fn default() -> Self {
|
||||
ReminderType::None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum RecurrenceType {
|
||||
None,
|
||||
Daily,
|
||||
Weekly,
|
||||
Monthly,
|
||||
Yearly,
|
||||
}
|
||||
|
||||
impl Default for RecurrenceType {
|
||||
fn default() -> Self {
|
||||
RecurrenceType::None
|
||||
}
|
||||
}
|
||||
|
||||
impl RecurrenceType {
|
||||
pub fn from_rrule(rrule: Option<&str>) -> Self {
|
||||
match rrule {
|
||||
Some(rule) if rule.contains("FREQ=DAILY") => RecurrenceType::Daily,
|
||||
Some(rule) if rule.contains("FREQ=WEEKLY") => RecurrenceType::Weekly,
|
||||
Some(rule) if rule.contains("FREQ=MONTHLY") => RecurrenceType::Monthly,
|
||||
Some(rule) if rule.contains("FREQ=YEARLY") => RecurrenceType::Yearly,
|
||||
_ => RecurrenceType::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct EventCreationData {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub start_date: NaiveDate,
|
||||
pub start_time: NaiveTime,
|
||||
pub end_date: NaiveDate,
|
||||
pub end_time: NaiveTime,
|
||||
pub location: String,
|
||||
pub all_day: bool,
|
||||
pub status: EventStatus,
|
||||
pub class: EventClass,
|
||||
pub priority: Option<u8>,
|
||||
pub organizer: String,
|
||||
pub attendees: String, // Comma-separated list
|
||||
pub categories: String, // Comma-separated list
|
||||
pub reminder: ReminderType,
|
||||
pub recurrence: RecurrenceType,
|
||||
pub recurrence_days: Vec<bool>, // [Sun, Mon, Tue, Wed, Thu, Fri, Sat] for weekly recurrence
|
||||
pub selected_calendar: Option<String>, // Calendar path
|
||||
}
|
||||
|
||||
impl Default for EventCreationData {
|
||||
fn default() -> Self {
|
||||
let now = chrono::Local::now().naive_local();
|
||||
let start_time = NaiveTime::from_hms_opt(9, 0, 0).unwrap_or_default();
|
||||
let end_time = NaiveTime::from_hms_opt(10, 0, 0).unwrap_or_default();
|
||||
|
||||
Self {
|
||||
title: String::new(),
|
||||
description: String::new(),
|
||||
start_date: now.date(),
|
||||
start_time,
|
||||
end_date: now.date(),
|
||||
end_time,
|
||||
location: String::new(),
|
||||
all_day: false,
|
||||
status: EventStatus::default(),
|
||||
class: EventClass::default(),
|
||||
priority: None,
|
||||
organizer: String::new(),
|
||||
attendees: String::new(),
|
||||
categories: String::new(),
|
||||
reminder: ReminderType::default(),
|
||||
recurrence: RecurrenceType::default(),
|
||||
recurrence_days: vec![false; 7], // [Sun, Mon, Tue, Wed, Thu, Fri, Sat] - all false by default
|
||||
selected_calendar: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EventCreationData {
|
||||
pub fn to_create_event_params(&self) -> (String, String, String, String, String, String, String, bool, String, String, Option<u8>, String, String, String, String, String, Vec<bool>, Option<String>) {
|
||||
// Convert local date/time to UTC
|
||||
let start_local = Local.from_local_datetime(&self.start_date.and_time(self.start_time)).single()
|
||||
.unwrap_or_else(|| Local::now());
|
||||
let end_local = Local.from_local_datetime(&self.end_date.and_time(self.end_time)).single()
|
||||
.unwrap_or_else(|| Local::now());
|
||||
|
||||
let start_utc = start_local.with_timezone(&Utc);
|
||||
let end_utc = end_local.with_timezone(&Utc);
|
||||
|
||||
(
|
||||
self.title.clone(),
|
||||
self.description.clone(),
|
||||
start_utc.format("%Y-%m-%d").to_string(),
|
||||
start_utc.format("%H:%M").to_string(),
|
||||
end_utc.format("%Y-%m-%d").to_string(),
|
||||
end_utc.format("%H:%M").to_string(),
|
||||
self.location.clone(),
|
||||
self.all_day,
|
||||
match self.status {
|
||||
EventStatus::Tentative => "TENTATIVE".to_string(),
|
||||
EventStatus::Confirmed => "CONFIRMED".to_string(),
|
||||
EventStatus::Cancelled => "CANCELLED".to_string(),
|
||||
},
|
||||
match self.class {
|
||||
EventClass::Public => "PUBLIC".to_string(),
|
||||
EventClass::Private => "PRIVATE".to_string(),
|
||||
EventClass::Confidential => "CONFIDENTIAL".to_string(),
|
||||
},
|
||||
self.priority,
|
||||
self.organizer.clone(),
|
||||
self.attendees.clone(),
|
||||
self.categories.clone(),
|
||||
match self.reminder {
|
||||
ReminderType::None => "".to_string(),
|
||||
ReminderType::Minutes15 => "15".to_string(),
|
||||
ReminderType::Minutes30 => "30".to_string(),
|
||||
ReminderType::Hour1 => "60".to_string(),
|
||||
ReminderType::Hours2 => "120".to_string(),
|
||||
ReminderType::Day1 => "1440".to_string(),
|
||||
ReminderType::Days2 => "2880".to_string(),
|
||||
ReminderType::Week1 => "10080".to_string(),
|
||||
},
|
||||
match self.recurrence {
|
||||
RecurrenceType::None => "".to_string(),
|
||||
RecurrenceType::Daily => "DAILY".to_string(),
|
||||
RecurrenceType::Weekly => "WEEKLY".to_string(),
|
||||
RecurrenceType::Monthly => "MONTHLY".to_string(),
|
||||
RecurrenceType::Yearly => "YEARLY".to_string(),
|
||||
},
|
||||
self.recurrence_days.clone(),
|
||||
self.selected_calendar.clone()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventCreationData {
|
||||
pub fn from_calendar_event(event: &VEvent) -> Self {
|
||||
// Convert VEvent to EventCreationData for editing
|
||||
// All events (including temporary drag events) now have proper UTC times
|
||||
// Convert to local time for display in the modal
|
||||
|
||||
Self {
|
||||
title: event.summary.clone().unwrap_or_default(),
|
||||
description: event.description.clone().unwrap_or_default(),
|
||||
start_date: event.dtstart.with_timezone(&chrono::Local).date_naive(),
|
||||
start_time: event.dtstart.with_timezone(&chrono::Local).time(),
|
||||
end_date: event.dtend.as_ref().map(|e| e.with_timezone(&chrono::Local).date_naive()).unwrap_or(event.dtstart.with_timezone(&chrono::Local).date_naive()),
|
||||
end_time: event.dtend.as_ref().map(|e| e.with_timezone(&chrono::Local).time()).unwrap_or(event.dtstart.with_timezone(&chrono::Local).time()),
|
||||
location: event.location.clone().unwrap_or_default(),
|
||||
all_day: event.all_day,
|
||||
status: event.status.as_ref().map(|s| match s {
|
||||
crate::models::ical::EventStatus::Tentative => EventStatus::Tentative,
|
||||
crate::models::ical::EventStatus::Confirmed => EventStatus::Confirmed,
|
||||
crate::models::ical::EventStatus::Cancelled => EventStatus::Cancelled,
|
||||
}).unwrap_or(EventStatus::Confirmed),
|
||||
class: event.class.as_ref().map(|c| match c {
|
||||
crate::models::ical::EventClass::Public => EventClass::Public,
|
||||
crate::models::ical::EventClass::Private => EventClass::Private,
|
||||
crate::models::ical::EventClass::Confidential => EventClass::Confidential,
|
||||
}).unwrap_or(EventClass::Public),
|
||||
priority: event.priority,
|
||||
organizer: event.organizer.as_ref().map(|o| o.cal_address.clone()).unwrap_or_default(),
|
||||
attendees: event.attendees.iter().map(|a| a.cal_address.clone()).collect::<Vec<_>>().join(", "),
|
||||
categories: event.categories.join(", "),
|
||||
reminder: ReminderType::default(), // TODO: Convert from event reminders
|
||||
recurrence: RecurrenceType::from_rrule(event.rrule.as_deref()),
|
||||
recurrence_days: vec![false; 7], // TODO: Parse from RRULE
|
||||
selected_calendar: event.calendar_path.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[function_component(CreateEventModal)]
|
||||
pub fn create_event_modal(props: &CreateEventModalProps) -> Html {
|
||||
let event_data = use_state(|| EventCreationData::default());
|
||||
|
||||
// Initialize with selected date or event data if provided
|
||||
use_effect_with((props.selected_date, props.event_to_edit.clone(), props.is_open, props.available_calendars.clone(), props.initial_start_time, props.initial_end_time), {
|
||||
let event_data = event_data.clone();
|
||||
move |(selected_date, event_to_edit, is_open, available_calendars, initial_start_time, initial_end_time)| {
|
||||
if *is_open {
|
||||
let mut data = if let Some(event) = event_to_edit {
|
||||
// Pre-populate with event data for editing
|
||||
EventCreationData::from_calendar_event(event)
|
||||
} else if let Some(date) = selected_date {
|
||||
// Initialize with selected date for new event
|
||||
let mut data = EventCreationData::default();
|
||||
data.start_date = *date;
|
||||
data.end_date = *date;
|
||||
|
||||
// Use initial times if provided (from drag-to-create)
|
||||
if let Some(start_time) = initial_start_time {
|
||||
data.start_time = *start_time;
|
||||
}
|
||||
if let Some(end_time) = initial_end_time {
|
||||
data.end_time = *end_time;
|
||||
}
|
||||
|
||||
data
|
||||
} else {
|
||||
// Default initialization
|
||||
EventCreationData::default()
|
||||
};
|
||||
|
||||
// Set default calendar to the first available one if none selected
|
||||
if data.selected_calendar.is_none() && !available_calendars.is_empty() {
|
||||
data.selected_calendar = Some(available_calendars[0].path.clone());
|
||||
}
|
||||
|
||||
event_data.set(data);
|
||||
}
|
||||
|| ()
|
||||
}
|
||||
});
|
||||
|
||||
if !props.is_open {
|
||||
return html! {};
|
||||
}
|
||||
|
||||
let on_backdrop_click = {
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |e: MouseEvent| {
|
||||
if e.target() == e.current_target() {
|
||||
on_close.emit(());
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_title_input = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.title = input.value();
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_calendar_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(select) = e.target_dyn_into::<HtmlSelectElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
let value = select.value();
|
||||
data.selected_calendar = if value.is_empty() { None } else { Some(value) };
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_description_input = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
if let Some(textarea) = e.target_dyn_into::<HtmlTextAreaElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.description = textarea.value();
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_location_input = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.location = input.value();
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_organizer_input = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.organizer = input.value();
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_attendees_input = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
if let Some(textarea) = e.target_dyn_into::<HtmlTextAreaElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.attendees = textarea.value();
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_categories_input = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.categories = input.value();
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_status_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(select) = e.target_dyn_into::<HtmlSelectElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.status = match select.value().as_str() {
|
||||
"tentative" => EventStatus::Tentative,
|
||||
"cancelled" => EventStatus::Cancelled,
|
||||
_ => EventStatus::Confirmed,
|
||||
};
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_class_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(select) = e.target_dyn_into::<HtmlSelectElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.class = match select.value().as_str() {
|
||||
"private" => EventClass::Private,
|
||||
"confidential" => EventClass::Confidential,
|
||||
_ => EventClass::Public,
|
||||
};
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_priority_input = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: InputEvent| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.priority = input.value().parse::<u8>().ok().filter(|&p| p <= 9);
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_reminder_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(select) = e.target_dyn_into::<HtmlSelectElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.reminder = match select.value().as_str() {
|
||||
"15min" => ReminderType::Minutes15,
|
||||
"30min" => ReminderType::Minutes30,
|
||||
"1hour" => ReminderType::Hour1,
|
||||
"2hours" => ReminderType::Hours2,
|
||||
"1day" => ReminderType::Day1,
|
||||
"2days" => ReminderType::Days2,
|
||||
"1week" => ReminderType::Week1,
|
||||
_ => ReminderType::None,
|
||||
};
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_recurrence_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(select) = e.target_dyn_into::<HtmlSelectElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.recurrence = match select.value().as_str() {
|
||||
"daily" => RecurrenceType::Daily,
|
||||
"weekly" => RecurrenceType::Weekly,
|
||||
"monthly" => RecurrenceType::Monthly,
|
||||
"yearly" => RecurrenceType::Yearly,
|
||||
_ => RecurrenceType::None,
|
||||
};
|
||||
// Reset recurrence days when changing recurrence type
|
||||
data.recurrence_days = vec![false; 7];
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_weekday_change = {
|
||||
let event_data = event_data.clone();
|
||||
move |day_index: usize| {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
if day_index < data.recurrence_days.len() {
|
||||
data.recurrence_days[day_index] = input.checked();
|
||||
event_data.set(data);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
let on_start_date_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
if let Ok(date) = NaiveDate::parse_from_str(&input.value(), "%Y-%m-%d") {
|
||||
let mut data = (*event_data).clone();
|
||||
data.start_date = date;
|
||||
event_data.set(data);
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_start_time_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
if let Ok(time) = NaiveTime::parse_from_str(&input.value(), "%H:%M") {
|
||||
let mut data = (*event_data).clone();
|
||||
data.start_time = time;
|
||||
event_data.set(data);
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_end_date_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
if let Ok(date) = NaiveDate::parse_from_str(&input.value(), "%Y-%m-%d") {
|
||||
let mut data = (*event_data).clone();
|
||||
data.end_date = date;
|
||||
event_data.set(data);
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_end_time_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
if let Ok(time) = NaiveTime::parse_from_str(&input.value(), "%H:%M") {
|
||||
let mut data = (*event_data).clone();
|
||||
data.end_time = time;
|
||||
event_data.set(data);
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_all_day_change = {
|
||||
let event_data = event_data.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
if let Some(input) = e.target_dyn_into::<HtmlInputElement>() {
|
||||
let mut data = (*event_data).clone();
|
||||
data.all_day = input.checked();
|
||||
event_data.set(data);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_submit_click = {
|
||||
let event_data = event_data.clone();
|
||||
let on_create = props.on_create.clone();
|
||||
let on_update = props.on_update.clone();
|
||||
let event_to_edit = props.event_to_edit.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
if let Some(original_event) = &event_to_edit {
|
||||
// We're editing - call on_update with original event and new data
|
||||
on_update.emit((original_event.clone(), (*event_data).clone()));
|
||||
} else {
|
||||
// We're creating - call on_create with new data
|
||||
on_create.emit((*event_data).clone());
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_cancel_click = {
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
on_close.emit(());
|
||||
})
|
||||
};
|
||||
|
||||
let data = &*event_data;
|
||||
|
||||
html! {
|
||||
<div class="modal-backdrop" onclick={on_backdrop_click}>
|
||||
<div class="modal-content create-event-modal" onclick={Callback::from(|e: MouseEvent| e.stop_propagation())}>
|
||||
<div class="modal-header">
|
||||
<h3>{if props.event_to_edit.is_some() { "Edit Event" } else { "Create New Event" }}</h3>
|
||||
<button type="button" class="modal-close" onclick={Callback::from({
|
||||
let on_close = props.on_close.clone();
|
||||
move |_: MouseEvent| on_close.emit(())
|
||||
})}>{"×"}</button>
|
||||
</div>
|
||||
|
||||
<div class="modal-body">
|
||||
<div class="form-group">
|
||||
<label for="event-title">{"Title *"}</label>
|
||||
<input
|
||||
type="text"
|
||||
id="event-title"
|
||||
class="form-input"
|
||||
value={data.title.clone()}
|
||||
oninput={on_title_input}
|
||||
placeholder="Enter event title"
|
||||
required=true
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-calendar">{"Calendar"}</label>
|
||||
<select
|
||||
id="event-calendar"
|
||||
class="form-input"
|
||||
onchange={on_calendar_change}
|
||||
>
|
||||
<option value="" selected={data.selected_calendar.is_none()}>{"Select calendar..."}</option>
|
||||
{
|
||||
props.available_calendars.iter().map(|calendar| {
|
||||
let is_selected = data.selected_calendar.as_ref() == Some(&calendar.path);
|
||||
html! {
|
||||
<option
|
||||
key={calendar.path.clone()}
|
||||
value={calendar.path.clone()}
|
||||
selected={is_selected}
|
||||
>
|
||||
{&calendar.display_name}
|
||||
</option>
|
||||
}
|
||||
}).collect::<Html>()
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-description">{"Description"}</label>
|
||||
<textarea
|
||||
id="event-description"
|
||||
class="form-input"
|
||||
value={data.description.clone()}
|
||||
oninput={on_description_input}
|
||||
placeholder="Enter event description"
|
||||
rows="3"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={data.all_day}
|
||||
onchange={on_all_day_change}
|
||||
/>
|
||||
{" All Day"}
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="start-date">{"Start Date *"}</label>
|
||||
<input
|
||||
type="date"
|
||||
id="start-date"
|
||||
class="form-input"
|
||||
value={data.start_date.format("%Y-%m-%d").to_string()}
|
||||
onchange={on_start_date_change}
|
||||
required=true
|
||||
/>
|
||||
</div>
|
||||
|
||||
if !data.all_day {
|
||||
<div class="form-group">
|
||||
<label for="start-time">{"Start Time"}</label>
|
||||
<input
|
||||
type="time"
|
||||
id="start-time"
|
||||
class="form-input"
|
||||
value={data.start_time.format("%H:%M").to_string()}
|
||||
onchange={on_start_time_change}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="end-date">{"End Date *"}</label>
|
||||
<input
|
||||
type="date"
|
||||
id="end-date"
|
||||
class="form-input"
|
||||
value={data.end_date.format("%Y-%m-%d").to_string()}
|
||||
onchange={on_end_date_change}
|
||||
required=true
|
||||
/>
|
||||
</div>
|
||||
|
||||
if !data.all_day {
|
||||
<div class="form-group">
|
||||
<label for="end-time">{"End Time"}</label>
|
||||
<input
|
||||
type="time"
|
||||
id="end-time"
|
||||
class="form-input"
|
||||
value={data.end_time.format("%H:%M").to_string()}
|
||||
onchange={on_end_time_change}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-location">{"Location"}</label>
|
||||
<input
|
||||
type="text"
|
||||
id="event-location"
|
||||
class="form-input"
|
||||
value={data.location.clone()}
|
||||
oninput={on_location_input}
|
||||
placeholder="Enter event location"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="event-status">{"Status"}</label>
|
||||
<select
|
||||
id="event-status"
|
||||
class="form-input"
|
||||
onchange={on_status_change}
|
||||
>
|
||||
<option value="confirmed" selected={matches!(data.status, EventStatus::Confirmed)}>{"Confirmed"}</option>
|
||||
<option value="tentative" selected={matches!(data.status, EventStatus::Tentative)}>{"Tentative"}</option>
|
||||
<option value="cancelled" selected={matches!(data.status, EventStatus::Cancelled)}>{"Cancelled"}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-class">{"Privacy"}</label>
|
||||
<select
|
||||
id="event-class"
|
||||
class="form-input"
|
||||
onchange={on_class_change}
|
||||
>
|
||||
<option value="public" selected={matches!(data.class, EventClass::Public)}>{"Public"}</option>
|
||||
<option value="private" selected={matches!(data.class, EventClass::Private)}>{"Private"}</option>
|
||||
<option value="confidential" selected={matches!(data.class, EventClass::Confidential)}>{"Confidential"}</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-priority">{"Priority (0-9, optional)"}</label>
|
||||
<input
|
||||
type="number"
|
||||
id="event-priority"
|
||||
class="form-input"
|
||||
value={data.priority.map(|p| p.to_string()).unwrap_or_default()}
|
||||
oninput={on_priority_input}
|
||||
placeholder="0-9 priority level"
|
||||
min="0"
|
||||
max="9"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-organizer">{"Organizer Email"}</label>
|
||||
<input
|
||||
type="email"
|
||||
id="event-organizer"
|
||||
class="form-input"
|
||||
value={data.organizer.clone()}
|
||||
oninput={on_organizer_input}
|
||||
placeholder="organizer@example.com"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-attendees">{"Attendees (comma-separated emails)"}</label>
|
||||
<textarea
|
||||
id="event-attendees"
|
||||
class="form-input"
|
||||
value={data.attendees.clone()}
|
||||
oninput={on_attendees_input}
|
||||
placeholder="attendee1@example.com, attendee2@example.com"
|
||||
rows="2"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-categories">{"Categories (comma-separated)"}</label>
|
||||
<input
|
||||
type="text"
|
||||
id="event-categories"
|
||||
class="form-input"
|
||||
value={data.categories.clone()}
|
||||
oninput={on_categories_input}
|
||||
placeholder="work, meeting, personal"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="event-reminder">{"Reminder"}</label>
|
||||
<select
|
||||
id="event-reminder"
|
||||
class="form-input"
|
||||
onchange={on_reminder_change}
|
||||
>
|
||||
<option value="none" selected={matches!(data.reminder, ReminderType::None)}>{"None"}</option>
|
||||
<option value="15min" selected={matches!(data.reminder, ReminderType::Minutes15)}>{"15 minutes"}</option>
|
||||
<option value="30min" selected={matches!(data.reminder, ReminderType::Minutes30)}>{"30 minutes"}</option>
|
||||
<option value="1hour" selected={matches!(data.reminder, ReminderType::Hour1)}>{"1 hour"}</option>
|
||||
<option value="2hours" selected={matches!(data.reminder, ReminderType::Hours2)}>{"2 hours"}</option>
|
||||
<option value="1day" selected={matches!(data.reminder, ReminderType::Day1)}>{"1 day"}</option>
|
||||
<option value="2days" selected={matches!(data.reminder, ReminderType::Days2)}>{"2 days"}</option>
|
||||
<option value="1week" selected={matches!(data.reminder, ReminderType::Week1)}>{"1 week"}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="event-recurrence">{"Recurrence"}</label>
|
||||
<select
|
||||
id="event-recurrence"
|
||||
class="form-input"
|
||||
onchange={on_recurrence_change}
|
||||
>
|
||||
<option value="none" selected={matches!(data.recurrence, RecurrenceType::None)}>{"None"}</option>
|
||||
<option value="daily" selected={matches!(data.recurrence, RecurrenceType::Daily)}>{"Daily"}</option>
|
||||
<option value="weekly" selected={matches!(data.recurrence, RecurrenceType::Weekly)}>{"Weekly"}</option>
|
||||
<option value="monthly" selected={matches!(data.recurrence, RecurrenceType::Monthly)}>{"Monthly"}</option>
|
||||
<option value="yearly" selected={matches!(data.recurrence, RecurrenceType::Yearly)}>{"Yearly"}</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
// Show weekday selection only when weekly recurrence is selected
|
||||
if matches!(data.recurrence, RecurrenceType::Weekly) {
|
||||
<div class="form-group">
|
||||
<label>{"Repeat on"}</label>
|
||||
<div class="weekday-selection">
|
||||
{
|
||||
["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, day)| {
|
||||
let day_checked = data.recurrence_days.get(i).cloned().unwrap_or(false);
|
||||
let on_change = on_weekday_change(i);
|
||||
html! {
|
||||
<label key={i} class="weekday-checkbox">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={day_checked}
|
||||
onchange={on_change}
|
||||
/>
|
||||
<span class="weekday-label">{day}</span>
|
||||
</label>
|
||||
}
|
||||
})
|
||||
.collect::<Html>()
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" onclick={on_cancel_click}>
|
||||
{"Cancel"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
onclick={on_submit_click}
|
||||
disabled={data.title.trim().is_empty()}
|
||||
>
|
||||
{if props.event_to_edit.is_some() { "Update Event" } else { "Create Event" }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
98
frontend/src/components/event_context_menu.rs
Normal file
98
frontend/src/components/event_context_menu.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
use yew::prelude::*;
|
||||
use web_sys::MouseEvent;
|
||||
use crate::models::ical::VEvent;
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub enum DeleteAction {
|
||||
DeleteThis,
|
||||
DeleteFollowing,
|
||||
DeleteSeries,
|
||||
}
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct EventContextMenuProps {
|
||||
pub is_open: bool,
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub event: Option<VEvent>,
|
||||
pub on_edit: Callback<()>,
|
||||
pub on_delete: Callback<DeleteAction>,
|
||||
pub on_close: Callback<()>,
|
||||
}
|
||||
|
||||
#[function_component(EventContextMenu)]
|
||||
pub fn event_context_menu(props: &EventContextMenuProps) -> Html {
|
||||
let menu_ref = use_node_ref();
|
||||
|
||||
if !props.is_open {
|
||||
return html! {};
|
||||
}
|
||||
|
||||
let style = format!(
|
||||
"position: fixed; left: {}px; top: {}px; z-index: 1001;",
|
||||
props.x, props.y
|
||||
);
|
||||
|
||||
// Check if the event is recurring
|
||||
let is_recurring = props.event.as_ref()
|
||||
.map(|event| event.rrule.is_some())
|
||||
.unwrap_or(false);
|
||||
|
||||
let on_edit_click = {
|
||||
let on_edit = props.on_edit.clone();
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
on_edit.emit(());
|
||||
on_close.emit(());
|
||||
})
|
||||
};
|
||||
|
||||
let create_delete_callback = |action: DeleteAction| {
|
||||
let on_delete = props.on_delete.clone();
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |_: MouseEvent| {
|
||||
on_delete.emit(action.clone());
|
||||
on_close.emit(());
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div
|
||||
ref={menu_ref}
|
||||
class="context-menu"
|
||||
style={style}
|
||||
>
|
||||
<div class="context-menu-item" onclick={on_edit_click}>
|
||||
<span class="context-menu-icon">{"✏️"}</span>
|
||||
{"Edit Event"}
|
||||
</div>
|
||||
{
|
||||
if is_recurring {
|
||||
html! {
|
||||
<>
|
||||
<div class="context-menu-item context-menu-delete" onclick={create_delete_callback(DeleteAction::DeleteThis)}>
|
||||
<span class="context-menu-icon">{"🗑️"}</span>
|
||||
{"Delete This Event"}
|
||||
</div>
|
||||
<div class="context-menu-item context-menu-delete" onclick={create_delete_callback(DeleteAction::DeleteFollowing)}>
|
||||
<span class="context-menu-icon">{"🗑️"}</span>
|
||||
{"Delete Following Events"}
|
||||
</div>
|
||||
<div class="context-menu-item context-menu-delete" onclick={create_delete_callback(DeleteAction::DeleteSeries)}>
|
||||
<span class="context-menu-icon">{"🗑️"}</span>
|
||||
{"Delete Entire Series"}
|
||||
</div>
|
||||
</>
|
||||
}
|
||||
} else {
|
||||
html! {
|
||||
<div class="context-menu-item context-menu-delete" onclick={create_delete_callback(DeleteAction::DeleteThis)}>
|
||||
<span class="context-menu-icon">{"🗑️"}</span>
|
||||
{"Delete Event"}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
239
frontend/src/components/event_modal.rs
Normal file
239
frontend/src/components/event_modal.rs
Normal file
@@ -0,0 +1,239 @@
|
||||
use yew::prelude::*;
|
||||
use chrono::{DateTime, Utc};
|
||||
use crate::models::ical::VEvent;
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct EventModalProps {
|
||||
pub event: Option<VEvent>,
|
||||
pub on_close: Callback<()>,
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn EventModal(props: &EventModalProps) -> Html {
|
||||
let close_modal = {
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |_| {
|
||||
on_close.emit(());
|
||||
})
|
||||
};
|
||||
|
||||
let backdrop_click = {
|
||||
let on_close = props.on_close.clone();
|
||||
Callback::from(move |e: MouseEvent| {
|
||||
if e.target() == e.current_target() {
|
||||
on_close.emit(());
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
if let Some(ref event) = props.event {
|
||||
html! {
|
||||
<div class="modal-backdrop" onclick={backdrop_click}>
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h3>{"Event Details"}</h3>
|
||||
<button class="modal-close" onclick={close_modal}>{"×"}</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="event-detail">
|
||||
<strong>{"Title:"}</strong>
|
||||
<span>{event.get_title()}</span>
|
||||
</div>
|
||||
|
||||
{
|
||||
if let Some(ref description) = event.description {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Description:"}</strong>
|
||||
<span>{description}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
<div class="event-detail">
|
||||
<strong>{"Start:"}</strong>
|
||||
<span>{format_datetime(&event.dtstart, event.all_day)}</span>
|
||||
</div>
|
||||
|
||||
{
|
||||
if let Some(ref end) = event.dtend {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"End:"}</strong>
|
||||
<span>{format_datetime(end, event.all_day)}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
<div class="event-detail">
|
||||
<strong>{"All Day:"}</strong>
|
||||
<span>{if event.all_day { "Yes" } else { "No" }}</span>
|
||||
</div>
|
||||
|
||||
{
|
||||
if let Some(ref location) = event.location {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Location:"}</strong>
|
||||
<span>{location}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
<div class="event-detail">
|
||||
<strong>{"Status:"}</strong>
|
||||
<span>{event.get_status_display()}</span>
|
||||
</div>
|
||||
|
||||
<div class="event-detail">
|
||||
<strong>{"Privacy:"}</strong>
|
||||
<span>{event.get_class_display()}</span>
|
||||
</div>
|
||||
|
||||
<div class="event-detail">
|
||||
<strong>{"Priority:"}</strong>
|
||||
<span>{event.get_priority_display()}</span>
|
||||
</div>
|
||||
|
||||
{
|
||||
if let Some(ref organizer) = event.organizer {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Organizer:"}</strong>
|
||||
<span>{organizer.cal_address.clone()}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if !event.attendees.is_empty() {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Attendees:"}</strong>
|
||||
<span>{event.attendees.iter().map(|a| a.cal_address.clone()).collect::<Vec<_>>().join(", ")}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if !event.categories.is_empty() {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Categories:"}</strong>
|
||||
<span>{event.categories.join(", ")}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if let Some(ref recurrence) = event.rrule {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Repeats:"}</strong>
|
||||
<span>{format_recurrence_rule(recurrence)}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Repeats:"}</strong>
|
||||
<span>{"No"}</span>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if !event.alarms.is_empty() {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Reminders:"}</strong>
|
||||
<span>{"Alarms configured"}</span> /* TODO: Convert VAlarm to displayable format */
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Reminders:"}</strong>
|
||||
<span>{"None"}</span>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if let Some(ref created) = event.created {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Created:"}</strong>
|
||||
<span>{format_datetime(created, false)}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if let Some(ref modified) = event.last_modified {
|
||||
html! {
|
||||
<div class="event-detail">
|
||||
<strong>{"Last Modified:"}</strong>
|
||||
<span>{format_datetime(modified, false)}</span>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
fn format_datetime(dt: &DateTime<Utc>, all_day: bool) -> String {
|
||||
if all_day {
|
||||
dt.format("%B %d, %Y").to_string()
|
||||
} else {
|
||||
dt.format("%B %d, %Y at %I:%M %p").to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn format_recurrence_rule(rrule: &str) -> String {
|
||||
// Basic parsing of RRULE to display user-friendly text
|
||||
if rrule.contains("FREQ=DAILY") {
|
||||
"Daily".to_string()
|
||||
} else if rrule.contains("FREQ=WEEKLY") {
|
||||
"Weekly".to_string()
|
||||
} else if rrule.contains("FREQ=MONTHLY") {
|
||||
"Monthly".to_string()
|
||||
} else if rrule.contains("FREQ=YEARLY") {
|
||||
"Yearly".to_string()
|
||||
} else {
|
||||
// Show the raw rule if we can't parse it
|
||||
format!("Custom ({})", rrule)
|
||||
}
|
||||
}
|
||||
|
||||
206
frontend/src/components/login.rs
Normal file
206
frontend/src/components/login.rs
Normal file
@@ -0,0 +1,206 @@
|
||||
use yew::prelude::*;
|
||||
use web_sys::HtmlInputElement;
|
||||
use gloo_storage::{LocalStorage, Storage};
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct LoginProps {
|
||||
pub on_login: Callback<String>, // Callback with JWT token
|
||||
}
|
||||
|
||||
#[function_component]
|
||||
pub fn Login(props: &LoginProps) -> Html {
|
||||
let server_url = use_state(String::new);
|
||||
let username = use_state(String::new);
|
||||
let password = use_state(String::new);
|
||||
let error_message = use_state(|| Option::<String>::None);
|
||||
let is_loading = use_state(|| false);
|
||||
|
||||
let server_url_ref = use_node_ref();
|
||||
let username_ref = use_node_ref();
|
||||
let password_ref = use_node_ref();
|
||||
|
||||
let on_server_url_change = {
|
||||
let server_url = server_url.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
let target = e.target_unchecked_into::<HtmlInputElement>();
|
||||
server_url.set(target.value());
|
||||
})
|
||||
};
|
||||
|
||||
let on_username_change = {
|
||||
let username = username.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
let target = e.target_unchecked_into::<HtmlInputElement>();
|
||||
username.set(target.value());
|
||||
})
|
||||
};
|
||||
|
||||
let on_password_change = {
|
||||
let password = password.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
let target = e.target_unchecked_into::<HtmlInputElement>();
|
||||
password.set(target.value());
|
||||
})
|
||||
};
|
||||
|
||||
let on_submit = {
|
||||
let server_url = server_url.clone();
|
||||
let username = username.clone();
|
||||
let password = password.clone();
|
||||
let error_message = error_message.clone();
|
||||
let is_loading = is_loading.clone();
|
||||
let on_login = props.on_login.clone();
|
||||
|
||||
Callback::from(move |e: SubmitEvent| {
|
||||
e.prevent_default();
|
||||
|
||||
let server_url = (*server_url).clone();
|
||||
let username = (*username).clone();
|
||||
let password = (*password).clone();
|
||||
let error_message = error_message.clone();
|
||||
let is_loading = is_loading.clone();
|
||||
let on_login = on_login.clone();
|
||||
|
||||
// Basic client-side validation
|
||||
if server_url.trim().is_empty() || username.trim().is_empty() || password.is_empty() {
|
||||
error_message.set(Some("Please fill in all fields".to_string()));
|
||||
return;
|
||||
}
|
||||
|
||||
is_loading.set(true);
|
||||
error_message.set(None);
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
web_sys::console::log_1(&"🚀 Starting login process...".into());
|
||||
match perform_login(server_url.clone(), username.clone(), password.clone()).await {
|
||||
Ok((token, credentials)) => {
|
||||
web_sys::console::log_1(&"✅ Login successful!".into());
|
||||
// Store token and credentials in local storage
|
||||
if let Err(_) = LocalStorage::set("auth_token", &token) {
|
||||
error_message.set(Some("Failed to store authentication token".to_string()));
|
||||
is_loading.set(false);
|
||||
return;
|
||||
}
|
||||
if let Err(_) = LocalStorage::set("caldav_credentials", &credentials) {
|
||||
error_message.set(Some("Failed to store credentials".to_string()));
|
||||
is_loading.set(false);
|
||||
return;
|
||||
}
|
||||
|
||||
is_loading.set(false);
|
||||
on_login.emit(token);
|
||||
}
|
||||
Err(err) => {
|
||||
web_sys::console::log_1(&format!("❌ Login failed: {}", err).into());
|
||||
error_message.set(Some(err));
|
||||
is_loading.set(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div class="login-container">
|
||||
<div class="login-form">
|
||||
<h2>{"Sign In to CalDAV"}</h2>
|
||||
<form onsubmit={on_submit}>
|
||||
<div class="form-group">
|
||||
<label for="server_url">{"CalDAV Server URL"}</label>
|
||||
<input
|
||||
ref={server_url_ref}
|
||||
type="text"
|
||||
id="server_url"
|
||||
placeholder="https://your-caldav-server.com/dav/"
|
||||
value={(*server_url).clone()}
|
||||
onchange={on_server_url_change}
|
||||
disabled={*is_loading}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="username">{"Username"}</label>
|
||||
<input
|
||||
ref={username_ref}
|
||||
type="text"
|
||||
id="username"
|
||||
placeholder="Enter your username"
|
||||
value={(*username).clone()}
|
||||
onchange={on_username_change}
|
||||
disabled={*is_loading}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="password">{"Password"}</label>
|
||||
<input
|
||||
ref={password_ref}
|
||||
type="password"
|
||||
id="password"
|
||||
placeholder="Enter your password"
|
||||
value={(*password).clone()}
|
||||
onchange={on_password_change}
|
||||
disabled={*is_loading}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{
|
||||
if let Some(error) = (*error_message).clone() {
|
||||
html! { <div class="error-message">{error}</div> }
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
|
||||
<button type="submit" disabled={*is_loading} class="login-button">
|
||||
{
|
||||
if *is_loading {
|
||||
"Signing in..."
|
||||
} else {
|
||||
"Sign In"
|
||||
}
|
||||
}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="auth-links">
|
||||
<p>{"Enter your CalDAV server credentials to connect to your calendar"}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
/// Perform login using the CalDAV auth service
|
||||
async fn perform_login(server_url: String, username: String, password: String) -> Result<(String, String), String> {
|
||||
use crate::auth::{AuthService, CalDAVLoginRequest};
|
||||
use serde_json;
|
||||
|
||||
web_sys::console::log_1(&format!("📡 Creating auth service and request...").into());
|
||||
|
||||
let auth_service = AuthService::new();
|
||||
let request = CalDAVLoginRequest {
|
||||
server_url: server_url.clone(),
|
||||
username: username.clone(),
|
||||
password: password.clone()
|
||||
};
|
||||
|
||||
web_sys::console::log_1(&format!("🚀 Sending login request to backend...").into());
|
||||
|
||||
match auth_service.login(request).await {
|
||||
Ok(response) => {
|
||||
web_sys::console::log_1(&format!("✅ Backend responded successfully").into());
|
||||
// Create credentials object to store
|
||||
let credentials = serde_json::json!({
|
||||
"server_url": server_url,
|
||||
"username": username,
|
||||
"password": password
|
||||
});
|
||||
Ok((response.token, credentials.to_string()))
|
||||
},
|
||||
Err(err) => {
|
||||
web_sys::console::log_1(&format!("❌ Backend error: {}", err).into());
|
||||
Err(err)
|
||||
},
|
||||
}
|
||||
}
|
||||
31
frontend/src/components/mod.rs
Normal file
31
frontend/src/components/mod.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
pub mod login;
|
||||
pub mod calendar;
|
||||
pub mod calendar_header;
|
||||
pub mod month_view;
|
||||
pub mod week_view;
|
||||
pub mod event_modal;
|
||||
pub mod create_calendar_modal;
|
||||
pub mod context_menu;
|
||||
pub mod event_context_menu;
|
||||
pub mod calendar_context_menu;
|
||||
pub mod create_event_modal;
|
||||
pub mod sidebar;
|
||||
pub mod calendar_list_item;
|
||||
pub mod route_handler;
|
||||
pub mod recurring_edit_modal;
|
||||
|
||||
pub use login::Login;
|
||||
pub use calendar::Calendar;
|
||||
pub use calendar_header::CalendarHeader;
|
||||
pub use month_view::MonthView;
|
||||
pub use week_view::WeekView;
|
||||
pub use event_modal::EventModal;
|
||||
pub use create_calendar_modal::CreateCalendarModal;
|
||||
pub use context_menu::ContextMenu;
|
||||
pub use event_context_menu::{EventContextMenu, DeleteAction};
|
||||
pub use calendar_context_menu::CalendarContextMenu;
|
||||
pub use create_event_modal::{CreateEventModal, EventCreationData, EventStatus, EventClass, ReminderType, RecurrenceType};
|
||||
pub use sidebar::{Sidebar, ViewMode, Theme};
|
||||
pub use calendar_list_item::CalendarListItem;
|
||||
pub use route_handler::RouteHandler;
|
||||
pub use recurring_edit_modal::{RecurringEditModal, RecurringEditAction};
|
||||
268
frontend/src/components/month_view.rs
Normal file
268
frontend/src/components/month_view.rs
Normal file
@@ -0,0 +1,268 @@
|
||||
use yew::prelude::*;
|
||||
use chrono::{Datelike, NaiveDate, Weekday};
|
||||
use std::collections::HashMap;
|
||||
use web_sys::window;
|
||||
use wasm_bindgen::{prelude::*, JsCast};
|
||||
use crate::services::calendar_service::UserInfo;
|
||||
use crate::models::ical::VEvent;
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct MonthViewProps {
|
||||
pub current_month: NaiveDate,
|
||||
pub today: NaiveDate,
|
||||
pub events: HashMap<NaiveDate, Vec<VEvent>>,
|
||||
pub on_event_click: Callback<VEvent>,
|
||||
#[prop_or_default]
|
||||
pub refreshing_event_uid: Option<String>,
|
||||
#[prop_or_default]
|
||||
pub user_info: Option<UserInfo>,
|
||||
#[prop_or_default]
|
||||
pub on_event_context_menu: Option<Callback<(web_sys::MouseEvent, VEvent)>>,
|
||||
#[prop_or_default]
|
||||
pub on_calendar_context_menu: Option<Callback<(web_sys::MouseEvent, NaiveDate)>>,
|
||||
#[prop_or_default]
|
||||
pub selected_date: Option<NaiveDate>,
|
||||
#[prop_or_default]
|
||||
pub on_day_select: Option<Callback<NaiveDate>>,
|
||||
}
|
||||
|
||||
#[function_component(MonthView)]
|
||||
pub fn month_view(props: &MonthViewProps) -> Html {
|
||||
let max_events_per_day = use_state(|| 4); // Default to 4 events max
|
||||
let first_day_of_month = props.current_month.with_day(1).unwrap();
|
||||
let days_in_month = get_days_in_month(props.current_month);
|
||||
let first_weekday = first_day_of_month.weekday();
|
||||
let days_from_prev_month = get_days_from_previous_month(props.current_month, first_weekday);
|
||||
|
||||
// Calculate maximum events that can fit based on available height
|
||||
let calculate_max_events = {
|
||||
let max_events_per_day = max_events_per_day.clone();
|
||||
move || {
|
||||
// Since we're using CSS Grid with equal row heights,
|
||||
// we can estimate based on typical calendar dimensions
|
||||
// Typical calendar height is around 600-800px for 6 rows
|
||||
// Each row gets ~100-133px, minus day number and padding leaves ~70-100px
|
||||
// Each event is ~18px, so we can fit ~3-4 events + "+n more" indicator
|
||||
max_events_per_day.set(3);
|
||||
}
|
||||
};
|
||||
|
||||
// Setup resize handler and initial calculation
|
||||
{
|
||||
let calculate_max_events = calculate_max_events.clone();
|
||||
use_effect_with((), move |_| {
|
||||
let calculate_max_events_clone = calculate_max_events.clone();
|
||||
|
||||
// Initial calculation with a slight delay to ensure DOM is ready
|
||||
if let Some(window) = window() {
|
||||
let timeout_closure = Closure::wrap(Box::new(move || {
|
||||
calculate_max_events_clone();
|
||||
}) as Box<dyn FnMut()>);
|
||||
|
||||
let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(
|
||||
timeout_closure.as_ref().unchecked_ref(),
|
||||
100,
|
||||
);
|
||||
timeout_closure.forget();
|
||||
}
|
||||
|
||||
// Setup resize listener
|
||||
let resize_closure = Closure::wrap(Box::new(move || {
|
||||
calculate_max_events();
|
||||
}) as Box<dyn Fn()>);
|
||||
|
||||
if let Some(window) = window() {
|
||||
let _ = window.add_event_listener_with_callback("resize", resize_closure.as_ref().unchecked_ref());
|
||||
resize_closure.forget(); // Keep the closure alive
|
||||
}
|
||||
|
||||
|| {}
|
||||
});
|
||||
}
|
||||
|
||||
// Helper function to get calendar color for an event
|
||||
let get_event_color = |event: &VEvent| -> String {
|
||||
if let Some(user_info) = &props.user_info {
|
||||
if let Some(calendar_path) = &event.calendar_path {
|
||||
if let Some(calendar) = user_info.calendars.iter()
|
||||
.find(|cal| &cal.path == calendar_path) {
|
||||
return calendar.color.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
"#3B82F6".to_string()
|
||||
};
|
||||
|
||||
html! {
|
||||
<div class="calendar-grid">
|
||||
// Weekday headers
|
||||
<div class="weekday-header">{"Sun"}</div>
|
||||
<div class="weekday-header">{"Mon"}</div>
|
||||
<div class="weekday-header">{"Tue"}</div>
|
||||
<div class="weekday-header">{"Wed"}</div>
|
||||
<div class="weekday-header">{"Thu"}</div>
|
||||
<div class="weekday-header">{"Fri"}</div>
|
||||
<div class="weekday-header">{"Sat"}</div>
|
||||
|
||||
// Days from previous month (grayed out)
|
||||
{
|
||||
days_from_prev_month.iter().map(|day| {
|
||||
html! {
|
||||
<div class="calendar-day prev-month">{*day}</div>
|
||||
}
|
||||
}).collect::<Html>()
|
||||
}
|
||||
|
||||
// Days of the current month
|
||||
{
|
||||
(1..=days_in_month).map(|day| {
|
||||
let date = props.current_month.with_day(day).unwrap();
|
||||
let is_today = date == props.today;
|
||||
let is_selected = props.selected_date == Some(date);
|
||||
let day_events = props.events.get(&date).cloned().unwrap_or_default();
|
||||
|
||||
// Calculate visible events and overflow
|
||||
let max_events = *max_events_per_day as usize;
|
||||
let visible_events: Vec<_> = day_events.iter().take(max_events).collect();
|
||||
let hidden_count = day_events.len().saturating_sub(max_events);
|
||||
|
||||
html! {
|
||||
<div
|
||||
class={classes!(
|
||||
"calendar-day",
|
||||
if is_today { Some("today") } else { None },
|
||||
if is_selected { Some("selected") } else { None }
|
||||
)}
|
||||
onclick={
|
||||
if let Some(callback) = &props.on_day_select {
|
||||
let callback = callback.clone();
|
||||
Some(Callback::from(move |e: web_sys::MouseEvent| {
|
||||
e.stop_propagation(); // Prevent other handlers
|
||||
callback.emit(date);
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
oncontextmenu={
|
||||
if let Some(callback) = &props.on_calendar_context_menu {
|
||||
let callback = callback.clone();
|
||||
Some(Callback::from(move |e: web_sys::MouseEvent| {
|
||||
e.prevent_default();
|
||||
callback.emit((e, date));
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
>
|
||||
<div class="day-number">{day}</div>
|
||||
<div class="day-events">
|
||||
{
|
||||
visible_events.iter().map(|event| {
|
||||
let event_color = get_event_color(event);
|
||||
let is_refreshing = props.refreshing_event_uid.as_ref() == Some(&event.uid);
|
||||
|
||||
let onclick = {
|
||||
let on_event_click = props.on_event_click.clone();
|
||||
let event = (*event).clone();
|
||||
Callback::from(move |_: web_sys::MouseEvent| {
|
||||
on_event_click.emit(event.clone());
|
||||
})
|
||||
};
|
||||
|
||||
let oncontextmenu = {
|
||||
if let Some(callback) = &props.on_event_context_menu {
|
||||
let callback = callback.clone();
|
||||
let event = (*event).clone();
|
||||
Some(Callback::from(move |e: web_sys::MouseEvent| {
|
||||
e.prevent_default();
|
||||
callback.emit((e, event.clone()));
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
html! {
|
||||
<div
|
||||
class={classes!("event-box", if is_refreshing { Some("refreshing") } else { None })}
|
||||
style={format!("background-color: {}", event_color)}
|
||||
{onclick}
|
||||
{oncontextmenu}
|
||||
>
|
||||
{event.summary.as_ref().unwrap_or(&"Untitled".to_string())}
|
||||
</div>
|
||||
}
|
||||
}).collect::<Html>()
|
||||
}
|
||||
{
|
||||
if hidden_count > 0 {
|
||||
html! {
|
||||
<div class="more-events-indicator">
|
||||
{format!("+{} more", hidden_count)}
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}).collect::<Html>()
|
||||
}
|
||||
|
||||
{ render_next_month_days(days_from_prev_month.len(), days_in_month) }
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
fn render_next_month_days(prev_days_count: usize, current_days_count: u32) -> Html {
|
||||
let total_slots = 42; // 6 rows x 7 days
|
||||
let used_slots = prev_days_count + current_days_count as usize;
|
||||
let remaining_slots = if used_slots < total_slots { total_slots - used_slots } else { 0 };
|
||||
|
||||
(1..=remaining_slots).map(|day| {
|
||||
html! {
|
||||
<div class="calendar-day next-month">{day}</div>
|
||||
}
|
||||
}).collect::<Html>()
|
||||
}
|
||||
|
||||
fn get_days_in_month(date: NaiveDate) -> u32 {
|
||||
NaiveDate::from_ymd_opt(
|
||||
if date.month() == 12 { date.year() + 1 } else { date.year() },
|
||||
if date.month() == 12 { 1 } else { date.month() + 1 },
|
||||
1
|
||||
)
|
||||
.unwrap()
|
||||
.pred_opt()
|
||||
.unwrap()
|
||||
.day()
|
||||
}
|
||||
|
||||
fn get_days_from_previous_month(current_month: NaiveDate, first_weekday: Weekday) -> Vec<u32> {
|
||||
let days_before = match first_weekday {
|
||||
Weekday::Sun => 0,
|
||||
Weekday::Mon => 1,
|
||||
Weekday::Tue => 2,
|
||||
Weekday::Wed => 3,
|
||||
Weekday::Thu => 4,
|
||||
Weekday::Fri => 5,
|
||||
Weekday::Sat => 6,
|
||||
};
|
||||
|
||||
if days_before == 0 {
|
||||
vec![]
|
||||
} else {
|
||||
let prev_month = if current_month.month() == 1 {
|
||||
NaiveDate::from_ymd_opt(current_month.year() - 1, 12, 1).unwrap()
|
||||
} else {
|
||||
NaiveDate::from_ymd_opt(current_month.year(), current_month.month() - 1, 1).unwrap()
|
||||
};
|
||||
|
||||
let prev_month_days = get_days_in_month(prev_month);
|
||||
((prev_month_days - days_before as u32 + 1)..=prev_month_days).collect()
|
||||
}
|
||||
}
|
||||
93
frontend/src/components/recurring_edit_modal.rs
Normal file
93
frontend/src/components/recurring_edit_modal.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use yew::prelude::*;
|
||||
use chrono::NaiveDateTime;
|
||||
use crate::models::ical::VEvent;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum RecurringEditAction {
|
||||
ThisEvent,
|
||||
FutureEvents,
|
||||
AllEvents,
|
||||
}
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct RecurringEditModalProps {
|
||||
pub show: bool,
|
||||
pub event: VEvent,
|
||||
pub new_start: NaiveDateTime,
|
||||
pub new_end: NaiveDateTime,
|
||||
pub on_choice: Callback<RecurringEditAction>,
|
||||
pub on_cancel: Callback<()>,
|
||||
}
|
||||
|
||||
#[function_component(RecurringEditModal)]
|
||||
pub fn recurring_edit_modal(props: &RecurringEditModalProps) -> Html {
|
||||
if !props.show {
|
||||
return html! {};
|
||||
}
|
||||
|
||||
let event_title = props.event.summary.as_ref().map(|s| s.as_str()).unwrap_or("Untitled Event");
|
||||
|
||||
let on_this_event = {
|
||||
let on_choice = props.on_choice.clone();
|
||||
Callback::from(move |_| {
|
||||
on_choice.emit(RecurringEditAction::ThisEvent);
|
||||
})
|
||||
};
|
||||
|
||||
let on_future_events = {
|
||||
let on_choice = props.on_choice.clone();
|
||||
Callback::from(move |_| {
|
||||
on_choice.emit(RecurringEditAction::FutureEvents);
|
||||
})
|
||||
};
|
||||
|
||||
let on_all_events = {
|
||||
let on_choice = props.on_choice.clone();
|
||||
Callback::from(move |_| {
|
||||
on_choice.emit(RecurringEditAction::AllEvents);
|
||||
})
|
||||
};
|
||||
|
||||
let on_cancel = {
|
||||
let on_cancel = props.on_cancel.clone();
|
||||
Callback::from(move |_| {
|
||||
on_cancel.emit(());
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<div class="modal-backdrop">
|
||||
<div class="modal-content recurring-edit-modal">
|
||||
<div class="modal-header">
|
||||
<h3>{"Edit Recurring Event"}</h3>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>{format!("You're modifying \"{}\" which is part of a recurring series.", event_title)}</p>
|
||||
<p>{"How would you like to apply this change?"}</p>
|
||||
|
||||
<div class="recurring-edit-options">
|
||||
<button class="btn btn-primary recurring-option" onclick={on_this_event}>
|
||||
<div class="option-title">{"This event only"}</div>
|
||||
<div class="option-description">{"Change only this occurrence"}</div>
|
||||
</button>
|
||||
|
||||
<button class="btn btn-primary recurring-option" onclick={on_future_events}>
|
||||
<div class="option-title">{"This and future events"}</div>
|
||||
<div class="option-description">{"Change this occurrence and all future occurrences"}</div>
|
||||
</button>
|
||||
|
||||
<button class="btn btn-primary recurring-option" onclick={on_all_events}>
|
||||
<div class="option-title">{"All events in series"}</div>
|
||||
<div class="option-description">{"Change all occurrences in the series"}</div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" onclick={on_cancel}>
|
||||
{"Cancel"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
297
frontend/src/components/route_handler.rs
Normal file
297
frontend/src/components/route_handler.rs
Normal file
@@ -0,0 +1,297 @@
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::*;
|
||||
use crate::components::{Login, ViewMode};
|
||||
use crate::services::calendar_service::UserInfo;
|
||||
use crate::models::ical::VEvent;
|
||||
|
||||
#[derive(Clone, Routable, PartialEq)]
|
||||
pub enum Route {
|
||||
#[at("/")]
|
||||
Home,
|
||||
#[at("/login")]
|
||||
Login,
|
||||
#[at("/calendar")]
|
||||
Calendar,
|
||||
}
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct RouteHandlerProps {
|
||||
pub auth_token: Option<String>,
|
||||
pub user_info: Option<UserInfo>,
|
||||
pub on_login: Callback<String>,
|
||||
#[prop_or_default]
|
||||
pub on_event_context_menu: Option<Callback<(web_sys::MouseEvent, VEvent)>>,
|
||||
#[prop_or_default]
|
||||
pub on_calendar_context_menu: Option<Callback<(web_sys::MouseEvent, chrono::NaiveDate)>>,
|
||||
#[prop_or_default]
|
||||
pub view: ViewMode,
|
||||
#[prop_or_default]
|
||||
pub on_create_event_request: Option<Callback<crate::components::EventCreationData>>,
|
||||
#[prop_or_default]
|
||||
pub on_event_update_request: Option<Callback<(VEvent, chrono::NaiveDateTime, chrono::NaiveDateTime, bool, Option<chrono::DateTime<chrono::Utc>>)>>,
|
||||
#[prop_or_default]
|
||||
pub context_menus_open: bool,
|
||||
}
|
||||
|
||||
#[function_component(RouteHandler)]
|
||||
pub fn route_handler(props: &RouteHandlerProps) -> Html {
|
||||
let auth_token = props.auth_token.clone();
|
||||
let user_info = props.user_info.clone();
|
||||
let on_login = props.on_login.clone();
|
||||
let on_event_context_menu = props.on_event_context_menu.clone();
|
||||
let on_calendar_context_menu = props.on_calendar_context_menu.clone();
|
||||
let view = props.view.clone();
|
||||
let on_create_event_request = props.on_create_event_request.clone();
|
||||
let on_event_update_request = props.on_event_update_request.clone();
|
||||
let context_menus_open = props.context_menus_open;
|
||||
|
||||
html! {
|
||||
<Switch<Route> render={move |route| {
|
||||
let auth_token = auth_token.clone();
|
||||
let user_info = user_info.clone();
|
||||
let on_login = on_login.clone();
|
||||
let on_event_context_menu = on_event_context_menu.clone();
|
||||
let on_calendar_context_menu = on_calendar_context_menu.clone();
|
||||
let view = view.clone();
|
||||
let on_create_event_request = on_create_event_request.clone();
|
||||
let on_event_update_request = on_event_update_request.clone();
|
||||
let context_menus_open = context_menus_open;
|
||||
|
||||
match route {
|
||||
Route::Home => {
|
||||
if auth_token.is_some() {
|
||||
html! { <Redirect<Route> to={Route::Calendar}/> }
|
||||
} else {
|
||||
html! { <Redirect<Route> to={Route::Login}/> }
|
||||
}
|
||||
}
|
||||
Route::Login => {
|
||||
if auth_token.is_some() {
|
||||
html! { <Redirect<Route> to={Route::Calendar}/> }
|
||||
} else {
|
||||
html! { <Login {on_login} /> }
|
||||
}
|
||||
}
|
||||
Route::Calendar => {
|
||||
if auth_token.is_some() {
|
||||
html! {
|
||||
<CalendarView
|
||||
user_info={user_info}
|
||||
on_event_context_menu={on_event_context_menu}
|
||||
on_calendar_context_menu={on_calendar_context_menu}
|
||||
view={view}
|
||||
on_create_event_request={on_create_event_request}
|
||||
on_event_update_request={on_event_update_request}
|
||||
context_menus_open={context_menus_open}
|
||||
/>
|
||||
}
|
||||
} else {
|
||||
html! { <Redirect<Route> to={Route::Login}/> }
|
||||
}
|
||||
}
|
||||
}
|
||||
}} />
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct CalendarViewProps {
|
||||
pub user_info: Option<UserInfo>,
|
||||
#[prop_or_default]
|
||||
pub on_event_context_menu: Option<Callback<(web_sys::MouseEvent, VEvent)>>,
|
||||
#[prop_or_default]
|
||||
pub on_calendar_context_menu: Option<Callback<(web_sys::MouseEvent, chrono::NaiveDate)>>,
|
||||
#[prop_or_default]
|
||||
pub view: ViewMode,
|
||||
#[prop_or_default]
|
||||
pub on_create_event_request: Option<Callback<crate::components::EventCreationData>>,
|
||||
#[prop_or_default]
|
||||
pub on_event_update_request: Option<Callback<(VEvent, chrono::NaiveDateTime, chrono::NaiveDateTime, bool, Option<chrono::DateTime<chrono::Utc>>)>>,
|
||||
#[prop_or_default]
|
||||
pub context_menus_open: bool,
|
||||
}
|
||||
|
||||
use gloo_storage::{LocalStorage, Storage};
|
||||
use crate::services::CalendarService;
|
||||
use crate::components::Calendar;
|
||||
use std::collections::HashMap;
|
||||
use chrono::{Local, NaiveDate, Datelike};
|
||||
|
||||
#[function_component(CalendarView)]
|
||||
pub fn calendar_view(props: &CalendarViewProps) -> Html {
|
||||
let events = use_state(|| HashMap::<NaiveDate, Vec<VEvent>>::new());
|
||||
let loading = use_state(|| true);
|
||||
let error = use_state(|| None::<String>);
|
||||
let refreshing_event = use_state(|| None::<String>);
|
||||
|
||||
let auth_token: Option<String> = LocalStorage::get("auth_token").ok();
|
||||
|
||||
|
||||
let today = Local::now().date_naive();
|
||||
let current_year = today.year();
|
||||
let current_month = today.month();
|
||||
|
||||
let on_event_click = {
|
||||
let events = events.clone();
|
||||
let refreshing_event = refreshing_event.clone();
|
||||
let auth_token = auth_token.clone();
|
||||
|
||||
Callback::from(move |event: VEvent| {
|
||||
if let Some(token) = auth_token.clone() {
|
||||
let events = events.clone();
|
||||
let refreshing_event = refreshing_event.clone();
|
||||
let uid = event.uid.clone();
|
||||
|
||||
refreshing_event.set(Some(uid.clone()));
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
match calendar_service.refresh_event(&token, &password, &uid).await {
|
||||
Ok(Some(refreshed_event)) => {
|
||||
let refreshed_vevent = VEvent::from_calendar_event(&refreshed_event);
|
||||
let mut updated_events = (*events).clone();
|
||||
|
||||
for (_, day_events) in updated_events.iter_mut() {
|
||||
day_events.retain(|e| e.uid != uid);
|
||||
}
|
||||
|
||||
if refreshed_vevent.rrule.is_some() {
|
||||
let new_occurrences = CalendarService::expand_recurring_events(vec![refreshed_vevent.clone()]);
|
||||
|
||||
for occurrence in new_occurrences {
|
||||
let date = occurrence.get_date();
|
||||
updated_events.entry(date)
|
||||
.or_insert_with(Vec::new)
|
||||
.push(occurrence);
|
||||
}
|
||||
} else {
|
||||
let date = refreshed_vevent.get_date();
|
||||
updated_events.entry(date)
|
||||
.or_insert_with(Vec::new)
|
||||
.push(refreshed_vevent);
|
||||
}
|
||||
|
||||
events.set(updated_events);
|
||||
}
|
||||
Ok(None) => {
|
||||
let mut updated_events = (*events).clone();
|
||||
for (_, day_events) in updated_events.iter_mut() {
|
||||
day_events.retain(|e| e.uid != uid);
|
||||
}
|
||||
events.set(updated_events);
|
||||
}
|
||||
Err(_err) => {
|
||||
}
|
||||
}
|
||||
|
||||
refreshing_event.set(None);
|
||||
});
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
{
|
||||
let events = events.clone();
|
||||
let loading = loading.clone();
|
||||
let error = error.clone();
|
||||
let auth_token = auth_token.clone();
|
||||
|
||||
use_effect_with((), move |_| {
|
||||
if let Some(token) = auth_token {
|
||||
let events = events.clone();
|
||||
let loading = loading.clone();
|
||||
let error = error.clone();
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async move {
|
||||
let calendar_service = CalendarService::new();
|
||||
|
||||
let password = if let Ok(credentials_str) = LocalStorage::get::<String>("caldav_credentials") {
|
||||
if let Ok(credentials) = serde_json::from_str::<serde_json::Value>(&credentials_str) {
|
||||
credentials["password"].as_str().unwrap_or("").to_string()
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
match calendar_service.fetch_events_for_month_vevent(&token, &password, current_year, current_month).await {
|
||||
Ok(vevents) => {
|
||||
let grouped_events = CalendarService::group_events_by_date(vevents);
|
||||
events.set(grouped_events);
|
||||
loading.set(false);
|
||||
}
|
||||
Err(err) => {
|
||||
error.set(Some(format!("Failed to load events: {}", err)));
|
||||
loading.set(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
loading.set(false);
|
||||
error.set(Some("No authentication token found".to_string()));
|
||||
}
|
||||
|
||||
|| ()
|
||||
});
|
||||
}
|
||||
|
||||
html! {
|
||||
<div class="calendar-view">
|
||||
{
|
||||
if *loading {
|
||||
html! {
|
||||
<div class="calendar-loading">
|
||||
<p>{"Loading calendar events..."}</p>
|
||||
</div>
|
||||
}
|
||||
} else if let Some(err) = (*error).clone() {
|
||||
let dummy_callback = Callback::from(|_: VEvent| {});
|
||||
html! {
|
||||
<div class="calendar-error">
|
||||
<p>{format!("Error: {}", err)}</p>
|
||||
<Calendar
|
||||
events={HashMap::new()}
|
||||
on_event_click={dummy_callback}
|
||||
refreshing_event_uid={(*refreshing_event).clone()}
|
||||
user_info={props.user_info.clone()}
|
||||
on_event_context_menu={props.on_event_context_menu.clone()}
|
||||
on_calendar_context_menu={props.on_calendar_context_menu.clone()}
|
||||
view={props.view.clone()}
|
||||
on_create_event_request={props.on_create_event_request.clone()}
|
||||
on_event_update_request={props.on_event_update_request.clone()}
|
||||
context_menus_open={props.context_menus_open}
|
||||
/>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! {
|
||||
<Calendar
|
||||
events={(*events).clone()}
|
||||
on_event_click={on_event_click}
|
||||
refreshing_event_uid={(*refreshing_event).clone()}
|
||||
user_info={props.user_info.clone()}
|
||||
on_event_context_menu={props.on_event_context_menu.clone()}
|
||||
on_calendar_context_menu={props.on_calendar_context_menu.clone()}
|
||||
view={props.view.clone()}
|
||||
on_create_event_request={props.on_create_event_request.clone()}
|
||||
on_event_update_request={props.on_event_update_request.clone()}
|
||||
context_menus_open={props.context_menus_open}
|
||||
/>
|
||||
}
|
||||
}
|
||||
}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
195
frontend/src/components/sidebar.rs
Normal file
195
frontend/src/components/sidebar.rs
Normal file
@@ -0,0 +1,195 @@
|
||||
use yew::prelude::*;
|
||||
use yew_router::prelude::*;
|
||||
use web_sys::HtmlSelectElement;
|
||||
use crate::services::calendar_service::UserInfo;
|
||||
use crate::components::CalendarListItem;
|
||||
|
||||
#[derive(Clone, Routable, PartialEq)]
|
||||
pub enum Route {
|
||||
#[at("/")]
|
||||
Home,
|
||||
#[at("/login")]
|
||||
Login,
|
||||
#[at("/calendar")]
|
||||
Calendar,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum ViewMode {
|
||||
Month,
|
||||
Week,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Theme {
|
||||
Default,
|
||||
Ocean,
|
||||
Forest,
|
||||
Sunset,
|
||||
Purple,
|
||||
Dark,
|
||||
Rose,
|
||||
Mint,
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
|
||||
pub fn value(&self) -> &'static str {
|
||||
match self {
|
||||
Theme::Default => "default",
|
||||
Theme::Ocean => "ocean",
|
||||
Theme::Forest => "forest",
|
||||
Theme::Sunset => "sunset",
|
||||
Theme::Purple => "purple",
|
||||
Theme::Dark => "dark",
|
||||
Theme::Rose => "rose",
|
||||
Theme::Mint => "mint",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_value(value: &str) -> Self {
|
||||
match value {
|
||||
"ocean" => Theme::Ocean,
|
||||
"forest" => Theme::Forest,
|
||||
"sunset" => Theme::Sunset,
|
||||
"purple" => Theme::Purple,
|
||||
"dark" => Theme::Dark,
|
||||
"rose" => Theme::Rose,
|
||||
"mint" => Theme::Mint,
|
||||
_ => Theme::Default,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ViewMode {
|
||||
fn default() -> Self {
|
||||
ViewMode::Month
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Properties, PartialEq)]
|
||||
pub struct SidebarProps {
|
||||
pub user_info: Option<UserInfo>,
|
||||
pub on_logout: Callback<()>,
|
||||
pub on_create_calendar: Callback<()>,
|
||||
pub color_picker_open: Option<String>,
|
||||
pub on_color_change: Callback<(String, String)>,
|
||||
pub on_color_picker_toggle: Callback<String>,
|
||||
pub available_colors: Vec<String>,
|
||||
pub on_calendar_context_menu: Callback<(MouseEvent, String)>,
|
||||
pub current_view: ViewMode,
|
||||
pub on_view_change: Callback<ViewMode>,
|
||||
pub current_theme: Theme,
|
||||
pub on_theme_change: Callback<Theme>,
|
||||
}
|
||||
|
||||
#[function_component(Sidebar)]
|
||||
pub fn sidebar(props: &SidebarProps) -> Html {
|
||||
let on_view_change = {
|
||||
let on_view_change = props.on_view_change.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
let target = e.target_dyn_into::<HtmlSelectElement>();
|
||||
if let Some(select) = target {
|
||||
let value = select.value();
|
||||
let new_view = match value.as_str() {
|
||||
"week" => ViewMode::Week,
|
||||
_ => ViewMode::Month,
|
||||
};
|
||||
on_view_change.emit(new_view);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let on_theme_change = {
|
||||
let on_theme_change = props.on_theme_change.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
let target = e.target_dyn_into::<HtmlSelectElement>();
|
||||
if let Some(select) = target {
|
||||
let value = select.value();
|
||||
let new_theme = Theme::from_value(&value);
|
||||
on_theme_change.emit(new_theme);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
html! {
|
||||
<aside class="app-sidebar">
|
||||
<div class="sidebar-header">
|
||||
<h1>{"Calendar App"}</h1>
|
||||
{
|
||||
if let Some(ref info) = props.user_info {
|
||||
html! {
|
||||
<div class="user-info">
|
||||
<div class="username">{&info.username}</div>
|
||||
<div class="server-url">{&info.server_url}</div>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! { <div class="user-info loading">{"Loading..."}</div> }
|
||||
}
|
||||
}
|
||||
</div>
|
||||
<nav class="sidebar-nav">
|
||||
<Link<Route> to={Route::Calendar} classes="nav-link">{"Calendar"}</Link<Route>>
|
||||
</nav>
|
||||
{
|
||||
if let Some(ref info) = props.user_info {
|
||||
if !info.calendars.is_empty() {
|
||||
html! {
|
||||
<div class="calendar-list">
|
||||
<h3>{"My Calendars"}</h3>
|
||||
<ul>
|
||||
{
|
||||
info.calendars.iter().map(|cal| {
|
||||
html! {
|
||||
<CalendarListItem
|
||||
calendar={cal.clone()}
|
||||
color_picker_open={props.color_picker_open.as_ref() == Some(&cal.path)}
|
||||
on_color_change={props.on_color_change.clone()}
|
||||
on_color_picker_toggle={props.on_color_picker_toggle.clone()}
|
||||
available_colors={props.available_colors.clone()}
|
||||
on_context_menu={props.on_calendar_context_menu.clone()}
|
||||
/>
|
||||
}
|
||||
}).collect::<Html>()
|
||||
}
|
||||
</ul>
|
||||
</div>
|
||||
}
|
||||
} else {
|
||||
html! { <div class="no-calendars">{"No calendars found"}</div> }
|
||||
}
|
||||
} else {
|
||||
html! {}
|
||||
}
|
||||
}
|
||||
<div class="sidebar-footer">
|
||||
<button onclick={props.on_create_calendar.reform(|_| ())} class="create-calendar-button">
|
||||
{"+ Create Calendar"}
|
||||
</button>
|
||||
|
||||
<div class="view-selector">
|
||||
<select class="view-selector-dropdown" onchange={on_view_change}>
|
||||
<option value="month" selected={matches!(props.current_view, ViewMode::Month)}>{"Month"}</option>
|
||||
<option value="week" selected={matches!(props.current_view, ViewMode::Week)}>{"Week"}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="theme-selector">
|
||||
<select class="theme-selector-dropdown" onchange={on_theme_change}>
|
||||
<option value="default" selected={matches!(props.current_theme, Theme::Default)}>{"🎨 Default"}</option>
|
||||
<option value="ocean" selected={matches!(props.current_theme, Theme::Ocean)}>{"🌊 Ocean"}</option>
|
||||
<option value="forest" selected={matches!(props.current_theme, Theme::Forest)}>{"🌲 Forest"}</option>
|
||||
<option value="sunset" selected={matches!(props.current_theme, Theme::Sunset)}>{"🌅 Sunset"}</option>
|
||||
<option value="purple" selected={matches!(props.current_theme, Theme::Purple)}>{"💜 Purple"}</option>
|
||||
<option value="dark" selected={matches!(props.current_theme, Theme::Dark)}>{"🌙 Dark"}</option>
|
||||
<option value="rose" selected={matches!(props.current_theme, Theme::Rose)}>{"🌹 Rose"}</option>
|
||||
<option value="mint" selected={matches!(props.current_theme, Theme::Mint)}>{"🍃 Mint"}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button onclick={props.on_logout.reform(|_| ())} class="logout-button">{"Logout"}</button>
|
||||
</div>
|
||||
</aside>
|
||||
}
|
||||
}
|
||||
1047
frontend/src/components/week_view.rs
Normal file
1047
frontend/src/components/week_view.rs
Normal file
File diff suppressed because it is too large
Load Diff
284
frontend/src/config.rs
Normal file
284
frontend/src/config.rs
Normal file
@@ -0,0 +1,284 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
use base64::prelude::*;
|
||||
|
||||
/// Configuration for CalDAV server connection and authentication.
|
||||
///
|
||||
/// This struct holds all the necessary information to connect to a CalDAV server,
|
||||
/// including server URL, credentials, and optional collection paths.
|
||||
///
|
||||
/// # Security Note
|
||||
///
|
||||
/// The password field contains sensitive information and should be handled carefully.
|
||||
/// This struct implements `Debug` but in production, consider implementing a custom
|
||||
/// `Debug` that masks the password field.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use crate::config::CalDAVConfig;
|
||||
///
|
||||
/// // Load configuration from environment variables
|
||||
/// let config = CalDAVConfig::from_env()?;
|
||||
///
|
||||
/// // Use the configuration for HTTP requests
|
||||
/// let auth_header = format!("Basic {}", config.get_basic_auth());
|
||||
/// ```
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CalDAVConfig {
|
||||
/// The base URL of the CalDAV server (e.g., "https://caldav.example.com/dav/")
|
||||
pub server_url: String,
|
||||
|
||||
/// Username for authentication with the CalDAV server
|
||||
pub username: String,
|
||||
|
||||
/// Password for authentication with the CalDAV server
|
||||
///
|
||||
/// **Security Note**: This contains sensitive information
|
||||
pub password: String,
|
||||
|
||||
/// Optional path to the calendar collection on the server
|
||||
///
|
||||
/// If not provided, the client will need to discover available calendars
|
||||
/// through CalDAV PROPFIND requests
|
||||
pub calendar_path: Option<String>,
|
||||
|
||||
/// Optional path to the tasks/todo collection on the server
|
||||
///
|
||||
/// Some CalDAV servers store tasks separately from calendar events
|
||||
pub tasks_path: Option<String>,
|
||||
}
|
||||
|
||||
impl CalDAVConfig {
|
||||
/// Creates a new CalDAVConfig by loading values from environment variables.
|
||||
///
|
||||
/// This method will attempt to load a `.env` file from the current directory
|
||||
/// and then read the following required environment variables:
|
||||
///
|
||||
/// - `CALDAV_SERVER_URL`: The CalDAV server base URL
|
||||
/// - `CALDAV_USERNAME`: Username for authentication
|
||||
/// - `CALDAV_PASSWORD`: Password for authentication
|
||||
///
|
||||
/// Optional environment variables:
|
||||
///
|
||||
/// - `CALDAV_CALENDAR_PATH`: Path to calendar collection
|
||||
/// - `CALDAV_TASKS_PATH`: Path to tasks collection
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns `ConfigError::MissingVar` if any required environment variable
|
||||
/// is not set or cannot be read.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use crate::config::CalDAVConfig;
|
||||
///
|
||||
/// match CalDAVConfig::from_env() {
|
||||
/// Ok(config) => {
|
||||
/// println!("Loaded config for server: {}", config.server_url);
|
||||
/// }
|
||||
/// Err(e) => {
|
||||
/// eprintln!("Failed to load config: {}", e);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn from_env() -> Result<Self, ConfigError> {
|
||||
// Attempt to load .env file, but don't fail if it doesn't exist
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
let server_url = env::var("CALDAV_SERVER_URL")
|
||||
.map_err(|_| ConfigError::MissingVar("CALDAV_SERVER_URL".to_string()))?;
|
||||
|
||||
let username = env::var("CALDAV_USERNAME")
|
||||
.map_err(|_| ConfigError::MissingVar("CALDAV_USERNAME".to_string()))?;
|
||||
|
||||
let password = env::var("CALDAV_PASSWORD")
|
||||
.map_err(|_| ConfigError::MissingVar("CALDAV_PASSWORD".to_string()))?;
|
||||
|
||||
// Optional paths - it's fine if these are not set
|
||||
let calendar_path = env::var("CALDAV_CALENDAR_PATH").ok();
|
||||
let tasks_path = env::var("CALDAV_TASKS_PATH").ok();
|
||||
|
||||
Ok(CalDAVConfig {
|
||||
server_url,
|
||||
username,
|
||||
password,
|
||||
calendar_path,
|
||||
tasks_path,
|
||||
})
|
||||
}
|
||||
|
||||
/// Generates a Base64-encoded string for HTTP Basic Authentication.
|
||||
///
|
||||
/// This method combines the username and password in the format
|
||||
/// `username:password` and encodes it using Base64, which is the
|
||||
/// standard format for the `Authorization: Basic` HTTP header.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A Base64-encoded string that can be used directly in the
|
||||
/// `Authorization` header: `Authorization: Basic <returned_value>`
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use crate::config::CalDAVConfig;
|
||||
///
|
||||
/// let config = CalDAVConfig {
|
||||
/// server_url: "https://example.com".to_string(),
|
||||
/// username: "user".to_string(),
|
||||
/// password: "pass".to_string(),
|
||||
/// calendar_path: None,
|
||||
/// tasks_path: None,
|
||||
/// };
|
||||
///
|
||||
/// let auth_value = config.get_basic_auth();
|
||||
/// let auth_header = format!("Basic {}", auth_value);
|
||||
/// ```
|
||||
pub fn get_basic_auth(&self) -> String {
|
||||
let credentials = format!("{}:{}", self.username, self.password);
|
||||
BASE64_STANDARD.encode(&credentials)
|
||||
}
|
||||
}
|
||||
|
||||
/// Errors that can occur when loading or using CalDAV configuration.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ConfigError {
|
||||
/// A required environment variable is missing or cannot be read.
|
||||
///
|
||||
/// This error occurs when calling `CalDAVConfig::from_env()` and one of the
|
||||
/// required environment variables (`CALDAV_SERVER_URL`, `CALDAV_USERNAME`,
|
||||
/// or `CALDAV_PASSWORD`) is not set.
|
||||
#[error("Missing environment variable: {0}")]
|
||||
MissingVar(String),
|
||||
|
||||
/// The configuration contains invalid or malformed values.
|
||||
///
|
||||
/// This could include malformed URLs, invalid authentication credentials,
|
||||
/// or other configuration issues that prevent proper CalDAV operation.
|
||||
#[error("Invalid configuration: {0}")]
|
||||
Invalid(String),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_basic_auth_encoding() {
|
||||
let config = CalDAVConfig {
|
||||
server_url: "https://example.com".to_string(),
|
||||
username: "testuser".to_string(),
|
||||
password: "testpass".to_string(),
|
||||
calendar_path: None,
|
||||
tasks_path: None,
|
||||
};
|
||||
|
||||
let auth = config.get_basic_auth();
|
||||
let expected = BASE64_STANDARD.encode("testuser:testpass");
|
||||
assert_eq!(auth, expected);
|
||||
}
|
||||
|
||||
/// Integration test that authenticates with the actual Baikal CalDAV server
|
||||
///
|
||||
/// This test requires a valid .env file with:
|
||||
/// - CALDAV_SERVER_URL
|
||||
/// - CALDAV_USERNAME
|
||||
/// - CALDAV_PASSWORD
|
||||
///
|
||||
/// Run with: `cargo test test_baikal_auth`
|
||||
#[tokio::test]
|
||||
async fn test_baikal_auth() {
|
||||
// Load config from .env
|
||||
let config = CalDAVConfig::from_env()
|
||||
.expect("Failed to load CalDAV config from environment");
|
||||
|
||||
println!("Testing authentication to: {}", config.server_url);
|
||||
|
||||
// Create HTTP client
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
// Make a simple OPTIONS request to test authentication
|
||||
let response = client
|
||||
.request(reqwest::Method::OPTIONS, &config.server_url)
|
||||
.header("Authorization", format!("Basic {}", config.get_basic_auth()))
|
||||
.header("User-Agent", "calendar-app/0.1.0")
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to send request to CalDAV server");
|
||||
|
||||
println!("Response status: {}", response.status());
|
||||
println!("Response headers: {:#?}", response.headers());
|
||||
|
||||
// Check if we got a successful response or at least not a 401 Unauthorized
|
||||
assert!(
|
||||
response.status().is_success() || response.status() != 401,
|
||||
"Authentication failed with status: {}. Check your credentials in .env",
|
||||
response.status()
|
||||
);
|
||||
|
||||
// For Baikal/CalDAV servers, we should see DAV headers
|
||||
assert!(
|
||||
response.headers().contains_key("dav") ||
|
||||
response.headers().contains_key("DAV") ||
|
||||
response.status().is_success(),
|
||||
"Server doesn't appear to be a CalDAV server - missing DAV headers"
|
||||
);
|
||||
|
||||
println!("✓ Authentication test passed!");
|
||||
}
|
||||
|
||||
/// Test making a PROPFIND request to discover calendars
|
||||
///
|
||||
/// This test requires a valid .env file and makes an actual CalDAV PROPFIND request
|
||||
///
|
||||
/// Run with: `cargo test test_propfind_calendars`
|
||||
#[tokio::test]
|
||||
async fn test_propfind_calendars() {
|
||||
let config = CalDAVConfig::from_env()
|
||||
.expect("Failed to load CalDAV config from environment");
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
// CalDAV PROPFIND request to discover calendars
|
||||
let propfind_body = r#"<?xml version="1.0" encoding="utf-8" ?>
|
||||
<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
||||
<d:prop>
|
||||
<d:resourcetype />
|
||||
<d:displayname />
|
||||
<c:calendar-description />
|
||||
<c:supported-calendar-component-set />
|
||||
</d:prop>
|
||||
</d:propfind>"#;
|
||||
|
||||
let response = client
|
||||
.request(reqwest::Method::from_bytes(b"PROPFIND").unwrap(), &config.server_url)
|
||||
.header("Authorization", format!("Basic {}", config.get_basic_auth()))
|
||||
.header("Content-Type", "application/xml")
|
||||
.header("Depth", "1")
|
||||
.header("User-Agent", "calendar-app/0.1.0")
|
||||
.body(propfind_body)
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to send PROPFIND request");
|
||||
|
||||
let status = response.status();
|
||||
println!("PROPFIND Response status: {}", status);
|
||||
|
||||
let body = response.text().await.expect("Failed to read response body");
|
||||
println!("PROPFIND Response body: {}", body);
|
||||
|
||||
// We should get a 207 Multi-Status for PROPFIND
|
||||
assert_eq!(
|
||||
status,
|
||||
reqwest::StatusCode::from_u16(207).unwrap(),
|
||||
"PROPFIND should return 207 Multi-Status"
|
||||
);
|
||||
|
||||
// The response should contain XML with calendar information
|
||||
assert!(body.contains("calendar"), "Response should contain calendar information");
|
||||
|
||||
println!("✓ PROPFIND calendars test passed!");
|
||||
}
|
||||
}
|
||||
12
frontend/src/main.rs
Normal file
12
frontend/src/main.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
mod app;
|
||||
mod auth;
|
||||
mod components;
|
||||
mod models;
|
||||
mod services;
|
||||
|
||||
use app::App;
|
||||
|
||||
fn main() {
|
||||
yew::Renderer::<App>::new().render();
|
||||
}
|
||||
841
frontend/src/models/ical.rs
Normal file
841
frontend/src/models/ical.rs
Normal file
@@ -0,0 +1,841 @@
|
||||
// RFC 5545 Compliant iCalendar Data Structures
|
||||
// This file contains updated structures that fully comply with RFC 5545 iCalendar specification
|
||||
|
||||
use chrono::{DateTime, Utc, Duration};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// ==================== CALENDAR OBJECT (VCALENDAR) ====================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct ICalendarObject {
|
||||
// Required calendar properties
|
||||
pub prodid: String, // Product identifier (PRODID)
|
||||
pub version: String, // Version (typically "2.0")
|
||||
|
||||
// Optional calendar properties
|
||||
pub calscale: Option<String>, // Calendar scale (CALSCALE) - default "GREGORIAN"
|
||||
pub method: Option<String>, // Method (METHOD)
|
||||
|
||||
// Components
|
||||
pub events: Vec<VEvent>, // VEVENT components
|
||||
pub todos: Vec<VTodo>, // VTODO components
|
||||
pub journals: Vec<VJournal>, // VJOURNAL components
|
||||
pub freebusys: Vec<VFreeBusy>, // VFREEBUSY components
|
||||
pub timezones: Vec<VTimeZone>, // VTIMEZONE components
|
||||
}
|
||||
|
||||
// ==================== VEVENT COMPONENT ====================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct VEvent {
|
||||
// Required properties
|
||||
pub dtstamp: DateTime<Utc>, // Date-time stamp (DTSTAMP) - REQUIRED
|
||||
pub uid: String, // Unique identifier (UID) - REQUIRED
|
||||
pub dtstart: DateTime<Utc>, // Start date-time (DTSTART) - REQUIRED
|
||||
|
||||
// Optional properties (commonly used)
|
||||
pub dtend: Option<DateTime<Utc>>, // End date-time (DTEND)
|
||||
pub duration: Option<Duration>, // Duration (DURATION) - alternative to DTEND
|
||||
pub summary: Option<String>, // Summary/title (SUMMARY)
|
||||
pub description: Option<String>, // Description (DESCRIPTION)
|
||||
pub location: Option<String>, // Location (LOCATION)
|
||||
|
||||
// Classification and status
|
||||
pub class: Option<EventClass>, // Classification (CLASS)
|
||||
pub status: Option<EventStatus>, // Status (STATUS)
|
||||
pub transp: Option<TimeTransparency>, // Time transparency (TRANSP)
|
||||
pub priority: Option<u8>, // Priority 0-9 (PRIORITY)
|
||||
|
||||
// People and organization
|
||||
pub organizer: Option<CalendarUser>, // Organizer (ORGANIZER)
|
||||
pub attendees: Vec<Attendee>, // Attendees (ATTENDEE)
|
||||
pub contact: Option<String>, // Contact information (CONTACT)
|
||||
|
||||
// Categorization and relationships
|
||||
pub categories: Vec<String>, // Categories (CATEGORIES)
|
||||
pub comment: Option<String>, // Comment (COMMENT)
|
||||
pub resources: Vec<String>, // Resources (RESOURCES)
|
||||
pub related_to: Option<String>, // Related component (RELATED-TO)
|
||||
pub url: Option<String>, // URL (URL)
|
||||
|
||||
// Geographical
|
||||
pub geo: Option<GeographicPosition>, // Geographic position (GEO)
|
||||
|
||||
// Versioning and modification
|
||||
pub sequence: Option<u32>, // Sequence number (SEQUENCE)
|
||||
pub created: Option<DateTime<Utc>>, // Creation time (CREATED)
|
||||
pub last_modified: Option<DateTime<Utc>>, // Last modified (LAST-MODIFIED)
|
||||
|
||||
// Recurrence
|
||||
pub rrule: Option<String>, // Recurrence rule (RRULE)
|
||||
pub rdate: Vec<DateTime<Utc>>, // Recurrence dates (RDATE)
|
||||
pub exdate: Vec<DateTime<Utc>>, // Exception dates (EXDATE)
|
||||
pub recurrence_id: Option<DateTime<Utc>>, // Recurrence ID (RECURRENCE-ID)
|
||||
|
||||
// Alarms and attachments
|
||||
pub alarms: Vec<VAlarm>, // VALARM components
|
||||
pub attachments: Vec<Attachment>, // Attachments (ATTACH)
|
||||
|
||||
// CalDAV specific (for implementation)
|
||||
pub etag: Option<String>, // ETag for CalDAV
|
||||
pub href: Option<String>, // Href for CalDAV
|
||||
pub calendar_path: Option<String>, // Calendar path
|
||||
pub all_day: bool, // All-day event flag
|
||||
}
|
||||
|
||||
// ==================== VTODO COMPONENT ====================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct VTodo {
|
||||
// Required properties
|
||||
pub dtstamp: DateTime<Utc>, // Date-time stamp (DTSTAMP) - REQUIRED
|
||||
pub uid: String, // Unique identifier (UID) - REQUIRED
|
||||
|
||||
// Optional date-time properties
|
||||
pub dtstart: Option<DateTime<Utc>>, // Start date-time (DTSTART)
|
||||
pub due: Option<DateTime<Utc>>, // Due date-time (DUE)
|
||||
pub duration: Option<Duration>, // Duration (DURATION)
|
||||
pub completed: Option<DateTime<Utc>>, // Completion date-time (COMPLETED)
|
||||
|
||||
// Descriptive properties
|
||||
pub summary: Option<String>, // Summary/title (SUMMARY)
|
||||
pub description: Option<String>, // Description (DESCRIPTION)
|
||||
pub location: Option<String>, // Location (LOCATION)
|
||||
|
||||
// Status and completion
|
||||
pub status: Option<TodoStatus>, // Status (STATUS)
|
||||
pub percent_complete: Option<u8>, // Percent complete 0-100 (PERCENT-COMPLETE)
|
||||
pub priority: Option<u8>, // Priority 0-9 (PRIORITY)
|
||||
|
||||
// People and organization
|
||||
pub organizer: Option<CalendarUser>, // Organizer (ORGANIZER)
|
||||
pub attendees: Vec<Attendee>, // Attendees (ATTENDEE)
|
||||
pub contact: Option<String>, // Contact information (CONTACT)
|
||||
|
||||
// Categorization and relationships
|
||||
pub categories: Vec<String>, // Categories (CATEGORIES)
|
||||
pub comment: Option<String>, // Comment (COMMENT)
|
||||
pub resources: Vec<String>, // Resources (RESOURCES)
|
||||
pub related_to: Option<String>, // Related component (RELATED-TO)
|
||||
pub url: Option<String>, // URL (URL)
|
||||
|
||||
// Geographical
|
||||
pub geo: Option<GeographicPosition>, // Geographic position (GEO)
|
||||
|
||||
// Versioning and modification
|
||||
pub sequence: Option<u32>, // Sequence number (SEQUENCE)
|
||||
pub created: Option<DateTime<Utc>>, // Creation time (CREATED)
|
||||
pub last_modified: Option<DateTime<Utc>>, // Last modified (LAST-MODIFIED)
|
||||
|
||||
// Recurrence
|
||||
pub rrule: Option<String>, // Recurrence rule (RRULE)
|
||||
pub rdate: Vec<DateTime<Utc>>, // Recurrence dates (RDATE)
|
||||
pub exdate: Vec<DateTime<Utc>>, // Exception dates (EXDATE)
|
||||
pub recurrence_id: Option<DateTime<Utc>>, // Recurrence ID (RECURRENCE-ID)
|
||||
|
||||
// Alarms and attachments
|
||||
pub alarms: Vec<VAlarm>, // VALARM components
|
||||
pub attachments: Vec<Attachment>, // Attachments (ATTACH)
|
||||
}
|
||||
|
||||
// ==================== VJOURNAL COMPONENT ====================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct VJournal {
|
||||
// Required properties
|
||||
pub dtstamp: DateTime<Utc>, // Date-time stamp (DTSTAMP) - REQUIRED
|
||||
pub uid: String, // Unique identifier (UID) - REQUIRED
|
||||
|
||||
// Optional properties
|
||||
pub dtstart: Option<DateTime<Utc>>, // Start date-time (DTSTART)
|
||||
pub summary: Option<String>, // Summary/title (SUMMARY)
|
||||
pub description: Option<String>, // Description (DESCRIPTION)
|
||||
|
||||
// Classification and status
|
||||
pub class: Option<EventClass>, // Classification (CLASS)
|
||||
pub status: Option<JournalStatus>, // Status (STATUS)
|
||||
|
||||
// People and organization
|
||||
pub organizer: Option<CalendarUser>, // Organizer (ORGANIZER)
|
||||
pub contact: Option<String>, // Contact information (CONTACT)
|
||||
|
||||
// Categorization and relationships
|
||||
pub categories: Vec<String>, // Categories (CATEGORIES)
|
||||
pub comment: Option<String>, // Comment (COMMENT)
|
||||
pub related_to: Option<String>, // Related component (RELATED-TO)
|
||||
pub url: Option<String>, // URL (URL)
|
||||
|
||||
// Versioning and modification
|
||||
pub sequence: Option<u32>, // Sequence number (SEQUENCE)
|
||||
pub created: Option<DateTime<Utc>>, // Creation time (CREATED)
|
||||
pub last_modified: Option<DateTime<Utc>>, // Last modified (LAST-MODIFIED)
|
||||
|
||||
// Recurrence
|
||||
pub rrule: Option<String>, // Recurrence rule (RRULE)
|
||||
pub rdate: Vec<DateTime<Utc>>, // Recurrence dates (RDATE)
|
||||
pub exdate: Vec<DateTime<Utc>>, // Exception dates (EXDATE)
|
||||
pub recurrence_id: Option<DateTime<Utc>>, // Recurrence ID (RECURRENCE-ID)
|
||||
|
||||
// Attachments
|
||||
pub attachments: Vec<Attachment>, // Attachments (ATTACH)
|
||||
}
|
||||
|
||||
// ==================== VFREEBUSY COMPONENT ====================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct VFreeBusy {
|
||||
// Required properties
|
||||
pub dtstamp: DateTime<Utc>, // Date-time stamp (DTSTAMP) - REQUIRED
|
||||
pub uid: String, // Unique identifier (UID) - REQUIRED
|
||||
|
||||
// Optional properties
|
||||
pub dtstart: Option<DateTime<Utc>>, // Start date-time (DTSTART)
|
||||
pub dtend: Option<DateTime<Utc>>, // End date-time (DTEND)
|
||||
pub duration: Option<Duration>, // Duration (DURATION)
|
||||
|
||||
// Free/busy information
|
||||
pub freebusy: Vec<FreeBusyTime>, // Free/busy periods (FREEBUSY)
|
||||
|
||||
// People and organization
|
||||
pub organizer: Option<CalendarUser>, // Organizer (ORGANIZER)
|
||||
pub attendees: Vec<CalendarUser>, // Attendees (ATTENDEE)
|
||||
pub contact: Option<String>, // Contact information (CONTACT)
|
||||
|
||||
// Additional properties
|
||||
pub comment: Option<String>, // Comment (COMMENT)
|
||||
pub url: Option<String>, // URL (URL)
|
||||
}
|
||||
|
||||
// ==================== VTIMEZONE COMPONENT ====================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct VTimeZone {
|
||||
// Required properties
|
||||
pub tzid: String, // Time zone identifier (TZID) - REQUIRED
|
||||
|
||||
// Optional properties
|
||||
pub tzname: Option<String>, // Time zone name (TZNAME)
|
||||
pub tzurl: Option<String>, // Time zone URL (TZURL)
|
||||
|
||||
// Standard and daylight components
|
||||
pub standard: Vec<TimeZoneComponent>, // Standard time components
|
||||
pub daylight: Vec<TimeZoneComponent>, // Daylight time components
|
||||
|
||||
// Last modified
|
||||
pub last_modified: Option<DateTime<Utc>>, // Last modified (LAST-MODIFIED)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct TimeZoneComponent {
|
||||
pub dtstart: DateTime<Utc>, // Start date-time (DTSTART) - REQUIRED
|
||||
pub tzoffsetfrom: String, // UTC offset from (TZOFFSETFROM) - REQUIRED
|
||||
pub tzoffsetto: String, // UTC offset to (TZOFFSETTO) - REQUIRED
|
||||
|
||||
pub tzname: Option<String>, // Time zone name (TZNAME)
|
||||
pub comment: Option<String>, // Comment (COMMENT)
|
||||
|
||||
// Recurrence
|
||||
pub rrule: Option<String>, // Recurrence rule (RRULE)
|
||||
pub rdate: Vec<DateTime<Utc>>, // Recurrence dates (RDATE)
|
||||
}
|
||||
|
||||
// ==================== VALARM COMPONENT ====================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct VAlarm {
|
||||
pub action: AlarmAction, // Action (ACTION) - REQUIRED
|
||||
pub trigger: AlarmTrigger, // Trigger (TRIGGER) - REQUIRED
|
||||
|
||||
// Optional properties (some required based on action)
|
||||
pub description: Option<String>, // Description (DESCRIPTION)
|
||||
pub summary: Option<String>, // Summary (SUMMARY)
|
||||
pub duration: Option<Duration>, // Duration (DURATION)
|
||||
pub repeat: Option<u32>, // Repeat count (REPEAT)
|
||||
pub attendees: Vec<CalendarUser>, // Attendees (ATTENDEE) - for EMAIL action
|
||||
pub attachments: Vec<Attachment>, // Attachments (ATTACH)
|
||||
}
|
||||
|
||||
// ==================== SUPPORTING TYPES ====================
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum EventClass {
|
||||
Public,
|
||||
Private,
|
||||
Confidential,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum EventStatus {
|
||||
Tentative,
|
||||
Confirmed,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum TodoStatus {
|
||||
NeedsAction,
|
||||
Completed,
|
||||
InProcess,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum JournalStatus {
|
||||
Draft,
|
||||
Final,
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum TimeTransparency {
|
||||
Opaque, // Time is not available (default)
|
||||
Transparent, // Time is available despite event
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum AlarmAction {
|
||||
Audio,
|
||||
Display,
|
||||
Email,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum AlarmTrigger {
|
||||
Duration(Duration), // Relative to start/end
|
||||
DateTime(DateTime<Utc>), // Absolute time
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct CalendarUser {
|
||||
pub cal_address: String, // Calendar address (email)
|
||||
pub cn: Option<String>, // Common name (CN parameter)
|
||||
pub dir: Option<String>, // Directory entry (DIR parameter)
|
||||
pub sent_by: Option<String>, // Sent by (SENT-BY parameter)
|
||||
pub language: Option<String>, // Language (LANGUAGE parameter)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct Attendee {
|
||||
pub cal_address: String, // Calendar address (email)
|
||||
pub cn: Option<String>, // Common name (CN parameter)
|
||||
pub role: Option<AttendeeRole>, // Role (ROLE parameter)
|
||||
pub partstat: Option<ParticipationStatus>, // Participation status (PARTSTAT parameter)
|
||||
pub rsvp: Option<bool>, // RSVP expectation (RSVP parameter)
|
||||
pub cutype: Option<CalendarUserType>, // Calendar user type (CUTYPE parameter)
|
||||
pub member: Vec<String>, // Group/list membership (MEMBER parameter)
|
||||
pub delegated_to: Vec<String>, // Delegated to (DELEGATED-TO parameter)
|
||||
pub delegated_from: Vec<String>, // Delegated from (DELEGATED-FROM parameter)
|
||||
pub sent_by: Option<String>, // Sent by (SENT-BY parameter)
|
||||
pub dir: Option<String>, // Directory entry (DIR parameter)
|
||||
pub language: Option<String>, // Language (LANGUAGE parameter)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum AttendeeRole {
|
||||
Chair,
|
||||
ReqParticipant,
|
||||
OptParticipant,
|
||||
NonParticipant,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum ParticipationStatus {
|
||||
NeedsAction,
|
||||
Accepted,
|
||||
Declined,
|
||||
Tentative,
|
||||
Delegated,
|
||||
Completed,
|
||||
InProcess,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum CalendarUserType {
|
||||
Individual,
|
||||
Group,
|
||||
Resource,
|
||||
Room,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct GeographicPosition {
|
||||
pub latitude: f64, // Latitude in decimal degrees
|
||||
pub longitude: f64, // Longitude in decimal degrees
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct Attachment {
|
||||
pub data: AttachmentData, // Attachment data
|
||||
pub fmttype: Option<String>, // Format type (FMTTYPE parameter)
|
||||
pub encoding: Option<String>, // Encoding (ENCODING parameter)
|
||||
pub filename: Option<String>, // Filename (X-FILENAME parameter)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum AttachmentData {
|
||||
Uri(String), // URI reference
|
||||
Binary(Vec<u8>), // Binary data
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct FreeBusyTime {
|
||||
pub period: (DateTime<Utc>, DateTime<Utc>), // Start and end time
|
||||
pub fbtype: Option<FreeBusyType>, // Free/busy type
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum FreeBusyType {
|
||||
Free,
|
||||
Busy,
|
||||
BusyUnavailable,
|
||||
BusyTentative,
|
||||
}
|
||||
|
||||
// ==================== COMPATIBILITY LAYER ====================
|
||||
|
||||
use crate::services::calendar_service::{CalendarEvent, EventReminder, ReminderAction};
|
||||
|
||||
// Conversion from new VEvent to existing CalendarEvent
|
||||
impl From<VEvent> for CalendarEvent {
|
||||
fn from(vevent: VEvent) -> Self {
|
||||
Self {
|
||||
uid: vevent.uid,
|
||||
summary: vevent.summary,
|
||||
description: vevent.description,
|
||||
start: vevent.dtstart,
|
||||
end: vevent.dtend,
|
||||
location: vevent.location,
|
||||
status: vevent.status.unwrap_or(EventStatus::Confirmed).into(),
|
||||
class: vevent.class.unwrap_or(EventClass::Public).into(),
|
||||
priority: vevent.priority,
|
||||
organizer: vevent.organizer.map(|o| o.cal_address),
|
||||
attendees: vevent.attendees.into_iter().map(|a| a.cal_address).collect(),
|
||||
categories: vevent.categories,
|
||||
created: vevent.created,
|
||||
last_modified: vevent.last_modified,
|
||||
recurrence_rule: vevent.rrule,
|
||||
exception_dates: vevent.exdate,
|
||||
all_day: vevent.all_day,
|
||||
reminders: vevent.alarms.into_iter().map(|a| a.into()).collect(),
|
||||
etag: vevent.etag,
|
||||
href: vevent.href,
|
||||
calendar_path: vevent.calendar_path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Conversion from existing CalendarEvent to new VEvent
|
||||
impl From<CalendarEvent> for VEvent {
|
||||
fn from(event: CalendarEvent) -> Self {
|
||||
use chrono::Utc;
|
||||
|
||||
Self {
|
||||
// Required properties
|
||||
dtstamp: Utc::now(), // Add required DTSTAMP
|
||||
uid: event.uid,
|
||||
dtstart: event.start,
|
||||
|
||||
// Optional properties
|
||||
dtend: event.end,
|
||||
duration: None, // Will be calculated from dtend if needed
|
||||
summary: event.summary,
|
||||
description: event.description,
|
||||
location: event.location,
|
||||
|
||||
// Classification and status
|
||||
class: Some(event.class.into()),
|
||||
status: Some(event.status.into()),
|
||||
transp: None, // Default to None, can be enhanced later
|
||||
priority: event.priority,
|
||||
|
||||
// People and organization
|
||||
organizer: event.organizer.map(|email| CalendarUser {
|
||||
cal_address: email,
|
||||
cn: None,
|
||||
dir: None,
|
||||
sent_by: None,
|
||||
language: None,
|
||||
}),
|
||||
attendees: event.attendees.into_iter().map(|email| Attendee {
|
||||
cal_address: email,
|
||||
cn: None,
|
||||
role: None,
|
||||
partstat: None,
|
||||
rsvp: None,
|
||||
cutype: None,
|
||||
member: Vec::new(),
|
||||
delegated_to: Vec::new(),
|
||||
delegated_from: Vec::new(),
|
||||
sent_by: None,
|
||||
dir: None,
|
||||
language: None,
|
||||
}).collect(),
|
||||
contact: None,
|
||||
|
||||
// Categorization and relationships
|
||||
categories: event.categories,
|
||||
comment: None,
|
||||
resources: Vec::new(),
|
||||
related_to: None,
|
||||
url: None,
|
||||
|
||||
// Geographical
|
||||
geo: None,
|
||||
|
||||
// Versioning and modification
|
||||
sequence: Some(0), // Start with sequence 0
|
||||
created: event.created,
|
||||
last_modified: event.last_modified,
|
||||
|
||||
// Recurrence
|
||||
rrule: event.recurrence_rule,
|
||||
rdate: Vec::new(),
|
||||
exdate: event.exception_dates,
|
||||
recurrence_id: None,
|
||||
|
||||
// Alarms and attachments
|
||||
alarms: event.reminders.into_iter().map(|r| r.into()).collect(),
|
||||
attachments: Vec::new(),
|
||||
|
||||
// CalDAV specific
|
||||
etag: event.etag,
|
||||
href: event.href,
|
||||
calendar_path: event.calendar_path,
|
||||
all_day: event.all_day,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert between status enums
|
||||
impl From<EventStatus> for crate::services::calendar_service::EventStatus {
|
||||
fn from(status: EventStatus) -> Self {
|
||||
match status {
|
||||
EventStatus::Tentative => crate::services::calendar_service::EventStatus::Tentative,
|
||||
EventStatus::Confirmed => crate::services::calendar_service::EventStatus::Confirmed,
|
||||
EventStatus::Cancelled => crate::services::calendar_service::EventStatus::Cancelled,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::services::calendar_service::EventStatus> for EventStatus {
|
||||
fn from(status: crate::services::calendar_service::EventStatus) -> Self {
|
||||
match status {
|
||||
crate::services::calendar_service::EventStatus::Tentative => EventStatus::Tentative,
|
||||
crate::services::calendar_service::EventStatus::Confirmed => EventStatus::Confirmed,
|
||||
crate::services::calendar_service::EventStatus::Cancelled => EventStatus::Cancelled,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert between class enums
|
||||
impl From<EventClass> for crate::services::calendar_service::EventClass {
|
||||
fn from(class: EventClass) -> Self {
|
||||
match class {
|
||||
EventClass::Public => crate::services::calendar_service::EventClass::Public,
|
||||
EventClass::Private => crate::services::calendar_service::EventClass::Private,
|
||||
EventClass::Confidential => crate::services::calendar_service::EventClass::Confidential,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::services::calendar_service::EventClass> for EventClass {
|
||||
fn from(class: crate::services::calendar_service::EventClass) -> Self {
|
||||
match class {
|
||||
crate::services::calendar_service::EventClass::Public => EventClass::Public,
|
||||
crate::services::calendar_service::EventClass::Private => EventClass::Private,
|
||||
crate::services::calendar_service::EventClass::Confidential => EventClass::Confidential,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert between reminder types
|
||||
impl From<VAlarm> for EventReminder {
|
||||
fn from(alarm: VAlarm) -> Self {
|
||||
let minutes_before = match alarm.trigger {
|
||||
AlarmTrigger::Duration(duration) => {
|
||||
// Convert duration to minutes (assuming it's negative for "before")
|
||||
(-duration.num_minutes()) as i32
|
||||
},
|
||||
AlarmTrigger::DateTime(_) => 0, // Absolute time alarms default to 0 minutes
|
||||
};
|
||||
|
||||
let action = match alarm.action {
|
||||
AlarmAction::Display => ReminderAction::Display,
|
||||
AlarmAction::Audio => ReminderAction::Audio,
|
||||
AlarmAction::Email => ReminderAction::Email,
|
||||
};
|
||||
|
||||
Self {
|
||||
minutes_before,
|
||||
action,
|
||||
description: alarm.description,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventReminder> for VAlarm {
|
||||
fn from(reminder: EventReminder) -> Self {
|
||||
use chrono::Duration;
|
||||
|
||||
let action = match reminder.action {
|
||||
ReminderAction::Display => AlarmAction::Display,
|
||||
ReminderAction::Audio => AlarmAction::Audio,
|
||||
ReminderAction::Email => AlarmAction::Email,
|
||||
};
|
||||
|
||||
let trigger = AlarmTrigger::Duration(Duration::minutes(-reminder.minutes_before as i64));
|
||||
|
||||
Self {
|
||||
action,
|
||||
trigger,
|
||||
description: reminder.description,
|
||||
summary: None,
|
||||
duration: None,
|
||||
repeat: None,
|
||||
attendees: Vec::new(),
|
||||
attachments: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== CONVERSION FUNCTIONS ====================
|
||||
|
||||
impl VEvent {
|
||||
/// Convert from legacy CalendarEvent to RFC 5545 compliant VEvent
|
||||
pub fn from_calendar_event(event: &crate::services::calendar_service::CalendarEvent) -> Self {
|
||||
let now = chrono::Utc::now();
|
||||
|
||||
// Convert attendees from strings to structured Attendee objects
|
||||
let attendees = event.attendees.iter()
|
||||
.map(|email| Attendee {
|
||||
cal_address: email.clone(),
|
||||
cn: None,
|
||||
role: Some(AttendeeRole::ReqParticipant),
|
||||
partstat: Some(ParticipationStatus::NeedsAction),
|
||||
rsvp: Some(true),
|
||||
cutype: Some(CalendarUserType::Individual),
|
||||
member: Vec::new(),
|
||||
delegated_to: Vec::new(),
|
||||
delegated_from: Vec::new(),
|
||||
sent_by: None,
|
||||
dir: None,
|
||||
language: None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Convert reminders to alarms
|
||||
let alarms = event.reminders.iter()
|
||||
.map(|reminder| {
|
||||
let action = match reminder.action {
|
||||
crate::services::calendar_service::ReminderAction::Display => AlarmAction::Display,
|
||||
crate::services::calendar_service::ReminderAction::Audio => AlarmAction::Audio,
|
||||
crate::services::calendar_service::ReminderAction::Email => AlarmAction::Email,
|
||||
};
|
||||
|
||||
VAlarm {
|
||||
action,
|
||||
trigger: AlarmTrigger::Duration(chrono::Duration::minutes(-reminder.minutes_before as i64)),
|
||||
description: reminder.description.clone(),
|
||||
summary: None,
|
||||
duration: None,
|
||||
repeat: None,
|
||||
attendees: Vec::new(),
|
||||
attachments: Vec::new(),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Convert status
|
||||
let status = match event.status {
|
||||
crate::services::calendar_service::EventStatus::Tentative => Some(EventStatus::Tentative),
|
||||
crate::services::calendar_service::EventStatus::Confirmed => Some(EventStatus::Confirmed),
|
||||
crate::services::calendar_service::EventStatus::Cancelled => Some(EventStatus::Cancelled),
|
||||
};
|
||||
|
||||
// Convert class
|
||||
let class = match event.class {
|
||||
crate::services::calendar_service::EventClass::Public => Some(EventClass::Public),
|
||||
crate::services::calendar_service::EventClass::Private => Some(EventClass::Private),
|
||||
crate::services::calendar_service::EventClass::Confidential => Some(EventClass::Confidential),
|
||||
};
|
||||
|
||||
Self {
|
||||
// Required properties
|
||||
dtstamp: event.last_modified.unwrap_or(now),
|
||||
uid: event.uid.clone(),
|
||||
dtstart: event.start,
|
||||
|
||||
// Date/time properties
|
||||
dtend: event.end,
|
||||
duration: None, // Will use dtend instead
|
||||
|
||||
// Content properties
|
||||
summary: event.summary.clone(),
|
||||
description: event.description.clone(),
|
||||
location: event.location.clone(),
|
||||
|
||||
// Classification and status
|
||||
class,
|
||||
status,
|
||||
transp: None, // Default transparency
|
||||
priority: event.priority,
|
||||
|
||||
// People and organization
|
||||
organizer: event.organizer.as_ref().map(|org| CalendarUser {
|
||||
cal_address: org.clone(),
|
||||
cn: None,
|
||||
dir: None,
|
||||
sent_by: None,
|
||||
language: None,
|
||||
}),
|
||||
attendees,
|
||||
contact: None,
|
||||
|
||||
// Categorization
|
||||
categories: event.categories.clone(),
|
||||
comment: None,
|
||||
resources: Vec::new(),
|
||||
related_to: None,
|
||||
url: None,
|
||||
|
||||
// Geographic
|
||||
geo: None,
|
||||
|
||||
// Versioning
|
||||
sequence: Some(0), // Start with sequence 0
|
||||
created: event.created,
|
||||
last_modified: event.last_modified,
|
||||
|
||||
// Recurrence
|
||||
rrule: event.recurrence_rule.clone(),
|
||||
rdate: Vec::new(),
|
||||
exdate: event.exception_dates.clone(),
|
||||
recurrence_id: None,
|
||||
|
||||
// Alarms and attachments
|
||||
alarms,
|
||||
attachments: Vec::new(),
|
||||
|
||||
// CalDAV specific
|
||||
etag: event.etag.clone(),
|
||||
href: event.href.clone(),
|
||||
calendar_path: event.calendar_path.clone(),
|
||||
all_day: event.all_day,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert to legacy CalendarEvent for backward compatibility
|
||||
#[allow(dead_code)]
|
||||
pub fn to_calendar_event(&self) -> crate::services::calendar_service::CalendarEvent {
|
||||
// Convert attendees back to simple email strings
|
||||
let attendees = self.attendees.iter()
|
||||
.map(|attendee| attendee.cal_address.clone())
|
||||
.collect();
|
||||
|
||||
// Convert alarms back to simple reminders
|
||||
let reminders = self.alarms.iter()
|
||||
.filter_map(|alarm| {
|
||||
if let AlarmTrigger::Duration(duration) = &alarm.trigger {
|
||||
Some(crate::services::calendar_service::EventReminder {
|
||||
minutes_before: (-duration.num_minutes()) as i32,
|
||||
action: match alarm.action {
|
||||
AlarmAction::Display => crate::services::calendar_service::ReminderAction::Display,
|
||||
AlarmAction::Audio => crate::services::calendar_service::ReminderAction::Audio,
|
||||
AlarmAction::Email => crate::services::calendar_service::ReminderAction::Email,
|
||||
},
|
||||
description: alarm.description.clone(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Convert status
|
||||
let status = match self.status {
|
||||
Some(EventStatus::Tentative) => crate::services::calendar_service::EventStatus::Tentative,
|
||||
Some(EventStatus::Cancelled) => crate::services::calendar_service::EventStatus::Cancelled,
|
||||
_ => crate::services::calendar_service::EventStatus::Confirmed,
|
||||
};
|
||||
|
||||
// Convert class
|
||||
let class = match self.class {
|
||||
Some(EventClass::Private) => crate::services::calendar_service::EventClass::Private,
|
||||
Some(EventClass::Confidential) => crate::services::calendar_service::EventClass::Confidential,
|
||||
_ => crate::services::calendar_service::EventClass::Public,
|
||||
};
|
||||
|
||||
crate::services::calendar_service::CalendarEvent {
|
||||
uid: self.uid.clone(),
|
||||
summary: self.summary.clone(),
|
||||
description: self.description.clone(),
|
||||
start: self.dtstart,
|
||||
end: self.dtend,
|
||||
location: self.location.clone(),
|
||||
status,
|
||||
class,
|
||||
priority: self.priority,
|
||||
organizer: self.organizer.as_ref().map(|org| org.cal_address.clone()),
|
||||
attendees,
|
||||
categories: self.categories.clone(),
|
||||
created: self.created,
|
||||
last_modified: self.last_modified,
|
||||
recurrence_rule: self.rrule.clone(),
|
||||
exception_dates: self.exdate.clone(),
|
||||
all_day: self.all_day,
|
||||
reminders,
|
||||
etag: self.etag.clone(),
|
||||
href: self.href.clone(),
|
||||
calendar_path: self.calendar_path.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the date for this event (for calendar display)
|
||||
pub fn get_date(&self) -> chrono::NaiveDate {
|
||||
if self.all_day {
|
||||
self.dtstart.date_naive()
|
||||
} else {
|
||||
self.dtstart.date_naive()
|
||||
}
|
||||
}
|
||||
|
||||
/// Get display title for the event
|
||||
pub fn get_title(&self) -> String {
|
||||
self.summary.clone().unwrap_or_else(|| "Untitled Event".to_string())
|
||||
}
|
||||
|
||||
/// Get display string for status
|
||||
pub fn get_status_display(&self) -> &'static str {
|
||||
match self.status {
|
||||
Some(EventStatus::Tentative) => "Tentative",
|
||||
Some(EventStatus::Confirmed) => "Confirmed",
|
||||
Some(EventStatus::Cancelled) => "Cancelled",
|
||||
None => "Confirmed", // Default
|
||||
}
|
||||
}
|
||||
|
||||
/// Get display string for class
|
||||
pub fn get_class_display(&self) -> &'static str {
|
||||
match self.class {
|
||||
Some(EventClass::Public) => "Public",
|
||||
Some(EventClass::Private) => "Private",
|
||||
Some(EventClass::Confidential) => "Confidential",
|
||||
None => "Public", // Default
|
||||
}
|
||||
}
|
||||
|
||||
/// Get display string for priority
|
||||
pub fn get_priority_display(&self) -> String {
|
||||
match self.priority {
|
||||
None => "Not set".to_string(),
|
||||
Some(0) => "Undefined".to_string(),
|
||||
Some(1) => "High".to_string(),
|
||||
Some(p) if p <= 4 => "High".to_string(),
|
||||
Some(5) => "Medium".to_string(),
|
||||
Some(p) if p <= 8 => "Low".to_string(),
|
||||
Some(9) => "Low".to_string(),
|
||||
Some(p) => format!("Priority {}", p),
|
||||
}
|
||||
}
|
||||
}
|
||||
5
frontend/src/models/mod.rs
Normal file
5
frontend/src/models/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// RFC 5545 Compliant iCalendar Models
|
||||
pub mod ical;
|
||||
|
||||
// Re-export commonly used types
|
||||
// pub use ical::VEvent;
|
||||
1005
frontend/src/services/calendar_service.rs
Normal file
1005
frontend/src/services/calendar_service.rs
Normal file
File diff suppressed because it is too large
Load Diff
3
frontend/src/services/mod.rs
Normal file
3
frontend/src/services/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod calendar_service;
|
||||
|
||||
pub use calendar_service::CalendarService;
|
||||
2273
frontend/styles.css
Normal file
2273
frontend/styles.css
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user