diff --git a/src/api/playlists.rs b/src/api/playlists.rs index fe2d0f8..dc247bd 100644 --- a/src/api/playlists.rs +++ b/src/api/playlists.rs @@ -1,4 +1,5 @@ use leptos::*; +use crate::models::Playlist; use cfg_if::cfg_if; @@ -26,7 +27,6 @@ cfg_if! { pub async fn create_playlist(playlist_name: String)->Result<(), ServerFnError> { use crate::schema::playlists::dsl::*; use leptos::server_fn::error::NoCustomError; - use crate::models::Playlist; let auth_session = extract::>().await .map_err(|e| ServerFnError::::ServerError(format!("Error getting auth session: {}", e)))?; @@ -47,3 +47,27 @@ pub async fn create_playlist(playlist_name: String)->Result<(), ServerFnError> { Ok(()) } +/// Get all playlists for the current user +/// +/// # Returns +/// +/// * `Result, ServerFnError>` - A vector of playlists if successful, or an error +/// +#[server(endpoint = "playlists/get-playlists")] +pub async fn get_playlists() -> Result, ServerFnError> { + use crate::schema::playlists::dsl::*; + use leptos::server_fn::error::NoCustomError; + + let auth_session = extract::>().await + .map_err(|e| ServerFnError::::ServerError(format!("Error getting auth session: {}", e)))?; + + let other_user_id = auth_session.user.unwrap().id.expect("User has no id"); + + let db_con = &mut get_db_conn(); + let results = playlists + .filter(user_id.eq(other_user_id)) + .load::(db_con) + .map_err(|e| ServerFnError::::ServerError(format!("Error getting playlists: {}", e)))?; + + Ok(results) +} diff --git a/src/app.rs b/src/app.rs index 2d78867..5cbe864 100644 --- a/src/app.rs +++ b/src/app.rs @@ -71,6 +71,7 @@ fn HomePage() -> impl IntoView { log!("User is not logged in"); set_logged_in.update(|value| *value = false); } + }); }); diff --git a/src/components/sidebar.rs b/src/components/sidebar.rs index adb2fab..01fd776 100644 --- a/src/components/sidebar.rs +++ b/src/components/sidebar.rs @@ -2,6 +2,7 @@ use leptos::leptos_dom::*; use leptos::*; use leptos_icons::*; use crate::api::playlists::create_playlist; +use crate::api::playlists::get_playlists; #[component] pub fn Sidebar(setter: WriteSignal, active: ReadSignal) -> impl IntoView { @@ -36,7 +37,21 @@ pub fn Sidebar(setter: WriteSignal, active: ReadSignal) -> impl Into #[component] pub fn Bottom() -> impl IntoView { let (create_playlist_open, set_create_playlist_open) = create_signal(false); + let (playlists, set_playlists) = create_signal(vec![]); + create_effect(move |_| { + spawn_local(async move { + let playlists = get_playlists().await; + if let Err(err) = playlists { + // Handle the error here, e.g., log it or display to the user + log!("Error getting playlists: {:?}", err); + } else { + log!("Playlists: {:?}", playlists); + set_playlists.update(|value| *value = playlists.unwrap()); + } + }) + }); + view! { } } @@ -60,7 +85,6 @@ pub fn CreatePlayList(opened: ReadSignal,closer: WriteSignal) -> imp let on_submit = move |ev: leptos::ev::SubmitEvent| { ev.prevent_default(); - let new_playlist_name = playlist_name.get(); spawn_local(async move { let create_result = create_playlist(new_playlist_name).await; @@ -69,9 +93,9 @@ pub fn CreatePlayList(opened: ReadSignal,closer: WriteSignal) -> imp log!("Error creating playlist: {:?}", err); } else { log!("Playlist created successfully!"); + closer.update(|value| *value = false); } }) - }; view! { diff --git a/style/sidebar.scss b/style/sidebar.scss index 448acd3..66a8d56 100644 --- a/style/sidebar.scss +++ b/style/sidebar.scss @@ -154,4 +154,33 @@ } } } + .playlists { + list-style: none; + padding: 0; + margin: 0; + margin-top: 5px; + height: 100%; + + .playlist { + padding: 0; + margin: 0; + margin-top: 5px; + display: flex; + flex-direction: row; + border: 1px solid grey; + height: 3rem; + border-radius: 5px; + cursor: pointer; + transition: background-color 0.3s ease; + .name { + font-size: 1rem; + margin-left: 1rem; + margin-top: 0.5rem; + color: white; + } + } + .playlist:hover { + background-color: #adadad36; + } + } }