use std::rc::Rc; use leptos::leptos_dom::*; use leptos::*; use leptos_icons::*; use leptos_router::Form; use web_sys::Response; use crate::search::search_artists; use crate::search::search_albums; use crate::models::Artist; use crate::models::Album; #[component] pub fn UploadBtn(dialog_open: RwSignal) -> impl IntoView { let open_dialog = move |_| { dialog_open.set(true); }; view! { } } #[component] pub fn Upload(open: RwSignal) -> impl IntoView { // Create signals for the artist input and the filtered artists let (artists, set_artists) = create_signal("".to_string()); let (filtered_artists, set_filtered_artists) = create_signal(vec![]); let (albums, set_albums) = create_signal("".to_string()); let (filtered_albums, set_filtered_albums) = create_signal(vec![]); let (error_msg, set_error_msg) = create_signal::>(Some("Error uploading song".to_string())); let close_dialog = move |ev: leptos::ev::MouseEvent| { ev.prevent_default(); open.set(false); }; // Create a filter function to handle filtering artists // Allow users to search for artists by name, converts the artist name to artist id to be handed off to backend let handle_filter_artists = move |ev: leptos::ev::Event| { ev.prevent_default(); let artist_input: String = event_target_value(&ev); //Get the artist that we are currently searching for let mut all_artists: Vec<&str> = artist_input.split(",").collect(); let search = all_artists.pop().unwrap().to_string(); //Update the artist signal with the input set_artists.update(|value: &mut String| *value = artist_input); spawn_local(async move { let filter_results = search_artists(search, 3).await; if let Err(err) = filter_results { log!("Error filtering artists: {:?}", err); } else if let Ok(artists) = filter_results { log!("Filtered artists: {:?}", artists); set_filtered_artists.update(|value| *value = artists); } }) }; // Create a filter function to handle filtering albums // Allow users to search for albums by title, converts the album title to album id to be handed off to backend let handle_filter_albums = move |ev: leptos::ev::Event| { ev.prevent_default(); let album_input: String = event_target_value(&ev); //Update the album signal with the input set_albums.update(|value: &mut String| *value = album_input); spawn_local(async move { let filter_results = search_albums(albums.get_untracked(), 3).await; if let Err(err) = filter_results { log!("Error filtering albums: {:?}", err); } else if let Ok(albums) = filter_results { log!("Filtered albums: {:?}", albums); set_filtered_albums.update(|value| *value = albums); } }) }; let handle_response = Rc::new(move |response: &Response| { if response.ok() { set_error_msg.update(|value| *value = None); open.set(false); } else { // TODO: Extract error message from response set_error_msg.update(|value| *value = Some("Error uploading song".to_string())); } }); view! {

Upload Song

Title
Track Number
Release Date
File
{error_msg.get().as_ref().unwrap()}
} } #[component] pub fn Artist(artist: Artist, artists: ReadSignal, set_artists: WriteSignal, set_filtered: WriteSignal>) -> impl IntoView { // Converts artist name to artist id and adds it to the artist input let add_artist = move |_| { //Create an empty string to hold previous artist ids let mut s: String = String::from(""); //Get the current artist input let all_artirts: String = artists.get(); //Split the input into a vector of artists separated by commas let mut ids: Vec<&str> = all_artirts.split(",").collect(); //If there is only one artist in the input, get their id equivalent and add it to the string if ids.len() == 1 { let value_str = match artist.id.clone() { Some(v) => v.to_string(), None => String::from("None"), }; s.push_str(&value_str); s.push_str(","); set_artists.update(|value| *value = s); //If there are multiple artists in the input, pop the last artist by string off the vector, //get their id equivalent, and add it to the string } else { ids.pop(); for id in ids { s.push_str(id); s.push_str(","); } let value_str = match artist.id.clone() { Some(v) => v.to_string(), None => String::from("None"), }; s.push_str(&value_str); s.push_str(","); set_artists.update(|value| *value = s); } //Clear the search results set_filtered.update(|value| *value = vec![]); }; view! {
{artist.name.clone()}
} } #[component] pub fn Album(album: Album, _albums: ReadSignal, set_albums: WriteSignal, set_filtered: WriteSignal>) -> impl IntoView { //Converts album title to album id to upload a song let add_album = move |_| { let value_str = match album.id.clone() { Some(v) => v.to_string(), None => String::from("None"), }; set_albums.update(|value| *value = value_str); set_filtered.update(|value| *value = vec![]); }; view! {
{album.title.clone()}
} }