feat: build out api request to run on load properly
This also has the option of refreshing, since it's made with a Resource struct.
This commit is contained in:
1 parent
40f12180e5
commit
e0b44c639c
2 files changed
+27
-17
No files matched your search
+23
-15
@@ -1,21 +1,23 @@
|
||||
use crate::components::{ActionButton, OutlinedTextField};
|
||||
use leptos::prelude::*;
|
||||
use leptos::leptos_dom::logging::console_log;
|
||||
use leptos::task::spawn_local;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct StudentSubmission {
|
||||
testing_value: Option<String>,
|
||||
first_name: String,
|
||||
last_name: String
|
||||
}
|
||||
|
||||
#[server]
|
||||
#[server(CreateSampleSubmission)]
|
||||
pub async fn create_sample_submission() -> Result<StudentSubmission, ServerFnError> {
|
||||
use aws_sdk_dynamodb::{types::AttributeValue, Client};
|
||||
|
||||
let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
|
||||
let dbclient = Client::new(&config);
|
||||
|
||||
console_log("Getting values from API");
|
||||
|
||||
match dbclient
|
||||
.get_item()
|
||||
.table_name("student-applications")
|
||||
@@ -25,12 +27,12 @@ pub async fn create_sample_submission() -> Result<StudentSubmission, ServerFnErr
|
||||
{
|
||||
Ok(output) => {
|
||||
println!("Found submission with passed key.");
|
||||
let value = output.item
|
||||
.unwrap()
|
||||
.get("studentFirstName").unwrap()
|
||||
.as_s().unwrap().clone();
|
||||
let item = output.item.unwrap();
|
||||
let first_name = item.get("studentFirstName").unwrap().as_s().unwrap().clone();
|
||||
let last_name = item.get("studentLastName").unwrap().as_s().unwrap().clone();
|
||||
Ok(StudentSubmission {
|
||||
testing_value: Some(value),
|
||||
first_name: first_name,
|
||||
last_name: last_name
|
||||
})
|
||||
}
|
||||
Err(e) => Err(ServerFnError::new("Couldn't find a submission with that ID."))
|
||||
@@ -42,20 +44,26 @@ pub fn HomePage() -> impl IntoView {
|
||||
// Creates a reactive value to update the button
|
||||
let count = RwSignal::new(0);
|
||||
let elements_disabled = RwSignal::new(false);
|
||||
let server_resource = Resource::new(|| {}, |_| async {
|
||||
create_sample_submission().await.unwrap()
|
||||
});
|
||||
let on_click = move |_| {
|
||||
*count.write() += 1;
|
||||
*elements_disabled.write() = count.get() == 10;
|
||||
spawn_local(async {
|
||||
let response = create_sample_submission().await;
|
||||
match response {
|
||||
Ok(submission) => console_log(submission.testing_value.unwrap_or("Default value.".to_string()).as_str()),
|
||||
Err(_) => console_log("Failed to get correct information.")
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
view! {
|
||||
<h1>"Welcome to Leptos!"</h1>
|
||||
<p>
|
||||
"Lillian's reported full name from the API: "
|
||||
<Suspense fallback=move || view! { <span>"Loading..."</span> }>
|
||||
{move || {
|
||||
server_resource.get().map(|submission| {
|
||||
view! { <span>{submission.first_name}" "{submission.last_name}</span> }
|
||||
})
|
||||
}}
|
||||
</Suspense>
|
||||
</p>
|
||||
<OutlinedTextField
|
||||
placeholder="Testing...".to_string()
|
||||
disabled={elements_disabled} />
|
||||
|
||||
Reference in new issue
Block a user