Skip to content

Commit

Permalink
create the localstorage utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
olasunkanmi-SE committed Jun 10, 2023
1 parent 98126c2 commit 74dd407
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
24 changes: 24 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@hookform/resolvers": "^2.9.11",
"axios": "^1.3.2",
"bootstrap": "^5.2.3",
"crypto-js": "^4.1.1",
"lodash.get": "4.4.2",
"react": "^18.2.0",
"react-bootstrap": "^2.7.0",
Expand All @@ -30,6 +31,7 @@
"zod": "^3.21.4"
},
"devDependencies": {
"@types/crypto-js": "^4.1.1",
"@types/lodash.get": "^4.4.7",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/contexts/shoppingCartContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export const ShoppingCartProvider = ({ children }: shoppingCartProviderProps) =>
}
let { menus, quantity, orderSummary } = state;
const orderInfo: OrderSummary = {
id: uuidv4(),
id: (Math.floor(Math.random() * 1000000) + 1).toString(),
menus,
quantity,
};
Expand Down Expand Up @@ -354,7 +354,7 @@ export const ShoppingCartProvider = ({ children }: shoppingCartProviderProps) =>
const orderMenus = orderSummary.map((menu) => menu.menus).flat();
const menu = orderMenus.find((menu) => menu.id === menuId);
if (menu) {
menu.quantity = menu.quantity ? (menu.quantity += 1) : menu.quantity;
menu.quantity = menu.quantity ? (menu.quantity = menu.quantity + 1) : menu.quantity;
}
}
dispatch({
Expand All @@ -368,7 +368,7 @@ export const ShoppingCartProvider = ({ children }: shoppingCartProviderProps) =>
const orderMenus = orderSummary.map((menu) => menu.menus).flat();
const menu = orderMenus.find((menu) => menu.id === menuId);
if (menu) {
menu.quantity = menu.quantity ? (menu.quantity -= 1) : menu.quantity;
menu.quantity = menu.quantity ? (menu.quantity = menu.quantity - 1) : menu.quantity;
if (menu.quantity === 0) {
const index = orderSummary.findIndex((order) => order.id === orderSummaryId);
if (index > -1) {
Expand Down
33 changes: 33 additions & 0 deletions frontend/src/utility/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useShoppingCart } from "../hooks/UseShoppingCart";
import cryptoJs from "crypto-js";

const currencyFormatter = new Intl.NumberFormat(undefined, {
currency: "NGR",
Expand Down Expand Up @@ -27,3 +28,35 @@ export const calculateTotalOrderAmount = (): number => {
const total = orderSummary ? orderSummary.reduce((acc, order) => acc + order.menus[0].menuTotalPrice!, 0) : 0;
return total > 0 ? total : 0;
};

export const setLocalStorageData = (key: string, value: string, encrypt: boolean) => {
if (encrypt) {
try {
const encryptedText = cryptoJs.AES.encrypt(value, import.meta.env.VITE_SECRET);
if (encryptedText) {
localStorage.setItem(key, encryptedText.toString());
}
} catch (error) {
console.log("Error while saving user Data", error);
}
} else {
localStorage.setItem(key, value);
}
};

export const getLocalStorageData = (key: string, decrypt: boolean) => {
let value = localStorage.getItem(key);
if (value && decrypt) {
try {
const decryptedText = cryptoJs.AES.decrypt(value, import.meta.env.VITE_SECRET);
return decryptedText.toString(cryptoJs.enc.Utf8);
} catch (error) {
console.log("Error while getting user data", error);
}
}
return value;
};

export const clearStorage = () => {
localStorage.clear();
};

0 comments on commit 74dd407

Please sign in to comment.