feat: implement file list retrieval for file drop components

This commit is contained in:
alphafetus committed 2026-03-02 14:25:07 -05:00
1 parent cc09769f28
commit 9d156c17e9
3 files changed
+91 -11

No files matched your search

+15 -1
View File
@@ -8,7 +8,11 @@ use leptos_oidc::AuthSignal;
use leptos_use::{UseDropZoneOptions, UseDropZoneReturn, use_drop_zone_with_options};
#[component]
pub fn FileDrop(#[prop(into)] name: String, #[prop(into)] form_id: String) -> impl IntoView {
pub fn FileDrop(
#[prop(into)] name: String,
#[prop(into)] form_id: String,
#[prop()] existing_files: Resource<Result<Vec<String>, ServerFnError>>,
) -> impl IntoView {
let zone_ref = NodeRef::<Label>::new();
let input_ref = NodeRef::<Input>::new();
@@ -121,6 +125,16 @@ pub fn FileDrop(#[prop(into)] name: String, #[prop(into)] form_id: String) -> im
});
view! {
{move || {
existing_files
.get()
.map(|result| {
if let Ok(files) = result {
file_name_list.set(files);
}
})
}}
<label
node_ref=zone_ref
class="m-1.5 p-2.5 flex flex-col transition-color duration-200 rounded-lg border-2 items-center cursor-pointer"
+53 -8
View File
@@ -2,12 +2,17 @@
use leptos::prelude::*;
use server_fn::codec::{MultipartData, MultipartFormData};
#[cfg(feature = "ssr")]
mod imports {
pub use super::super::{MAIN_TABLE_NAME, S3_BUCKET_NAME};
pub use crate::pages::api::tokens::validate_and_get_token_info;
pub use crate::utils::server::create_aws_config;
pub use aws_sdk_dynamodb::types::{AttributeValue, ReturnValue, Select};
}
#[server(input = MultipartFormData)]
pub async fn upload_file(data: MultipartData) -> Result<String, ServerFnError> {
use super::{MAIN_TABLE_NAME, S3_BUCKET_NAME};
use crate::pages::api::tokens::validate_and_get_token_info;
use crate::utils::server::create_aws_config;
use aws_sdk_dynamodb::types::AttributeValue;
use imports::*;
let mut multipart = data.into_inner().unwrap();
@@ -131,10 +136,7 @@ pub async fn delete_file(
input_name: String,
file_name: String,
) -> Result<String, ServerFnError> {
use super::{MAIN_TABLE_NAME, S3_BUCKET_NAME};
use crate::pages::api::tokens::validate_and_get_token_info;
use crate::utils::server::create_aws_config;
use aws_sdk_dynamodb::types::{AttributeValue, ReturnValue};
use imports::*;
let user_claims = validate_and_get_token_info(access_token).await?;
let subject = user_claims.subject;
@@ -187,3 +189,46 @@ pub async fn delete_file(
Ok(file_name)
}
#[server]
pub async fn list_files(
access_token: String,
form_id: String,
input_name: String,
) -> Result<Vec<String>, ServerFnError> {
use imports::*;
let user_claims = validate_and_get_token_info(access_token).await?;
let subject = user_claims.subject;
let entry_hk = format!("STUDENT#{subject}");
let entry_sk = format!("FILE#{form_id}#{input_name}");
debug_log!("Getting file list: HK = {}, SK = {}", entry_hk, entry_sk);
let dynamo_client = aws_sdk_dynamodb::Client::new(&create_aws_config().await);
dynamo_client
.query()
.table_name(MAIN_TABLE_NAME)
.key_condition_expression("HK = :hk AND begins_with(SK, :sk)")
.expression_attribute_values(":hk".to_string(), AttributeValue::S(entry_hk))
.expression_attribute_values(":sk".to_string(), AttributeValue::S(entry_sk))
.send()
.await
.map(|output| {
let Some(items) = output.items else {
return vec![];
};
items
.into_iter()
.filter_map(|item| {
item.get("file_name")
.map(|v| v.as_s().ok().cloned())
.flatten()
})
.collect::<Vec<String>>()
})
.map_err(|e| ServerFnError::new(format!("Couldn't list files: {}", e.to_string())))
}
+23 -2
View File
@@ -1,11 +1,30 @@
use crate::components::{FileDrop, Header};
use crate::components::{FileDrop, Header, Loading};
use crate::pages::api::files::list_files;
use leptos::prelude::*;
use leptos_oidc::AuthSignal;
#[component]
pub fn StudentFinancialPage() -> impl IntoView {
// This is where we'll get information about the files that have been uploaded.
// For now, we'll leave it blank.
let auth = expect_context::<AuthSignal>();
let files_resource = Resource::new(
move || {
auth.try_with(|a| a.authenticated().map(|a| a.access_token()))
.flatten()
},
async move |access_token| {
list_files(
access_token.unwrap_or_default(),
"financial_info".to_string(),
"fafsa".to_string(),
)
.await
},
);
view! {
<div class="flex flex-1" />
<div class="flex flex-col flex-2 mt-6">
@@ -14,7 +33,9 @@ pub fn StudentFinancialPage() -> impl IntoView {
title="Financial Information"
description="Please upload your FAFSA SAR (Student Aid Report) here."
/>
<FileDrop name="fafsa" form_id="financial_info" />
<Suspense fallback=Loading>
<FileDrop name="fafsa" form_id="financial_info" existing_files=files_resource />
</Suspense>
// </ValidatedForm>
</div>
<div class="flex flex-1" />