Work on adding files to a Journal Entry

This commit is contained in:
Scott Idem
2025-05-19 19:23:16 -04:00
parent 2976d618f8
commit 83a8377155
3 changed files with 263 additions and 6 deletions

View File

@@ -86,7 +86,7 @@ export async function load_ae_obj_li__hosted_file(
}
// Check if for_obj_type is in the list of valid Aether object types:
let valid_for_obj_types = ['account', 'archive', 'archive_content', 'event', 'event_session', 'event_presentation', 'event_presenter', 'event_location', 'post', 'post_comment'];
let valid_for_obj_types = ['account', 'archive', 'archive_content', 'event', 'event_session', 'event_presentation', 'event_presenter', 'event_location', 'journal', 'journal_entry', 'post', 'post_comment'];
if (!valid_for_obj_types.includes(for_obj_type)) {
console.log(`Invalid for_obj_type: ${for_obj_type}`);
return [];

View File

@@ -18,7 +18,7 @@ export async function db_save_ae_obj_li__ae_obj({
properties_to_save: string[];
log_lvl?: number;
}) {
log_lvl = 1;
// log_lvl = 1;
if (log_lvl) {
console.log(`*** db_save_ae_obj_li__ae_obj() *** table_name=${table_name}`, obj_li);
}
@@ -72,7 +72,7 @@ export async function db_save_ae_obj_li__ae_obj({
} catch (error) {
// This is fairly common and normal if the object is new
if (log_lvl) {
console.error(`Bulk update failed. Falling back to bulkPut.`, error);
console.log(`Bulk update failed. Falling back to bulkPut. This is normal.`, error);
}
}
@@ -85,7 +85,7 @@ export async function db_save_ae_obj_li__ae_obj({
return putKeys;
} catch (error) {
// This should not happen if the object is new
console.error(`Bulk put failed.`, error);
console.error(`Bulk put failed. Something likely went wrong!`, error);
return [];
}
}

View File

@@ -35,11 +35,12 @@ import E_app_codemirror_v5 from '$lib/e_app_codemirror_v5.svelte';
// *** Import Aether specific variables and functions
import type { key_val } from '$lib/ae_stores';
import { ae_util } from '$lib/ae_utils/ae_utils';
import { core_func } from '$lib/ae_core/ae_core_functions';
import { ae_snip, ae_loc, ae_sess, ae_api, ae_trig, slct, slct_trigger } from '$lib/ae_stores';
import { journals_loc, journals_sess, journals_slct, journals_trig, journals_prom } from '$lib/ae_journals/ae_journals_stores';
import { journals_func } from '$lib/ae_journals/ae_journals_functions';
console.log(`ae_comp__journal_entry_obj_id_view.svelte`);
import Comp_hosted_files_upload from '$lib/ae_core/ae_comp__hosted_files_upload.svelte';
import Element_manage_hosted_file_li_wrap from '$lib/element_manage_hosted_file_li_all.svelte';
interface Props {
log_lvl?: number;
@@ -55,11 +56,17 @@ let {
lq__journal_entry_obj,
}: Props = $props();
if (log_lvl) {
console.log(`ae_comp__journal_entry_obj_id_view.svelte`, lq__journal_obj, lq__journal_obj_li, lq__journal_entry_obj);
}
let ae_promises: key_val = $state({});
// let ae_tmp: key_val = {};
// let ae_trigger: any = null;
// let ae_triggers: key_val = {};
let show_menu: boolean = $state(false);
@@ -297,6 +304,12 @@ async function update_journal_entry() {
// log_lvl = 1;
// append slct_hosted_file_kv to data_json.hosted_file_kv
if (!tmp_entry_obj.data_json.hosted_file_kv) {
tmp_entry_obj.data_json.hosted_file_kv = {};
}
tmp_entry_obj.data_json.hosted_file_kv = $journals_loc.entry.hosted_file_kv;
let data_kv: key_val = {
alert: tmp_entry_obj?.alert,
personal: tmp_entry_obj?.personal,
@@ -317,6 +330,8 @@ async function update_journal_entry() {
history: tmp_entry_obj?.history,
history_encrypted: null, // This should only be generated below.
data_json: tmp_entry_obj?.data_json,
sort: tmp_entry_obj?.sort,
group: tmp_entry_obj?.group,
archive_on: tmp_entry_obj?.archive_on,
@@ -846,6 +861,46 @@ function handle_marked(text_string: string) {
});
return marked_string;
}
let slct_hosted_file_kv: key_val = $state({});
let slct_hosted_file_id: any = $state(null);
let slct_hosted_file_obj: any = $state(null);
$effect(() => {
if (slct_hosted_file_id && slct_hosted_file_obj) {
console.log(`*** handle_hosted_file_selected() *** ${slct_hosted_file_id}`);
// We need to update the journal_entry_obj with the new file (for now just the first one).
update_journal_entry();
// ae_promises.journal_entry_obj = archives_func.update_ae_obj__journal_entry({
// api_cfg: $ae_api,
// journal_entry_id: tmp_entry_obj.journal_entry_id,
// data_kv: {
// hosted_file_id_random: slct_hosted_file_id,
// // file_path: hosted_file_obj_li[0].file_path,
// filename: slct_hosted_file_obj.filename,
// file_extension: slct_hosted_file_obj.extension,
// journal_entry_type: 'hosted_file',
// },
// log_lvl: log_lvl
// })
// .then(function (journal_entry_obj_update_result) {
// // tmp_entry_obj.journal_entry_obj = $lq__journal_entry_obj;
// tmp_entry_obj.journal_entry_obj = journal_entry_obj_update_result;
// })
// .catch(function (error) {
// console.log('Something went wrong.');
// console.log(error);
// return false;
// });
slct_hosted_file_id = null;
slct_hosted_file_obj = null;
}
});
</script>
@@ -2210,6 +2265,208 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} -->
{/if}
<hr class="divider my-2" />
{#if $lq__journal_entry_obj?.journal_entry_id}
<section
class:hidden={!$ae_loc.edit_mode}
class="ae_section journal_entry__hosted_file border border-gray-200 rounded p-2 space-y-2"
>
<h3 class="h3">Upload/Manage Hosted File</h3>
{#if !tmp_entry_obj?.data_json?.hosted_file_kv}
No file(s) uploaded yet.
{#if $ae_loc.trusted_access}
<button
type="button"
class="btn btn-sm variant-ghost-warning hover:variant-filled-warning float-right"
title="Toggle between Upload and Select from Hosted Files"
onclick={() => {
if ($ae_sess.files.add_to_use_files_method == 'upload') {
$ae_sess.files.add_to_use_files_method = 'select';
} else {
$ae_sess.files.add_to_use_files_method = 'upload';
}
}}
>
<span class="fas fa-exchange-alt m-1"></span>
Upload/Select
</button>
<div
class:hidden={$ae_sess.files.add_to_use_files_method != 'upload'}
class="upload"
>
<Comp_hosted_files_upload
class_li="border border-gray-300 rounded-md p-2 bg-gray-100 hover:bg-gray-200"
link_to_type="journal_entry"
link_to_id={tmp_entry_obj.journal_entry_id}
bind:hosted_file_id_li={tmp_entry_obj.hosted_file_id_li}
bind:hosted_file_obj_li={tmp_entry_obj.hosted_file_obj_li}
bind:upload_complete={tmp_entry_obj.upload_complete}
log_lvl={log_lvl}
>
{#snippet label()}
<span >
<div>
<span class="fas fa-upload"></span>
<strong class="bg-green-100 p-1">Upload Journal Entry files</strong>
</div>
<span class="text-sm text-gray-600 dark:text-gray-400 italic">
<strong>Aether hosted files only</strong>
</span>
</span>
{/snippet}
</Comp_hosted_files_upload>
</div>
<div
class:hidden={$ae_sess.files.add_to_use_files_method != 'select'}
class=""
>
<!-- link_to_type={'journal_entry'} -->
<!-- link_to_id={$lq__journal_entry_obj?.journal_entry_id} -->
<Element_manage_hosted_file_li_wrap
link_to_type={'account'}
link_to_id={$ae_loc?.account_id}
allow_basic={true}
allow_moderator={true}
class_li={''}
bind:slct_hosted_file_kv={slct_hosted_file_kv}
bind:slct_hosted_file_id={slct_hosted_file_id}
bind:slct_hosted_file_obj={slct_hosted_file_obj}
/>
</div>
{/if}
{:else}
<button
disabled={!$ae_loc.administrator_access}
type="button"
onclick={() => {
if (confirm('Are you sure you want to remove the file?')) {
// First - Attempt to delete the hosted file
ae_promises.journal_entry_obj__hosted_file = core_func.delete_ae_obj_id__hosted_file({
api_cfg: $ae_api,
hosted_file_id: tmp_entry_obj.hosted_file_id,
link_to_type: 'journal_entry',
link_to_id: $lq__journal_entry_obj?.journal_entry_id,
rm_orphan: true,
fake_delete: false,
log_lvl: log_lvl
})
.then(function (delete_result) {
// Second - If deleted, then update the journal_entry_obj
console.log(`File removed. Now update the journal_entry_obj`);
update_journal_entry();
// ae_promises.journal_entry_obj = archives_func.update_ae_obj__journal_entry({
// api_cfg: $ae_api,
// journal_entry_id: $lq__journal_entry_obj?.journal_entry_id,
// data_kv: {
// hosted_file_id_random: null,
// file_path: null,
// filename: null,
// file_extension: null,
// journal_entry_type: null,
// },
// log_lvl: log_lvl
// })
// .then(function (journal_entry_obj_update_result) {
// // We need to do all of this since the DB object has changed and the SLCT object does automatically update (yet...??? Svelte 5?).
// // tmp_entry_obj = $lq__journal_entry_obj;
// })
// .catch(function (error) {
// console.log('Something went wrong.');
// console.log(error);
// return false;
// });
})
.catch(function (error) {
console.log('Something went wrong.');
console.log(error);
return false;
})
.finally(() => {
// We need to do all of this since the DB object has changed and the SLCT object does automatically update (yet...??? Svelte 5?).
tmp_entry_obj.hosted_file_id = null;
tmp_entry_obj.file_path = null;
tmp_entry_obj.filename = null;
tmp_entry_obj.file_extension = null;
});
}
}}
class="novi_btn btn btn-sm variant-soft-error float-right"
>
{#await ae_promises.journal_entry_obj__hosted_file}
<span class="fas fa-spinner fa-spin m-1"></span>
{:then}
<span class="fas fa-trash-alt m-1"></span>
Remove File
{/await}
<!-- <span class="fas fa-trash-alt m-1"></span>
Remove File -->
</button>
<!-- <label for="file_path">File Path
{#if !$ae_loc.administrator_access}
<span class="fas fa-lock" title="Field is locked"></span>
{:else}
<span class="fas fa-unlock" title="Field is unlocked"></span>
{/if}
<input
type="text"
id="file_path"
name="file_path"
value={(tmp_entry_obj.file_path ? tmp_entry_obj.file_path : '')}
readonly={!$ae_loc.administrator_access}
class="input w-full"
>
</label> -->
<label for="filename">Filename
<input type="text" id="filename" name="filename" value={(tmp_entry_obj.filename ? tmp_entry_obj.filename : 'unknown')} class="input w-full">
</label>
<label for="file_extension">File Extension
{#if !$ae_loc.administrator_access}
<span class="fas fa-lock" title="Field is locked"></span>
{:else}
<span class="fas fa-unlock" title="Field is unlocked"></span>
{/if}
<input
type="text"
id="file_extension"
name="file_extension"
value={(tmp_entry_obj.file_extension ? tmp_entry_obj.file_extension : 'ext')}
readonly={!$ae_loc.administrator_access}
class="input w-24"
>
</label>
{/if}
</section>
{:else}
<section
class="ae_section journal_entry__hosted_file border border-gray-200 rounded p-2 space-y-2"
>
<span class="fas fa-exclamation-triangle text-red-500"></span>
Save the form first before uploading a file.
</section>
{/if}
{:else if ($journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id] == 'history')}
<div