feat: refactor checkbox on_change to use match statements for readability
This commit is contained in:
1 parent
059f904f5d
commit
f2d8688c0c
2 files changed
+27
-36
No files matched your search
+22
-23
@@ -77,29 +77,28 @@ pub fn CheckboxList(
|
|||||||
move || {
|
move || {
|
||||||
let result = ValueType::String(Some(item_name.clone()));
|
let result = ValueType::String(Some(item_name.clone()));
|
||||||
data_map.update(|map| {
|
data_map.update(|map| {
|
||||||
if let Some(value_type) = map.get_mut(&data_member) {
|
// Attempt to get the value type from the hash map.
|
||||||
// Get the list from the enum
|
match map.get_mut(&data_member) {
|
||||||
if let Some(mut list) = value_type.as_list().unwrap_or(None) {
|
Some(value_type) => {
|
||||||
// Check if the value exists in the list or not. Both of these branches
|
// Attempt to get the value as a list.
|
||||||
// directly update the value within the hash map, otherwise the change
|
match value_type.as_list().unwrap_or(None) {
|
||||||
// isn't reactive.
|
Some(mut list) => {
|
||||||
if let Some(index) = list.iter().position(|val| *val == result) {
|
// Attempt to find the selected value in the selected list.
|
||||||
// Value is already selected, deselect it.
|
// If it's present, remove it, or vice versa.
|
||||||
list.remove(index);
|
match list.iter().position(|val| *val == result) {
|
||||||
} else {
|
Some(index) => { list.remove(index); },
|
||||||
// Value is not selected, so select it.
|
None => { list.push(result); }
|
||||||
list.push(result);
|
};
|
||||||
}
|
// Update the existing value_type entry in the hash map.
|
||||||
*value_type = ValueType::List(Some(list));
|
*value_type = ValueType::List(Some(list));
|
||||||
} else {
|
},
|
||||||
// Update the value at the current data_member
|
// Insert a new value_type entry.
|
||||||
let temp = map.get_mut(&data_member.clone()).unwrap();
|
None => { *value_type = ValueType::List(Some(vec![result])); }
|
||||||
*temp = ValueType::List(Some(vec![result]));
|
};
|
||||||
}
|
},
|
||||||
} else {
|
// Insert a new value_type entry.
|
||||||
// Insert a new list into the hash map
|
None => { map.insert(data_member.clone(), ValueType::List(Some(vec![result]))); }
|
||||||
map.insert(data_member.clone(), ValueType::List(Some(vec![result])));
|
};
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+5
-13
@@ -58,7 +58,7 @@ pub async fn get_submission(id: String) -> Result<StudentInfo, ServerFnError> {
|
|||||||
///
|
///
|
||||||
/// All information is stored using a `PutItemCommand` in the student application DynamoDB table.
|
/// All information is stored using a `PutItemCommand` in the student application DynamoDB table.
|
||||||
#[server(CreateSampleSubmission, endpoint = "/create-sample-submission")]
|
#[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: ExpandableInfo) -> Result<(), ServerFnError> {
|
||||||
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);
|
||||||
|
|
||||||
@@ -137,7 +137,6 @@ pub fn HomePage() -> impl IntoView {
|
|||||||
server_resource
|
server_resource
|
||||||
.get()
|
.get()
|
||||||
.map(|submission| {
|
.map(|submission| {
|
||||||
let store_info = Store::new(submission.clone());
|
|
||||||
let expandable = ExpandableInfo::new("email".into());
|
let expandable = ExpandableInfo::new("email".into());
|
||||||
let expandable_react = expandable.as_reactive();
|
let expandable_react = expandable.as_reactive();
|
||||||
expandable_react.data.update(|map| {
|
expandable_react.data.update(|map| {
|
||||||
@@ -145,7 +144,6 @@ pub fn HomePage() -> impl IntoView {
|
|||||||
map.insert("phone_number".into(), ValueType::String(Some("123456789-10".into())));
|
map.insert("phone_number".into(), ValueType::String(Some("123456789-10".into())));
|
||||||
map.insert("math_sat".into(), ValueType::Number(Some(1234)));
|
map.insert("math_sat".into(), ValueType::Number(Some(1234)));
|
||||||
});
|
});
|
||||||
let reactive_info = submission.as_reactive();
|
|
||||||
let elements_disabled = RwSignal::new(false);
|
let elements_disabled = RwSignal::new(false);
|
||||||
let result_msg = Signal::derive(move || {
|
let result_msg = Signal::derive(move || {
|
||||||
// Eventually, this function will show and hide a loading symbol
|
// Eventually, this function will show and hide a loading symbol
|
||||||
@@ -275,18 +273,12 @@ pub fn HomePage() -> impl IntoView {
|
|||||||
<Row>
|
<Row>
|
||||||
<ActionButton
|
<ActionButton
|
||||||
on:click=move |_| {
|
on:click=move |_| {
|
||||||
let captured = reactive_info.capture();
|
|
||||||
let captured_map = expandable_react.capture();
|
let captured_map = expandable_react.capture();
|
||||||
console_log(format!("Found values: {captured:?}").as_str());
|
console_log(format!("Map values: {:?}", captured_map).as_str());
|
||||||
console_log(format!("Map values: {captured_map:?}").as_str());
|
submit_action
|
||||||
log_action
|
.dispatch(CreateSampleSubmission {
|
||||||
.dispatch(LogExpandableInfo {
|
student_info: captured_map,
|
||||||
info: captured_map
|
|
||||||
});
|
});
|
||||||
// submit_action
|
|
||||||
// .dispatch(CreateSampleSubmission {
|
|
||||||
// student_info: captured,
|
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
disabled=elements_disabled
|
disabled=elements_disabled
|
||||||
>"Submit"</ActionButton>
|
>"Submit"</ActionButton>
|
||||||
|
|||||||
Reference in new issue
Block a user