Skip to content

Commit

Permalink
feat: add functionality to clear all data from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
thedemonsid committed Jan 1, 2025
1 parent 4998867 commit f895dde
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 84 deletions.
14 changes: 14 additions & 0 deletions src/electron/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ class RestaurantDB {
);
return response;
}
//! Clear all data
public clearData() {
try {
this.db.exec("DELETE FROM MenuItem");
this.db.exec("DELETE FROM Expenses");
this.db.exec("DELETE FROM 'Order'");
this.db.exec("DELETE FROM OrderMenuItem");
return true;
} catch (error) {
console.error("Failed to clear data:", error);
return false;
}
}

//! Close the database connection
public close(): void {
this.db.close();
Expand Down
Binary file added src/electron/database/restaurant.db-shm
Binary file not shown.
Binary file added src/electron/database/restaurant.db-wal
Binary file not shown.
10 changes: 10 additions & 0 deletions src/electron/ipcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,13 @@ ipcMain.handle("restaurant:update-info", async (event, info) => {
throw error;
}
});

//! Clear all the Data
ipcMain.handle("db:clear-all-data", async () => {
try {
return await RestaurantDB.clearData();
} catch (error) {
console.error("Failed to clear all data:", error);
throw error;
}
});
3 changes: 3 additions & 0 deletions src/electron/preload.cts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ contextBridge.exposeInMainWorld("restaurant", {
updateRestaurantInfo: (info: any) =>
ipcRenderer.invoke("restaurant:update-info", info),
},
db:{
clearData: (): Promise<boolean> => ipcRenderer.invoke("db:clear-all-data"),
}
});
208 changes: 124 additions & 84 deletions src/web/pages/settings.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Setting page to change restaurant name, address, and phone

import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { toast } from "@/hooks/use-toast";
Expand All @@ -21,96 +22,135 @@ export default function Settings() {
fetchRestaurantInfo();
}, []);
return (
<div className="p-4">
<h1 className="text-2xl font-bold mb-4">Settings</h1>
<form
onSubmit={async () => {
// first check for changes
<div className="p-4 flex flex-col gap-4 justify-between h-full">
<div>
{" "}
<h1 className="text-2xl font-bold mb-4">Settings</h1>
<form
onSubmit={async () => {
// first check for changes

const response =
await window.restaurant.restaurant.updateRestaurantInfo(
restaurantInfo
);
console.log(response);
const response =
await window.restaurant.restaurant.updateRestaurantInfo(
restaurantInfo
);
console.log(response);

if (response) {
console.log("Restaurant info updated successfully");
} else {
console.log("Failed to update restaurant info");
}
}}
>
<div className="grid grid-cols-4 gap-4">
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="name" className="text-right">
Restaurant name
</Label>
<Input
id="name"
value={restaurantInfo.name}
className="col-span-3"
onChange={(e) => {
setRestaurantInfo((prev) => ({
...prev,
name: e.target.value,
}));
}}
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="address" className="text-right">
Address
</Label>
<Input
id="address"
value={restaurantInfo.address}
className="col-span-3"
onChange={(e) => {
setRestaurantInfo((prev) => ({
...prev,
address: e.target.value,
}));
}}
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="phone" className="text-right">
Phone
</Label>
<Input
id="phone"
value={restaurantInfo.phone.join(",")}
className="col-span-3"
onChange={(e) => {
setRestaurantInfo((prev) => {
return {
if (response) {
console.log("Restaurant info updated successfully");
} else {
console.log("Failed to update restaurant info");
}
}}
>
<div className="grid grid-cols-4 gap-4">
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="name" className="text-right">
Restaurant name
</Label>
<Input
id="name"
value={restaurantInfo.name}
className="col-span-3"
onChange={(e) => {
setRestaurantInfo((prev) => ({
...prev,
phone: e.target.value.split(","),
};
name: e.target.value,
}));
}}
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="address" className="text-right">
Address
</Label>
<Input
id="address"
value={restaurantInfo.address}
className="col-span-3"
onChange={(e) => {
setRestaurantInfo((prev) => ({
...prev,
address: e.target.value,
}));
}}
/>
</div>
<div className="grid grid-cols-4 items-center gap-4">
<Label htmlFor="phone" className="text-right">
Phone
</Label>
<Input
id="phone"
value={restaurantInfo.phone.join(",")}
className="col-span-3"
onChange={(e) => {
setRestaurantInfo((prev) => {
return {
...prev,
phone: e.target.value.split(","),
};
});
}}
/>
</div>
</div>
<div className="mt-4">
<Button
type="submit"
onClick={() => {
toast({
title: "Settings saved",
description: `New restaurant name: ${
restaurantInfo.name
}, address: ${
restaurantInfo.address
}, phone: ${restaurantInfo.phone.join(", ")}`,
className: "bg-green-100",
});
}}
/>
>
Save
</Button>
</div>
</form>
</div>
<Dialog>
<DialogTrigger>
<Button variant={"secondary"}>Clear All Data</Button>
</DialogTrigger>
<DialogContent>
<div className="p-4">
<h2 className="text-2xl font-bold">Delete All Data</h2>
<p className="text-red-500">
This will delete all data including menu items, orders, and
expenses
</p>
<div className="mt-4">
<Button
onClick={async () => {
const response = await window.restaurant.db.clearData();
if (response) {
toast({
title: "Data cleared",
description: "All data has been deleted",
className: "bg-red-100",
});
} else {
toast({
title: "Failed to clear data",
description: "Please try again",
variant: "destructive",
});
}
}}
>
Clear Data
</Button>
</div>
</div>
</div>
<div className="mt-4">
<Button
type="submit"
onClick={() => {
toast({
title: "Settings saved",
description: `New restaurant name: ${
restaurantInfo.name
}, address: ${
restaurantInfo.address
}, phone: ${restaurantInfo.phone.join(", ")}`,
className: "bg-green-100",
});
}}
>
Save
</Button>
</div>
</form>
</DialogContent>
</Dialog>
</div>
);
}
3 changes: 3 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ interface Window {
phone: string[];
}) => Promise<boolean>;
};
db: {
clearData: () => Promise<boolean>;
};
};
}

0 comments on commit f895dde

Please sign in to comment.