Implement complete calendar creation functionality
Add full end-to-end calendar creation feature including: - Create Calendar button in sidebar footer - Modal form with name, description, and color picker (16 predefined colors in 4x4 grid) - Form validation and error handling with loading states - Backend API endpoint for calendar creation with authentication - CalDAV MKCALENDAR protocol implementation with proper XML generation - Real-time calendar list refresh after successful creation - Responsive design for mobile and desktop 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -590,6 +590,74 @@ impl CalDAVClient {
|
||||
|
||||
Err(CalDAVError::ParseError(format!("Unable to parse datetime: {}", datetime_str)))
|
||||
}
|
||||
|
||||
/// Create a new calendar on the CalDAV server using MKCALENDAR
|
||||
pub async fn create_calendar(&self, name: &str, description: Option<&str>, color: Option<&str>) -> Result<(), CalDAVError> {
|
||||
// Sanitize calendar name for URL path
|
||||
let calendar_id = name
|
||||
.chars()
|
||||
.map(|c| if c.is_alphanumeric() { c } else { '-' })
|
||||
.collect::<String>()
|
||||
.to_lowercase();
|
||||
|
||||
let calendar_path = format!("/calendars/{}/{}/", self.config.username, calendar_id);
|
||||
let full_url = format!("{}{}", self.config.server_url.trim_end_matches('/'), calendar_path);
|
||||
|
||||
// Build color property if provided
|
||||
let color_property = if let Some(color) = color {
|
||||
format!(r#"<ic:calendar-color xmlns:ic="http://apple.com/ns/ical/">{}</ic:calendar-color>"#, color)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
let description_property = if let Some(desc) = description {
|
||||
format!(r#"<c:calendar-description xmlns:c="urn:ietf:params:xml:ns:caldav">{}</c:calendar-description>"#, desc)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
// Create the MKCALENDAR request body
|
||||
let mkcalendar_body = format!(
|
||||
r#"<?xml version="1.0" encoding="utf-8" ?>
|
||||
<c:mkcalendar xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:ic="http://apple.com/ns/ical/">
|
||||
<d:set>
|
||||
<d:prop>
|
||||
<d:displayname>{}</d:displayname>
|
||||
<c:supported-calendar-component-set>
|
||||
<c:comp name="VEVENT"/>
|
||||
</c:supported-calendar-component-set>
|
||||
{}
|
||||
{}
|
||||
</d:prop>
|
||||
</d:set>
|
||||
</c:mkcalendar>"#,
|
||||
name, color_property, description_property
|
||||
);
|
||||
|
||||
println!("Creating calendar at: {}", full_url);
|
||||
println!("MKCALENDAR body: {}", mkcalendar_body);
|
||||
|
||||
let response = self.http_client
|
||||
.request(reqwest::Method::from_bytes(b"MKCALENDAR").unwrap(), &full_url)
|
||||
.header("Content-Type", "application/xml; charset=utf-8")
|
||||
.header("Authorization", format!("Basic {}", self.config.get_basic_auth()))
|
||||
.body(mkcalendar_body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| CalDAVError::ParseError(e.to_string()))?;
|
||||
|
||||
println!("Calendar creation response status: {}", response.status());
|
||||
|
||||
if response.status().is_success() {
|
||||
println!("✅ Calendar created successfully at {}", calendar_path);
|
||||
Ok(())
|
||||
} else {
|
||||
let status = response.status();
|
||||
let error_body = response.text().await.unwrap_or_default();
|
||||
println!("❌ Calendar creation failed: {} - {}", status, error_body);
|
||||
Err(CalDAVError::ServerError(status.as_u16()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper struct for extracting calendar data from XML responses
|
||||
@@ -688,7 +756,9 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Test parsing a sample iCal event
|
||||
}
|
||||
|
||||
/// Test parsing a sample iCal event
|
||||
#[test]
|
||||
fn test_parse_ical_event() {
|
||||
let sample_ical = r#"BEGIN:VCALENDAR
|
||||
@@ -780,4 +850,3 @@ END:VCALENDAR"#;
|
||||
|
||||
println!("✓ Event enum tests passed!");
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
use chrono::Datelike;
|
||||
|
||||
use crate::{AppState, models::{CalDAVLoginRequest, AuthResponse, ApiError, UserInfo, CalendarInfo}};
|
||||
use crate::{AppState, models::{CalDAVLoginRequest, AuthResponse, ApiError, UserInfo, CalendarInfo, CreateCalendarRequest, CreateCalendarResponse}};
|
||||
use crate::calendar::{CalDAVClient, CalendarEvent};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -252,4 +252,44 @@ fn extract_password_header(headers: &HeaderMap) -> Result<String, ApiError> {
|
||||
} else {
|
||||
Err(ApiError::BadRequest("X-CalDAV-Password header required".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_calendar(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Json(request): Json<CreateCalendarRequest>,
|
||||
) -> Result<Json<CreateCalendarResponse>, ApiError> {
|
||||
println!("📝 Create calendar request received: name='{}', description={:?}, color={:?}",
|
||||
request.name, request.description, request.color);
|
||||
|
||||
// Extract and verify token
|
||||
let token = extract_bearer_token(&headers)?;
|
||||
let password = extract_password_header(&headers)?;
|
||||
|
||||
// Validate request
|
||||
if request.name.trim().is_empty() {
|
||||
return Err(ApiError::BadRequest("Calendar name is required".to_string()));
|
||||
}
|
||||
|
||||
if request.name.len() > 100 {
|
||||
return Err(ApiError::BadRequest("Calendar name too long (max 100 characters)".to_string()));
|
||||
}
|
||||
|
||||
// Create CalDAV config from token and password
|
||||
let config = state.auth_service.caldav_config_from_token(&token, &password)?;
|
||||
let client = CalDAVClient::new(config);
|
||||
|
||||
// Create the calendar
|
||||
client.create_calendar(
|
||||
&request.name,
|
||||
request.description.as_deref(),
|
||||
request.color.as_deref()
|
||||
)
|
||||
.await
|
||||
.map_err(|e| ApiError::Internal(format!("Failed to create calendar: {}", e)))?;
|
||||
|
||||
Ok(Json(CreateCalendarResponse {
|
||||
success: true,
|
||||
message: "Calendar created successfully".to_string(),
|
||||
}))
|
||||
}
|
||||
@@ -38,6 +38,7 @@ pub async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.route("/api/auth/login", post(handlers::login))
|
||||
.route("/api/auth/verify", get(handlers::verify_token))
|
||||
.route("/api/user/info", get(handlers::get_user_info))
|
||||
.route("/api/calendar/create", post(handlers::create_calendar))
|
||||
.route("/api/calendar/events", get(handlers::get_calendar_events))
|
||||
.route("/api/calendar/events/:uid", get(handlers::refresh_event))
|
||||
.layer(
|
||||
|
||||
@@ -34,6 +34,19 @@ pub struct CalendarInfo {
|
||||
pub color: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct CreateCalendarRequest {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub color: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct CreateCalendarResponse {
|
||||
pub success: bool,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
// Error handling
|
||||
#[derive(Debug)]
|
||||
pub enum ApiError {
|
||||
|
||||
Reference in New Issue
Block a user