Skip to main content

dioxus_leaflet/components/
polygon.rs

1use dioxus::{
2    core::{spawn_forever, use_drop},
3    prelude::*,
4};
5use dioxus_logger::tracing::error;
6use std::rc::Rc;
7
8use crate::{LatLng, PathOptions, interop, types::Id};
9
10#[component]
11pub fn Polygon(
12    coordinates: ReadSignal<Vec<Vec<Vec<LatLng>>>>,
13
14    #[props(default = PathOptions::default())] options: ReadSignal<PathOptions>,
15
16    children: Element,
17) -> Element {
18    let id: Rc<Id> = use_context();
19    let id = use_context_provider(|| Rc::new(Id::polygon(&id, dioxus_core::current_scope_id().0)));
20
21    let id2 = id.clone();
22    use_effect(move || {
23        let id = id2.clone();
24        let coords = coordinates();
25        let opts = options();
26        spawn(async move {
27            if let Err(e) = interop::update_polygon(&id, &coords, &opts).await {
28                error!("{e}");
29            }
30        });
31    });
32
33    let id2 = id.clone();
34    use_drop(move || {
35        let id = id2.clone();
36        spawn_forever(async move {
37            if let Err(e) = interop::delete_polygon(&id).await {
38                error!("{e}");
39            }
40        });
41    });
42
43    rsx!({ children })
44}