feat: refactor checkbox on_change to use match statements for readability

This commit is contained in:
alphafetus committed 2025-09-25 13:29:23 -04:00
1 parent 059f904f5d
commit f2d8688c0c
2 files changed
+26 -35

No files matched your search

+21 -22
View File
@@ -77,29 +77,28 @@ pub fn CheckboxList(
move || {
let result = ValueType::String(Some(item_name.clone()));
data_map.update(|map| {
if let Some(value_type) = map.get_mut(&data_member) {
// Get the list from the enum
if let Some(mut list) = value_type.as_list().unwrap_or(None) {
// Check if the value exists in the list or not. Both of these branches
// directly update the value within the hash map, otherwise the change
// isn't reactive.
if let Some(index) = list.iter().position(|val| *val == result) {
// Value is already selected, deselect it.
list.remove(index);
} else {
// Value is not selected, so select it.
list.push(result);
}
// Attempt to get the value type from the hash map.
match map.get_mut(&data_member) {
Some(value_type) => {
// Attempt to get the value as a list.
match value_type.as_list().unwrap_or(None) {
Some(mut list) => {
// Attempt to find the selected value in the selected list.
// If it's present, remove it, or vice versa.
match list.iter().position(|val| *val == result) {
Some(index) => { list.remove(index); },
None => { list.push(result); }
};
// Update the existing value_type entry in the hash map.
*value_type = ValueType::List(Some(list));
} else {
// Update the value at the current data_member
let temp = map.get_mut(&data_member.clone()).unwrap();
*temp = ValueType::List(Some(vec![result]));
}
} else {
// Insert a new list into the hash map
map.insert(data_member.clone(), ValueType::List(Some(vec![result])));
}
},
// Insert a new value_type entry.
None => { *value_type = ValueType::List(Some(vec![result])); }
};
},
// Insert a new value_type entry.
None => { map.insert(data_member.clone(), ValueType::List(Some(vec![result]))); }
};
});
}
};
+5 -13
View File
@@ -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.
#[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 dbclient = Client::new(&config);
@@ -137,7 +137,6 @@ pub fn HomePage() -> impl IntoView {
server_resource
.get()
.map(|submission| {
let store_info = Store::new(submission.clone());
let expandable = ExpandableInfo::new("email".into());
let expandable_react = expandable.as_reactive();
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("math_sat".into(), ValueType::Number(Some(1234)));
});
let reactive_info = submission.as_reactive();
let elements_disabled = RwSignal::new(false);
let result_msg = Signal::derive(move || {
// Eventually, this function will show and hide a loading symbol
@@ -275,18 +273,12 @@ pub fn HomePage() -> impl IntoView {
<Row>
<ActionButton
on:click=move |_| {
let captured = reactive_info.capture();
let captured_map = expandable_react.capture();
console_log(format!("Found values: {captured:?}").as_str());
console_log(format!("Map values: {captured_map:?}").as_str());
log_action
.dispatch(LogExpandableInfo {
info: captured_map
console_log(format!("Map values: {:?}", captured_map).as_str());
submit_action
.dispatch(CreateSampleSubmission {
student_info: captured_map,
});
// submit_action
// .dispatch(CreateSampleSubmission {
// student_info: captured,
// });
}
disabled=elements_disabled
>"Submit"</ActionButton>