diff --git a/frontend/src/components/layout/sidebar.rs b/frontend/src/components/layout/sidebar.rs index 4e70d92..daee297 100644 --- a/frontend/src/components/layout/sidebar.rs +++ b/frontend/src/components/layout/sidebar.rs @@ -146,48 +146,50 @@ pub fn Sidebar() -> impl IntoView { - - // Push Notification Toggle -
-
- "Bildirimler" - "Web Push" + +
+ // Push Notification Toggle +
+
+ "Bildirimler" + "Web Push" +
+
- -
-
-
- {first_letter} -
-
-
{username}
-
"Yönetici"
-
- -
- +
+
+ {first_letter} +
+
+
{username}
+
"Yönetici"
+
- +
+ + + +
diff --git a/frontend/src/store.rs b/frontend/src/store.rs index 8618736..8edb028 100644 --- a/frontend/src/store.rs +++ b/frontend/src/store.rs @@ -162,7 +162,7 @@ pub async fn is_push_subscribed() -> Result { .dyn_into::() .map_err(|_| "not a registration")?; - let push_manager = registration.push_manager(); + let push_manager = registration.push_manager().map_err(|e| format!("{:?}", e))?; let subscription = wasm_bindgen_futures::JsFuture::from(push_manager.get_subscription().map_err(|e| format!("{:?}", e))?) .await .map_err(|e| format!("{:?}", e))?; @@ -181,7 +181,8 @@ pub async fn subscribe_to_push_notifications() { }; // 1. Get Public Key from Backend - let public_key = match shared::server_fns::push::get_push_public_key().await { + let public_key_res: Result = shared::server_fns::push::get_public_key().await; + let public_key = match public_key_res { Ok(key) => key, Err(e) => { log::error!("Failed to get public key: {:?}", e); return; } }; @@ -192,25 +193,25 @@ pub async fn subscribe_to_push_notifications() { // 3. Prepare Options let mut options = web_sys::PushSubscriptionOptionsInit::new(); - options.user_visible_only(true); - options.application_server_key(Some(&key_array.into())); + options.set_user_visible_only(true); + options.set_application_server_key(&key_array.into()); // 4. Subscribe - let push_manager = registration.push_manager(); + let push_manager = registration.push_manager().expect("no push manager"); match wasm_bindgen_futures::JsFuture::from(push_manager.subscribe_with_options(&options).expect("subscribe failed")).await { Ok(subscription) => { - let sub = subscription.dyn_into::().expect("not a sub"); - let json = sub.to_json().expect("sub to json failed"); + let sub_js = subscription.clone(); - // Extract keys from JSON - let sub_obj: serde_json::Value = serde_wasm_bindgen::from_value(json).expect("serde from value failed"); + // Use JS to extract JSON string representation + let json_str = js_sys::JSON::stringify(&sub_js).expect("stringify failed").as_string().expect("not a string"); + let sub_obj: serde_json::Value = serde_json::from_str(&json_str).expect("serde from str failed"); let endpoint = sub_obj["endpoint"].as_str().expect("no endpoint").to_string(); let p256dh = sub_obj["keys"]["p256dh"].as_str().expect("no p256dh").to_string(); let auth = sub_obj["keys"]["auth"].as_str().expect("no auth").to_string(); // 5. Save to Backend - match shared::server_fns::push::save_push_subscription(endpoint, p256dh, auth).await { + match shared::server_fns::push::subscribe_push(endpoint, p256dh, auth).await { Ok(_) => { log::info!("Push subscription saved successfully"); toast_success("Bildirimler aktif edildi"); @@ -229,7 +230,7 @@ pub async fn unsubscribe_from_push_notifications() { let registration = wasm_bindgen_futures::JsFuture::from(sw_container.ready().expect("sw not ready")).await .unwrap().dyn_into::().unwrap(); - let push_manager = registration.push_manager(); + let push_manager = registration.push_manager().unwrap(); if let Ok(sub_future) = push_manager.get_subscription() { if let Ok(subscription) = wasm_bindgen_futures::JsFuture::from(sub_future).await { if !subscription.is_null() { @@ -240,7 +241,7 @@ pub async fn unsubscribe_from_push_notifications() { let _ = wasm_bindgen_futures::JsFuture::from(sub.unsubscribe().unwrap()).await; // 2. Remove from Backend - let _ = shared::server_fns::push::remove_push_subscription(endpoint).await; + let _ = shared::server_fns::push::unsubscribe_push(endpoint).await; log::info!("Push subscription removed"); show_toast(NotificationLevel::Info, "Bildirimler kapatıldı"); } diff --git a/shared/src/server_fns/push.rs b/shared/src/server_fns/push.rs index 72db4a7..fc5d4b0 100644 --- a/shared/src/server_fns/push.rs +++ b/shared/src/server_fns/push.rs @@ -20,3 +20,13 @@ pub async fn subscribe_push( .await .map_err(|e| ServerFnError::new(format!("Failed to save subscription: {}", e))) } + +#[server(UnsubscribePush, "/api/server_fns")] +pub async fn unsubscribe_push(endpoint: String) -> Result<(), ServerFnError> { + let db_ctx = expect_context::(); + db_ctx + .db + .remove_push_subscription(&endpoint) + .await + .map_err(|e| ServerFnError::new(format!("Failed to remove subscription: {}", e))) +}