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
52 changes: 20 additions & 32 deletions Govt-Billing-React/src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Menu: React.FC<{
const [showAlert4, setShowAlert4] = useState(false);
const [showToast1, setShowToast1] = useState(false);
const [toastMessage, setToastMessage] = useState("");
/* Utility functions */

const _validateName = async (filename) => {
filename = filename.trim();
if (filename === "default" || filename === "Untitled") {
Expand All @@ -49,7 +49,6 @@ const Menu: React.FC<{
};

const _formatString = (filename) => {
/* Remove whitespaces */
while (filename.indexOf(" ") !== -1) {
filename = filename.replace(" ", "");
}
Expand All @@ -62,48 +61,50 @@ const Menu: React.FC<{
printer.print(AppGeneral.getCurrentHTMLContent());
} else {
const content = AppGeneral.getCurrentHTMLContent();
// useReactToPrint({ content: () => content });
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 existingData = await props.store._getFile(props.file);


const createdTimestamp =
existingData?.created ?? new Date().toString();

const file = new File(
(data as any).created,
new Date().toString(),
createdTimestamp,
new Date().toString(),
content,
props.file,
props.bT
);

props.store._saveFile(file);
props.updateSelectedFile(props.file);
setShowAlert2(true);
};

const doSaveAs = async (filename) => {
// event.preventDefault();
if (filename) {
// console.log(filename, _validateName(filename));
if (await _validateName(filename)) {
// filename valid . go on save
const content = encodeURIComponent(AppGeneral.getSpreadsheetContent());
// console.log(content);
const file = new File(
new Date().toString(),
new Date().toString(),
content,
filename,
props.bT
);
// const data = { created: file.created, modified: file.modified, content: file.content, password: file.password };
// console.log(JSON.stringify(data));
props.store._saveFile(file);
props.updateSelectedFile(filename);
setShowAlert4(true);
Expand All @@ -117,7 +118,6 @@ const Menu: React.FC<{
if (isPlatform("hybrid")) {
const content = AppGeneral.getCurrentHTMLContent();
const base64 = btoa(content);

EmailComposer.open({
to: ["jackdwell08@gmail.com"],
cc: [],
Expand All @@ -128,7 +128,7 @@ const Menu: React.FC<{
isHtml: true,
});
} else {
alert("This Functionality works on Anroid/IOS devices");
alert("This Functionality works on Android/iOS devices");
}
};

Expand Down Expand Up @@ -179,31 +179,23 @@ const Menu: React.FC<{
isOpen={showAlert1}
onDidDismiss={() => setShowAlert1(false)}
header="Alert Message"
message={
"Cannot update <strong>" + getCurrentFileName() + "</strong> file!"
}
message={"Cannot update <strong>" + getCurrentFileName() + "</strong> file!"}
buttons={["Ok"]}
/>
<IonAlert
animated
isOpen={showAlert2}
onDidDismiss={() => setShowAlert2(false)}
header="Save"
message={
"File <strong>" +
getCurrentFileName() +
"</strong> updated successfully"
}
message={"File <strong>" + getCurrentFileName() + "</strong> updated successfully"}
buttons={["Ok"]}
/>
<IonAlert
animated
isOpen={showAlert3}
onDidDismiss={() => setShowAlert3(false)}
header="Save As"
inputs={[
{ name: "filename", type: "text", placeholder: "Enter filename" },
]}
inputs={[{ name: "filename", type: "text", placeholder: "Enter filename" }]}
buttons={[
{
text: "Ok",
Expand All @@ -218,11 +210,7 @@ const Menu: React.FC<{
isOpen={showAlert4}
onDidDismiss={() => setShowAlert4(false)}
header="Save As"
message={
"File <strong>" +
getCurrentFileName() +
"</strong> saved successfully"
}
message={"File <strong>" + getCurrentFileName() + "</strong> saved successfully"}
buttons={["Ok"]}
/>
<IonToast
Expand All @@ -240,4 +228,4 @@ const Menu: React.FC<{
);
};

export default Menu;
export default Menu;
36 changes: 23 additions & 13 deletions Govt-Billing-React/src/components/NewFile/NewFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,31 @@ 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);

const existingData = await props.store._getFile(props.file);

if (existingData === null) {
console.warn(
`[NewFile] _getFile returned null for "${props.file}". Skipping persist.`
);
} else {
const file = new File(
existingData.created,
new Date().toString(),
content,
props.file,
props.billType
);

await props.store._saveFile(file);
props.updateSelectedFile(props.file);
}
}

const msc = DATA["home"][AppGeneral.getDeviceType()]["msc"];
AppGeneral.viewFile("default", JSON.stringify(msc));
props.updateSelectedFile("default");
Expand All @@ -41,7 +52,6 @@ const NewFile: React.FC<{
size="large"
onClick={() => {
newFile();
// console.log("New file clicked");
}}
/>
<IonAlert
Expand All @@ -56,4 +66,4 @@ const NewFile: React.FC<{
);
};

export default NewFile;
export default NewFile;
47 changes: 30 additions & 17 deletions Govt-Billing-React/src/components/Storage/LocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export class File {
}

export class Local {
_saveFile = async (file: File) => {
let data = {
_saveFile = async (file: File): Promise<void> => {
const data = {
created: file.created,
modified: file.modified,
content: file.content,
Expand All @@ -37,32 +37,45 @@ export class Local {
});
};

_getFile = async (name: string) => {
const rawData = await Preferences.get({ key: name });
return JSON.parse(rawData.value);
/**
* Reads a stored FileObject by key.
*
* @returns The parsed FileObject, or `null` if the key does not exist
* or the stored value cannot be parsed. Callers MUST `await`
* this method before accessing any properties on the result.
*/
_getFile = async (name: string): Promise<File | null> => {
try {
const rawData = await Preferences.get({ key: name });
if (rawData.value === null || rawData.value === undefined) {
return null;
}
return JSON.parse(rawData.value) as File;
} catch (err) {
console.error(`[LocalStorage] _getFile failed for key "${name}":`, err);
return null;
}
};

_getAllFiles = async () => {
let arr = {};
_getAllFiles = async (): Promise<Record<string, string>> => {
const arr: Record<string, string> = {};
const { keys } = await Preferences.keys();
for (let i = 0; i < keys.length; i++) {
let fname = keys[i];
const fname = keys[i];
const data = await this._getFile(fname);
arr[fname] = (data as any).modified;
if (data !== null) {
arr[fname] = data.modified;
}
}
return arr;
};

_deleteFile = async (name: string) => {
_deleteFile = async (name: string): Promise<void> => {
await Preferences.remove({ key: name });
};

_checkKey = async (key: string) => {
_checkKey = async (key: string): Promise<boolean> => {
const { keys } = await Preferences.keys();
if (keys.includes(key, 0)) {
return true;
} else {
return false;
}
return keys.includes(key);
};
}
}