chore: pc change
This commit is contained in:
1 parent
21ba5e45ff
commit
f7ca1ecbc5
27 files changed
+926
-262
No files matched your search
+125
-68
@@ -1,38 +1,54 @@
|
||||
use crate::components::{ActionButton, OutlinedTextField};
|
||||
use crate::common::StudentInfo;
|
||||
use leptos::leptos_dom::logging::console_log;
|
||||
use leptos::prelude::*;
|
||||
|
||||
use std::collections::HashMap;
|
||||
#[cfg(feature = "ssr")]
|
||||
use aws_sdk_dynamodb::{error::ProvideErrorMetadata, types::AttributeValue, Client};
|
||||
use crate::app::Unauthenticated;
|
||||
use crate::common::{StudentInfo, StudentInfoReactive, UserClaims};
|
||||
use crate::components::{ActionButton, Loading, OutlinedTextField, Select};
|
||||
use leptos::leptos_dom::logging::console_log;
|
||||
use leptos::prelude::*;
|
||||
use leptos_oidc::{Algorithm, AuthLoaded, AuthSignal, Authenticated};
|
||||
|
||||
#[server(GetSubmission, endpoint = "/get-submission")]
|
||||
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());
|
||||
console_log(format!("Getting values from API using username {}", id).as_str());
|
||||
|
||||
match dbclient
|
||||
.get_item()
|
||||
.table_name("student-applications")
|
||||
.key("Email", AttributeValue::S(id))
|
||||
.key("Email", AttributeValue::S(id.clone()))
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(output) => {
|
||||
console_log("Found submission with passed key.");
|
||||
let item = output.item.unwrap();
|
||||
let first_name = item
|
||||
|
||||
let first_name: String = item
|
||||
.get("studentFirstName")
|
||||
.unwrap()
|
||||
.as_s()
|
||||
.unwrap()
|
||||
.clone();
|
||||
let last_name = item.get("studentLastName").unwrap().as_s().unwrap().clone();
|
||||
.and_then(|attr| attr.as_s().ok())
|
||||
.map(|s| s.to_owned())
|
||||
.unwrap_or_default();
|
||||
|
||||
let last_name: String = item
|
||||
.get("studentLastName")
|
||||
.and_then(|attr| attr.as_s().ok())
|
||||
.map(|s| s.to_owned())
|
||||
.unwrap_or_default();
|
||||
|
||||
let math_sat_score = item
|
||||
.get("mathSAT")
|
||||
.and_then(|attr| attr.as_n().ok())
|
||||
.map(|n| n.parse::<i32>().unwrap_or(0))
|
||||
.unwrap_or_default();
|
||||
|
||||
console_log(format!("Got values from API: {} {}", first_name, last_name).as_str());
|
||||
|
||||
Ok(StudentInfo {
|
||||
first_name,
|
||||
last_name,
|
||||
math_sat_score,
|
||||
})
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -43,7 +59,10 @@ pub async fn get_submission(id: String) -> Result<StudentInfo, ServerFnError> {
|
||||
}
|
||||
|
||||
#[server(CreateSampleSubmission, endpoint = "/create-sample-submission")]
|
||||
pub async fn create_sample_submission(student_info: StudentInfo) -> Result<(), ServerFnError> {
|
||||
pub async fn create_sample_submission(
|
||||
student_info: StudentInfo,
|
||||
subject: String,
|
||||
) -> Result<(), ServerFnError> {
|
||||
use aws_sdk_dynamodb::{types::AttributeValue, Client};
|
||||
|
||||
let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
|
||||
@@ -51,18 +70,16 @@ pub async fn create_sample_submission(student_info: StudentInfo) -> Result<(), S
|
||||
|
||||
console_log(
|
||||
format!(
|
||||
"Creating sample submission with name {} {}",
|
||||
student_info.first_name, student_info.last_name
|
||||
"Creating sample submission with name {} {}, math score {}, and subject {}",
|
||||
student_info.first_name, student_info.last_name, student_info.math_sat_score, subject
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
|
||||
let submission_id = String::from("testing_student");
|
||||
|
||||
match dbclient
|
||||
.update_item()
|
||||
.table_name("student-applications")
|
||||
.key("Email", AttributeValue::S(submission_id))
|
||||
.key("Email", AttributeValue::S(subject))
|
||||
.expression_attribute_values(
|
||||
":studentFirstName",
|
||||
AttributeValue::S(student_info.first_name),
|
||||
@@ -71,10 +88,17 @@ pub async fn create_sample_submission(student_info: StudentInfo) -> Result<(), S
|
||||
":studentLastName",
|
||||
AttributeValue::S(student_info.last_name),
|
||||
)
|
||||
.expression_attribute_values(
|
||||
":mathScoreSAT",
|
||||
AttributeValue::N(student_info.math_sat_score.to_string()),
|
||||
)
|
||||
.expression_attribute_names("#studentFirstName", "studentFirstName")
|
||||
.expression_attribute_names("#studentLastName", "studentLastName")
|
||||
.expression_attribute_names("#mathScoreSAT", "mathScoreSAT")
|
||||
.update_expression(
|
||||
"SET #studentFirstName = :studentFirstName, #studentLastName = :studentLastName",
|
||||
concat!("SET #studentFirstName = :studentFirstName, ",
|
||||
"#studentLastName = :studentLastName, ",
|
||||
"#mathScoreSAT = :mathScoreSAT"),
|
||||
)
|
||||
.send()
|
||||
.await
|
||||
@@ -86,65 +110,98 @@ pub async fn create_sample_submission(student_info: StudentInfo) -> Result<(), S
|
||||
|
||||
#[component]
|
||||
pub fn HomePage() -> impl IntoView {
|
||||
let sample_id = RwSignal::new("google_113247439743075864879".to_string());
|
||||
// Creates a reactive value to update the button
|
||||
let count = RwSignal::new(0);
|
||||
let elements_disabled = RwSignal::new(false);
|
||||
|
||||
let auth = use_context::<AuthSignal>().expect("Couldn't find user information.");
|
||||
let user_claims = Signal::derive(move || {
|
||||
auth.with(|auth| {
|
||||
auth.authenticated().and_then(|data| {
|
||||
data.decoded_access_token::<UserClaims>(Algorithm::RS256, &["account"])
|
||||
})
|
||||
})
|
||||
});
|
||||
|
||||
// Note that the value passed in MUST be equatable.
|
||||
// We get/unwrap the value repeatedly until we get a simple string value, then clone it so that
|
||||
// we don't lose access to it in the future, should we need it again.
|
||||
let server_resource = Resource::new(
|
||||
move || sample_id.get(),
|
||||
async |id| {
|
||||
get_submission(id).await.unwrap_or_else(|e| {
|
||||
move || user_claims.get().map(|claim| claim.claims.subject.clone()),
|
||||
async |opt_username| match opt_username {
|
||||
Some(username) => get_submission(username).await.unwrap_or_else(|e| {
|
||||
console_log(e.to_string().as_str());
|
||||
StudentInfo {
|
||||
first_name: String::from("Error"),
|
||||
last_name: String::from(""),
|
||||
math_sat_score: 0,
|
||||
}
|
||||
})
|
||||
}),
|
||||
None => StudentInfo {
|
||||
first_name: String::new(),
|
||||
last_name: String::new(),
|
||||
math_sat_score: 0,
|
||||
},
|
||||
},
|
||||
);
|
||||
let on_click = move |_| {
|
||||
*count.write() += 1;
|
||||
*elements_disabled.write() = count.get() == 10;
|
||||
};
|
||||
let submit_action = ServerAction::<CreateSampleSubmission>::new();
|
||||
|
||||
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>
|
||||
<ActionForm action=submit_action>
|
||||
<div>
|
||||
<OutlinedTextField
|
||||
label="First Name".into()
|
||||
placeholder="John".into()
|
||||
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".into()
|
||||
>
|
||||
"Submit"
|
||||
</ActionButton>
|
||||
</div>
|
||||
</ActionForm>
|
||||
<AuthLoaded fallback=Loading>
|
||||
<Authenticated unauthenticated=Unauthenticated>
|
||||
// Replace this fallback with a real loading screen.
|
||||
<Suspense fallback=Loading>
|
||||
{move || {
|
||||
server_resource.get().map(|submission: StudentInfo| {
|
||||
let reactive_info = StudentInfoReactive::new(submission);
|
||||
|
||||
let select_value = RwSignal::new(String::from("Math"));
|
||||
|
||||
view! {
|
||||
<p>
|
||||
"Current user's reported full name from the API: "
|
||||
{reactive_info.first_name}" "{reactive_info.last_name}
|
||||
</p>
|
||||
|
||||
<form>
|
||||
<div>
|
||||
<OutlinedTextField
|
||||
label="First Name".into()
|
||||
placeholder="John".into()
|
||||
value=reactive_info.first_name />
|
||||
</div>
|
||||
<div>
|
||||
<OutlinedTextField
|
||||
label="Last Name".into()
|
||||
placeholder="Smith".into()
|
||||
value=reactive_info.last_name />
|
||||
</div>
|
||||
<div>
|
||||
<OutlinedTextField
|
||||
label="Testing".into()
|
||||
placeholder="Test".into()
|
||||
value=reactive_info.math_sat_score />
|
||||
</div>
|
||||
<Select
|
||||
value_list = vec!["Math".into(), "English".into(), "Science".into()]
|
||||
value = select_value
|
||||
/>
|
||||
<div>
|
||||
<ActionButton
|
||||
on:click=move |_| {
|
||||
submit_action.dispatch(CreateSampleSubmission {
|
||||
student_info: reactive_info.capture(),
|
||||
subject: user_claims.get().unwrap().claims.subject.clone()
|
||||
});
|
||||
}
|
||||
>
|
||||
"Submit"
|
||||
</ActionButton>
|
||||
</div>
|
||||
</form>
|
||||
}
|
||||
})
|
||||
}}
|
||||
</Suspense>
|
||||
</Authenticated>
|
||||
</AuthLoaded>
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user