Skip to content

Commit

Permalink
feat: save file to database
Browse files Browse the repository at this point in the history
- Add `Save` to `File` nav bar drop-down
- Parse `id` to `CressTable`
- Add `updateAttachment()`
- Rename `initializeExportButtons()` to `initFileListener()`
- Add save file listener

Resolves: #26
  • Loading branch information
yinanazhou committed Jul 1, 2024
1 parent dc34a4b commit ca70bf0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
4 changes: 2 additions & 2 deletions assets/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
</div>

<div class="navbar-dropdown">
<!-- <div id="save" class="navbar-dropdown-item">
<div id="save" class="navbar-dropdown-item">
<div class="navbar-dropdown-item-text">Save</div>
</div> -->
</div>
<div id="export-to-csv" class="navbar-dropdown-item">
<div class="navbar-dropdown-item-text">Export to CSV</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/CressView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CressView {

document.getElementById('loading').style.display = 'none';

this.table = new CressTable(this.header, this.body);
this.table = new CressTable(this.id, this.header, this.body);

return;
})
Expand Down
36 changes: 36 additions & 0 deletions src/Dashboard/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,39 @@ export function updateDocName(id: string, newName: string): Promise<boolean> {
.catch((err) => reject(err)); // db.get
});
}

/**
* Update the doc from the database
* @param id
* @param body [header, ...content]
* @returns Promise<boolean>
*/
export async function updateAttachment(
id: string,
body: any[],
): Promise<boolean> {
try {
// Retrieve the document using the provided id
const doc = await db.get(id);

// Convert the body array to a JSON blob
const jsonBlob = new Blob([JSON.stringify(body)], {
type: 'application/json',
});

// Update the attachment
doc._attachments = doc._attachments || {};
doc._attachments['table'] = {
content_type: 'application/json',
data: jsonBlob,
};

// Save the document back to the database
await db.put(doc);

return true;
} catch (error) {
console.error('Error updating attachment:', error);
return false;
}
}
40 changes: 25 additions & 15 deletions src/Editor/CressTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ import * as Validation from '../Validation';
import { ImageHandler } from './ImageHandler';
import { ExportHandler } from './ExportHandler';
import { ColumnTools } from './ColumnTools';
import { updateAttachment } from '../Dashboard/Storage';

export class CressTable {
private exportToCsvButton: HTMLElement;
private exportToExcelButton: HTMLElement;
private table: Handsontable;
private images: any[] = []; // Array to store images
private imageHandler: ImageHandler;
private exportHandler: ExportHandler;
private ColumnTools: ColumnTools;

constructor(inputHeader: string[], body: any[]) {
constructor(id: string, inputHeader: string[], body: any[]) {
const container = document.getElementById('hot-container');

// Initialize handlers
Expand Down Expand Up @@ -71,28 +70,39 @@ export class CressTable {
afterValidate: (isValid) => this.setResultStatus(isValid),
});

this.initializeExportButtons(inputHeader, body, headers);
this.initFileListener(id, inputHeader, body, headers);
}

private initializeExportButtons(
private initFileListener(
id: string,
inputHeader: string[],
body: any[],
headers: string[],
) {
this.exportToCsvButton = document.getElementById('export-to-csv');
const exportPlugin = this.table.getPlugin('exportFile');
this.exportToCsvButton.addEventListener('click', () => {
document.getElementById('export-to-csv').addEventListener('click', () => {
this.exportHandler.exportToCsv(exportPlugin);
});

this.exportToExcelButton = document.getElementById('export-to-excel');
this.exportToExcelButton.addEventListener('click', async () => {
await this.exportHandler.exportToExcel(
inputHeader,
body,
headers,
this.images,
);
document
.getElementById('export-to-excel')
.addEventListener('click', async () => {
await this.exportHandler.exportToExcel(
inputHeader,
body,
headers,
this.images,
);
});

document.getElementById('save').addEventListener('click', async () => {
const result = await updateAttachment(id, [inputHeader, ...body]);

if (result) {
// TODO: notification
} else {
// TODO: notification
}
});
}

Expand Down

0 comments on commit ca70bf0

Please sign in to comment.