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
46 changes: 24 additions & 22 deletions Govt-Billing-React/src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ const Menu: React.FC<{
return props.file;
};

const _formatString = (filename) => {
/* Remove whitespaces */
while (filename.indexOf(" ") !== -1) {
filename = filename.replace(" ", "");
}
return filename;
};

const doPrint = () => {
if (isPlatform("hybrid")) {
const printer = Printer;
Expand All @@ -68,23 +60,33 @@ const Menu: React.FC<{
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 = await props.store._getFile(props.file);
if (!data) {
setToastMessage("Unable to load saved metadata for this file.");
setShowToast1(true);
return;
}
const file = new File(
(data as any).created,
new Date().toString(),
content,
props.file,
props.bT
);
await props.store._saveFile(file);
props.updateSelectedFile(props.file);
setShowAlert2(true);
} catch {
setToastMessage("Unable to save file. Please try again.");
setShowToast1(true);
}
};

const doSaveAs = async (filename) => {
Expand All @@ -104,7 +106,7 @@ const Menu: React.FC<{
);
// const data = { created: file.created, modified: file.modified, content: file.content, password: file.password };
// console.log(JSON.stringify(data));
props.store._saveFile(file);
await props.store._saveFile(file);
props.updateSelectedFile(filename);
setShowAlert4(true);
} else {
Expand Down
34 changes: 21 additions & 13 deletions Govt-Billing-React/src/components/NewFile/NewFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@ 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 = await props.store._getFile(props.file);
if (data) {
const file = new File(
(data as any).created,
new Date().toString(),
content,
props.file,
props.billType
);
await props.store._saveFile(file);
props.updateSelectedFile(props.file);
} else {
alert("Current file metadata was not found. Changes were not saved.");
}
} catch {
alert("Unable to save current file before creating a new one.");
}
}
const msc = DATA["home"][AppGeneral.getDeviceType()]["msc"];
AppGeneral.viewFile("default", JSON.stringify(msc));
Expand All @@ -40,7 +48,7 @@ const NewFile: React.FC<{
className="ion-padding-end"
size="large"
onClick={() => {
newFile();
void newFile();
// console.log("New file clicked");
}}
/>
Expand Down
13 changes: 11 additions & 2 deletions Govt-Billing-React/src/components/Storage/LocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ export class Local {

_getFile = async (name: string) => {
const rawData = await Preferences.get({ key: name });
return JSON.parse(rawData.value);
if (!rawData.value) {
return null;
}
try {
return JSON.parse(rawData.value);
} catch {
return null;
}
};

_getAllFiles = async () => {
Expand All @@ -48,7 +55,9 @@ export class Local {
for (let i = 0; i < keys.length; i++) {
let fname = keys[i];
const data = await this._getFile(fname);
arr[fname] = (data as any).modified;
if (data) {
arr[fname] = (data as any).modified;
}
}
return arr;
};
Expand Down