add: movie input, refactor things, and fix vscode config
This commit is contained in:
52
src/app/movies.rs
Normal file
52
src/app/movies.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use leptos::prelude::*;
|
||||
|
||||
use crate::model::Movie;
|
||||
|
||||
#[server]
|
||||
pub async fn load_movies() -> Result<Vec<Movie>, ServerFnError> {
|
||||
use crate::common::Context;
|
||||
let data = use_context::<Context>().unwrap();
|
||||
let movies = data.movies.lock().await.clone();
|
||||
Ok(movies)
|
||||
}
|
||||
|
||||
#[server]
|
||||
pub async fn add_movie(name: String) -> Result<(), ServerFnError> {
|
||||
use crate::common::Context;
|
||||
let data = use_context::<Context>().unwrap();
|
||||
let mut data = data.movies.lock().await;
|
||||
data.push(Movie::new(&name));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Renders the home page of your application.
|
||||
#[component]
|
||||
pub fn Movies() -> impl IntoView {
|
||||
let add_movie = ServerMultiAction::<AddMovie>::new();
|
||||
let movies_resource =
|
||||
Resource::new(move || (add_movie.version().get(),), move |_| load_movies());
|
||||
let movies = move || {
|
||||
Suspend::new(async move {
|
||||
let movies = movies_resource.await.unwrap();
|
||||
view! {
|
||||
<ul>
|
||||
{movies.into_iter().map(move |movie| {
|
||||
view! {
|
||||
<li>{movie.name}</li>
|
||||
}
|
||||
}).collect::<Vec<_>>()}
|
||||
</ul>
|
||||
}
|
||||
})
|
||||
};
|
||||
view! {
|
||||
<h2>"Movies"</h2>
|
||||
<MultiActionForm action=add_movie>
|
||||
<label>"Add a movie" <input type="text" name="name"/></label>
|
||||
<input type="submit" value="Add"/>
|
||||
</MultiActionForm>
|
||||
<Transition fallback=move || view! { <p>"Loading..."</p> }>
|
||||
{movies}
|
||||
</Transition>
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user