feat: create new API that updates a submission
This commit is contained in:
1 parent
e0b44c639c
commit
39db7d80e3
2 files changed
+102
-23
No files matched your search
@@ -5,8 +5,12 @@ pub fn OutlinedTextField(
|
|||||||
#[prop(default = String::from(""))] placeholder: String,
|
#[prop(default = String::from(""))] placeholder: String,
|
||||||
#[prop(default = RwSignal::new("".to_string()))] value: RwSignal<String>,
|
#[prop(default = RwSignal::new("".to_string()))] value: RwSignal<String>,
|
||||||
#[prop(default = RwSignal::new(false))] disabled: RwSignal<bool>,
|
#[prop(default = RwSignal::new(false))] disabled: RwSignal<bool>,
|
||||||
|
#[prop(default = String::from(""))] name: String,
|
||||||
|
#[prop(default = String::from(""))] label: String
|
||||||
) -> impl IntoView {
|
) -> impl IntoView {
|
||||||
view! (
|
view! (
|
||||||
|
<label>
|
||||||
|
{ label }
|
||||||
<input
|
<input
|
||||||
class="flex-row border-2 m-1.5 p-1.5 rounded-md bg-transparent mt-6 relative transition-all duration-400"
|
class="flex-row border-2 m-1.5 p-1.5 rounded-md bg-transparent mt-6 relative transition-all duration-400"
|
||||||
class=(["border-red-700"], move || !disabled.get())
|
class=(["border-red-700"], move || !disabled.get())
|
||||||
@@ -14,7 +18,9 @@ pub fn OutlinedTextField(
|
|||||||
r#type="text"
|
r#type="text"
|
||||||
disabled={disabled.get()}
|
disabled={disabled.get()}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
prop:value=value
|
prop:name=name
|
||||||
|
bind:value=value // This is a two-way binding - it is allowed to read *and* write.
|
||||||
/>
|
/>
|
||||||
|
</label>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
+95
-22
@@ -3,54 +3,106 @@ use leptos::prelude::*;
|
|||||||
use leptos::leptos_dom::logging::console_log;
|
use leptos::leptos_dom::logging::console_log;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
use aws_sdk_dynamodb::{
|
||||||
|
Client,
|
||||||
|
types::AttributeValue,
|
||||||
|
error::ProvideErrorMetadata
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct StudentSubmission {
|
pub struct StudentInfo {
|
||||||
first_name: String,
|
first_name: String,
|
||||||
last_name: String
|
last_name: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[server(GetSubmission)]
|
||||||
|
pub async fn get_submission(id: String)
|
||||||
|
-> Result<StudentInfo, ServerFnError> {
|
||||||
|
|
||||||
|
let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
|
||||||
|
let dbclient = Client::new(&config);
|
||||||
|
|
||||||
|
console_log(format!("Getting values from API using key {}", id).as_str());
|
||||||
|
|
||||||
|
match dbclient
|
||||||
|
.get_item()
|
||||||
|
.table_name("student-applications")
|
||||||
|
.key("Email", AttributeValue::S(id))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(output) => {
|
||||||
|
console_log("Found submission with passed key.");
|
||||||
|
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(StudentInfo {
|
||||||
|
first_name,
|
||||||
|
last_name
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
let msg = err.message().unwrap_or("Unknown error");
|
||||||
|
console_log(format!("Exception while getting submission information: {}", msg).as_str());
|
||||||
|
Err(ServerFnError::new(msg))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[server(CreateSampleSubmission)]
|
#[server(CreateSampleSubmission)]
|
||||||
pub async fn create_sample_submission() -> Result<StudentSubmission, ServerFnError> {
|
pub async fn create_sample_submission(student_info: StudentInfo) -> Result<(), ServerFnError> {
|
||||||
use aws_sdk_dynamodb::{types::AttributeValue, Client};
|
use aws_sdk_dynamodb::{types::AttributeValue, Client};
|
||||||
|
|
||||||
let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
|
let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
|
||||||
let dbclient = Client::new(&config);
|
let dbclient = Client::new(&config);
|
||||||
|
|
||||||
console_log("Getting values from API");
|
console_log(format!("Creating sample submission with name {} {}", student_info.first_name, student_info.last_name).as_str());
|
||||||
|
|
||||||
|
let submission_id = String::from("testing_student");
|
||||||
|
|
||||||
match dbclient
|
match dbclient
|
||||||
.get_item()
|
.update_item()
|
||||||
.table_name("student-applications")
|
.table_name("student-applications")
|
||||||
.key("Email", AttributeValue::S("google_113247439743075864879".to_string()))
|
.key("Email", AttributeValue::S(submission_id))
|
||||||
|
.expression_attribute_values(":studentFirstName", AttributeValue::S(student_info.first_name))
|
||||||
|
.expression_attribute_values(":studentLastName", AttributeValue::S(student_info.last_name))
|
||||||
|
.expression_attribute_names("#studentFirstName", "studentFirstName")
|
||||||
|
.expression_attribute_names("#studentLastName", "studentLastName")
|
||||||
|
.update_expression("SET #studentFirstName = :studentFirstName, #studentLastName = :studentLastName")
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(output) => {
|
Ok(_) => Ok(()),
|
||||||
println!("Found submission with passed key.");
|
Err(err) => Err(ServerFnError::new(err.into_service_error().to_string()))
|
||||||
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 {
|
|
||||||
first_name: first_name,
|
|
||||||
last_name: last_name
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Err(e) => Err(ServerFnError::new("Couldn't find a submission with that ID."))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn HomePage() -> impl IntoView {
|
pub fn HomePage() -> impl IntoView {
|
||||||
|
let sample_id = RwSignal::new("google_113247439743075864879".to_string());
|
||||||
// Creates a reactive value to update the button
|
// Creates a reactive value to update the button
|
||||||
let count = RwSignal::new(0);
|
let count = RwSignal::new(0);
|
||||||
let elements_disabled = RwSignal::new(false);
|
let elements_disabled = RwSignal::new(false);
|
||||||
let server_resource = Resource::new(|| {}, |_| async {
|
|
||||||
create_sample_submission().await.unwrap()
|
let server_resource = Resource::new(
|
||||||
});
|
move || { sample_id.get() },
|
||||||
|
async |id| {
|
||||||
|
get_submission(id).await.unwrap_or_else(|e| {
|
||||||
|
console_log(e.to_string().as_str());
|
||||||
|
StudentInfo {
|
||||||
|
first_name: String::from("Error"),
|
||||||
|
last_name: String::from("")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
);
|
||||||
let on_click = move |_| {
|
let on_click = move |_| {
|
||||||
*count.write() += 1;
|
*count.write() += 1;
|
||||||
*elements_disabled.write() = count.get() == 10;
|
*elements_disabled.write() = count.get() == 10;
|
||||||
};
|
};
|
||||||
|
let submit_action = ServerAction::<CreateSampleSubmission>::new();
|
||||||
|
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<h1>"Welcome to Leptos!"</h1>
|
<h1>"Welcome to Leptos!"</h1>
|
||||||
@@ -64,9 +116,30 @@ pub fn HomePage() -> impl IntoView {
|
|||||||
}}
|
}}
|
||||||
</Suspense>
|
</Suspense>
|
||||||
</p>
|
</p>
|
||||||
|
<ActionForm action=submit_action>
|
||||||
|
<div>
|
||||||
<OutlinedTextField
|
<OutlinedTextField
|
||||||
placeholder="Testing...".to_string()
|
label="First Name".into()
|
||||||
disabled={elements_disabled} />
|
placeholder="John".into()
|
||||||
<ActionButton on:click=on_click disabled={elements_disabled}>"Click Me: " {count}</ActionButton>
|
disabled={elements_disabled}
|
||||||
|
name="student_info[first_name]".into() />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<OutlinedTextField
|
||||||
|
label="Last Name".into()
|
||||||
|
placeholder="Smith".into()
|
||||||
|
disabled={elements_disabled}
|
||||||
|
name="student_info[last_name]".into() />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<ActionButton
|
||||||
|
on:click=on_click
|
||||||
|
disabled={elements_disabled}
|
||||||
|
button_type="submit".to_string()
|
||||||
|
>
|
||||||
|
"Submit"
|
||||||
|
</ActionButton>
|
||||||
|
</div>
|
||||||
|
</ActionForm>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user