Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/components/Dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const Dashboard: React.FC<DashboardProps> = ({ store, onOpenFile, currentBillTyp
};

useEffect(() => {
loadData();
loadData().catch(err => console.error("Failed to load data:", err));
checkConnection();
}, []);

Expand All @@ -87,11 +87,14 @@ const Dashboard: React.FC<DashboardProps> = ({ store, onOpenFile, currentBillTyp
return new Date(date).toLocaleString();
};

const handleOpen = (key: string) => {
store._getFile(key).then((data: any) => {
const handleOpen = async (key: string) => {
try {
const data: any = await store._getFile(key);
AppGeneral.viewFile(key, decodeURIComponent(data.content));
onOpenFile(key, data.billType);
});
} catch (err) {
displayToast("Failed to open file");
}
};

const handleBackup = async (key: string) => {
Expand Down Expand Up @@ -120,7 +123,7 @@ const Dashboard: React.FC<DashboardProps> = ({ store, onOpenFile, currentBillTyp
await store._addBackupHistory({ cid: record.cid, timestamp: new Date().toISOString(), name: key });
await store._incrementMetric('invoicesBackedUp');
await store._logActivity({ type: 'BACKUP', description: `Backed up ${key} to IPFS`, cid: record.cid });
loadData();
await loadData();
setBackupSuccessCid(record.cid);
} catch (error) {
displayToast(error instanceof Error ? error.message : "Backup failed");
Expand Down Expand Up @@ -158,7 +161,7 @@ const Dashboard: React.FC<DashboardProps> = ({ store, onOpenFile, currentBillTyp
await store._saveFile(file);
await store._incrementMetric('successfulRestores');
await store._logActivity({ type: 'RESTORE', description: `Restored ${filename} from IPFS`, cid });
loadData();
await loadData();
handleOpen(filename);
setRestoreSuccessFile(filename);
} catch (error) {
Expand All @@ -172,7 +175,7 @@ const Dashboard: React.FC<DashboardProps> = ({ store, onOpenFile, currentBillTyp
try {
await navigator.clipboard.writeText(`CID: ${cid}\nGateway: https://gateway.pinata.cloud/ipfs/${cid}`);
await store._logActivity({ type: 'SHARE', description: `Shared IPFS CID`, cid });
loadData();
await loadData();
displayToast("CID & Gateway Link copied to clipboard!");
} catch (err) {
displayToast("Failed to copy link");
Expand All @@ -188,7 +191,7 @@ const Dashboard: React.FC<DashboardProps> = ({ store, onOpenFile, currentBillTyp
if (fileToDelete) {
await store._deleteFile(fileToDelete);
await store._logActivity({ type: 'DELETE', description: `Deleted invoice ${fileToDelete}` });
loadData();
await loadData();
setFileToDelete(null);
displayToast("Invoice deleted");
}
Expand All @@ -208,7 +211,7 @@ const Dashboard: React.FC<DashboardProps> = ({ store, onOpenFile, currentBillTyp
await store._saveFile(file);
await store._incrementMetric('invoicesCreated');
await store._logActivity({ type: 'CREATE', description: `Created new invoice ${filename}` });
loadData();
await loadData();
handleOpen(filename);
};

Expand Down
27 changes: 17 additions & 10 deletions src/components/Files/Files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ const Files: React.FC<{
setShowToast(true);
};

const editFile = (key: string) => {
props.store._getFile(key).then((data: any) => {
const editFile = async (key: string) => {
try {
const data: any = await props.store._getFile(key);
AppGeneral.viewFile(key, decodeURIComponent(data.content));
props.updateSelectedFile(key);
props.updateBillType(data.billType);
setListFiles(false);
});
} catch (err) {
displayToast("Failed to open file");
}
};

const deleteFile = (key: string) => {
Expand Down Expand Up @@ -124,7 +127,7 @@ const Files: React.FC<{
name: key
});
displayToast("Backup successful!");
loadFiles();
await loadFiles();
} catch (error) {
displayToast(error instanceof Error ? error.message : "Backup failed");
} finally {
Expand All @@ -148,7 +151,7 @@ const Files: React.FC<{
);
await props.store._saveFile(file);
displayToast(`Restored IPFS version of ${key}`);
loadFiles();
await loadFiles();
editFile(key); // Automatically open the restored version
} catch (error) {
displayToast("Restore failed: " + (error instanceof Error ? error.message : String(error)));
Expand Down Expand Up @@ -321,12 +324,16 @@ const Files: React.FC<{
{ text: "No", role: "cancel" },
{
text: "Yes",
handler: () => {
handler: async () => {
if (currentKey) {
props.store._deleteFile(currentKey);
loadDefault();
setCurrentKey(null);
loadFiles(); // Refresh list after delete
try {
await props.store._deleteFile(currentKey);
loadDefault();
setCurrentKey(null);
await loadFiles();
} catch (err) {
displayToast("Failed to delete file");
}
}
},
},
Expand Down
72 changes: 44 additions & 28 deletions src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,42 @@ const Menu: React.FC<{
}
return filename;
};
const doPrint = () => {
const doPrint = async () => {
if (isPlatform("hybrid")) {
const printer = Printer;
printer.print(AppGeneral.getCurrentHTMLContent());
try {
await printer.print(AppGeneral.getCurrentHTMLContent());
} catch (err) {
console.error("Print failed:", err);
}
} else {
const content = AppGeneral.getCurrentHTMLContent();
const printWindow = window.open("/printwindow", "Print Invoice");
printWindow.document.write(content);
printWindow.print();
}
};
const doSave = () => {
const doSave = async () => {
if (props.file === "default") {
setShowAlert1(true);
return;
}
const content = encodeURIComponent(AppGeneral.getSpreadsheetContent());
const data = props.store._getFile(props.file);
const file = new File(
(data as any).created,
new Date().toString(),
content,
props.file,
props.bT
);
props.store._saveFile(file);
props.updateSelectedFile(props.file);
setShowAlert2(true);
try {
const content = encodeURIComponent(AppGeneral.getSpreadsheetContent());
const data: any = await props.store._getFile(props.file);
const file = new File(
data.created,
new Date().toString(),
content,
props.file,
props.bT
);
await props.store._saveFile(file);
props.updateSelectedFile(props.file);
setShowAlert2(true);
} catch (err) {
console.error("Save failed:", err);
}
};
const doSaveAs = async (filename) => {
if (filename) {
Expand All @@ -105,9 +113,13 @@ const Menu: React.FC<{
filename,
props.bT
);
props.store._saveFile(file);
props.updateSelectedFile(filename);
setShowAlert4(true);
try {
await props.store._saveFile(file);
props.updateSelectedFile(filename);
setShowAlert4(true);
} catch (err) {
console.error("Save As failed:", err);
}
} else {
setShowToast1(true);
}
Expand Down Expand Up @@ -186,19 +198,23 @@ const Menu: React.FC<{
}
};

const sendEmail = () => {
const sendEmail = async () => {
if (isPlatform("hybrid")) {
const content = AppGeneral.getCurrentHTMLContent();
const base64 = btoa(content);
EmailComposer.open({
to: ["jackdwell08@gmail.com"],
cc: [],
bcc: [],
body: "PFA",
attachments: [{ type: "base64", path: base64, name: "Invoice.html" }],
subject: `${APP_NAME} attached`,
isHtml: true,
});
try {
await EmailComposer.open({
to: ["jackdwell08@gmail.com"],
cc: [],
bcc: [],
body: "PFA",
attachments: [{ type: "base64", path: base64, name: "Invoice.html" }],
subject: `${APP_NAME} attached`,
isHtml: true,
});
} catch (err) {
console.error("Email send failed:", err);
}
} else {
alert("This Functionality works on Anroid/IOS devices");
}
Expand Down
4 changes: 3 additions & 1 deletion src/components/Modals/BackupSuccessModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const BackupSuccessModal: React.FC<Props> = ({ isOpen, onClose, cid }) =>
const copyToClipboard = async () => {
try {
await navigator.clipboard.writeText(cid);
} catch (err) {}
} catch (err) {
console.warn("Clipboard write failed:", err);
}
};

const openGateway = () => {
Expand Down
28 changes: 16 additions & 12 deletions src/components/NewFile/NewFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ const NewFile: React.FC<{
billType: number;
}> = (props) => {
const [showAlertNewFileCreated, setShowAlertNewFileCreated] = useState(false);
const newFile = () => {
const newFile = async () => {
if (props.file !== "default") {
const content = encodeURIComponent(AppGeneral.getSpreadsheetContent());
const data = props.store._getFile(props.file);
const file = new File(
(data as any).created,
new Date().toString(),
content,
props.file,
props.billType
);
props.store._saveFile(file);
props.updateSelectedFile(props.file);
try {
const content = encodeURIComponent(AppGeneral.getSpreadsheetContent());
const data: any = await props.store._getFile(props.file);
const file = new File(
data.created,
new Date().toString(),
content,
props.file,
props.billType
);
await props.store._saveFile(file);
props.updateSelectedFile(props.file);
} catch (err) {
console.error("Failed to save current file before creating new:", err);
}
}
const msc = DATA["home"][AppGeneral.getDeviceType()]["msc"];
AppGeneral.viewFile("default", JSON.stringify(msc));
Expand Down