Manual formating and cleanups

This commit is contained in:
Michał 2024-05-04 16:52:21 +01:00
parent 31a31a43e0
commit 42951bab20
3 changed files with 15 additions and 22 deletions

View file

@ -25,18 +25,14 @@ function createCartStore() {
async function addToCart(uuid: string, amount: number) { async function addToCart(uuid: string, amount: number) {
if (get(cart)[uuid] !== undefined) { if (get(cart)[uuid] !== undefined) {
cart.update((cart: Record<string, CartItem>) => { cart.update((cart: Record<string, CartItem>) => {
cart[uuid].amount += amount; cart[uuid].amount += amount; // skipcq: JS-0320
return cart; return cart;
}); });
} else { } else {
await getItemByUUID(uuid).then((data: Item) => { await getItemByUUID(uuid).then((data: Item) => {
cart.update((cart: Record<string, CartItem>) => cart.update((cart: Record<string, CartItem>) =>
Object.assign({}, cart, { Object.assign({}, cart, {
[uuid]: { [uuid]: {uuid, amount, data},
uuid: uuid,
amount: amount,
data: data,
},
}) })
); );
}); });
@ -44,7 +40,7 @@ function createCartStore() {
cart.update((cart: Record<string, CartItem>) => { cart.update((cart: Record<string, CartItem>) => {
if (cart[uuid].amount <= 0) { if (cart[uuid].amount <= 0) {
delete cart[uuid]; delete cart[uuid]; // skipcq: JS-0320
} }
return cart; return cart;
}); });
@ -59,7 +55,7 @@ function createCartStore() {
} }
function getTotalLength(): number { function getTotalLength(): number {
let totalCartSize: number = 0; let totalCartSize = 0;
Object.values(get(cart)).forEach((item: CartItem) => { Object.values(get(cart)).forEach((item: CartItem) => {
totalCartSize += item.amount; totalCartSize += item.amount;
}); });

View file

@ -1,11 +1,9 @@
import { type CartItem, type Item } from "./types"; import { type CartItem, type Item } from "./types";
import TestData from "./test-data"; import TestData from "./test-data";
let cache: Record<string, any> = {}; const cache = {};
// @ts-ignore async function fakeDelay(timeout = 1000) {
async function fakeDelay(timeout: number = 1000) {
// @ts-ignore
await new Promise((resolve: TimerHandler) => setTimeout(resolve, timeout)); await new Promise((resolve: TimerHandler) => setTimeout(resolve, timeout));
} }
@ -58,7 +56,7 @@ export async function getMenuItems(): Promise<
export async function getItemsByUUID(items: string[]): Promise<Item[]> { export async function getItemsByUUID(items: string[]): Promise<Item[]> {
await fakeDelay(200); await fakeDelay(200);
let data: Item[] = []; const data: Item[] = [];
TestData.forEach((itemInDatabase: Item) => { TestData.forEach((itemInDatabase: Item) => {
items.forEach((itemInRequest) => { items.forEach((itemInRequest) => {
@ -68,7 +66,7 @@ export async function getItemsByUUID(items: string[]): Promise<Item[]> {
}); });
}); });
if (data.length < 0) { if (data.length === 0) {
throw new Error("Resource could not be found"); throw new Error("Resource could not be found");
} }
@ -76,15 +74,14 @@ export async function getItemsByUUID(items: string[]): Promise<Item[]> {
} }
export async function getItemByUUID(uuid: string): Promise<Item> { export async function getItemByUUID(uuid: string): Promise<Item> {
let data: Item[]; const data: Item[] = [];
await getItemsByUUID([uuid]) await getItemsByUUID([uuid])
.then((result) => { .then((result) => {
if (result.length != 1) { if (result.length !== 1) {
throw new Error("Resource could not be found"); throw new Error("Resource could not be found");
} else {
data = result;
} }
data.push(...result);
}) })
.catch((error) => { .catch((error) => {
throw error; throw error;
@ -118,17 +115,17 @@ export async function postContactEmail(
export async function postVerifyCart( export async function postVerifyCart(
currentCartData: Record<string, CartItem> currentCartData: Record<string, CartItem>
): Promise<Record<string, CartItem>> { ): Promise<Record<string, CartItem>> {
let verifiedItems: Item[] = []; const verifiedItems: Item[] = [];
await getItemsByUUID(Object.keys(currentCartData)) await getItemsByUUID(Object.keys(currentCartData))
.then((data) => { .then((data) => {
verifiedItems = data; verifiedItems.push(...data);
}) })
.catch(() => { .catch(() => {
return new Error("Could not collect new cart information"); return new Error("Could not collect new cart information");
}); });
let newCartData: Record<string, CartItem> = {}; const newCartData: Record<string, CartItem> = {};
Object.entries(currentCartData).forEach(([key, value]) => { Object.entries(currentCartData).forEach(([key, value]) => {
verifiedItems.forEach((verifiedItem: Item) => { verifiedItems.forEach((verifiedItem: Item) => {

View file

@ -1,6 +1,6 @@
export function expandOnTyping(element) { export function expandOnTyping(element) {
element.oninput = (event) => { element.oninput = (event) => {
event.target.style.height = ""; event.target.style.height = "";
event.target.style.height = event.target.scrollHeight + 2 + "px"; event.target.style.height = `${event.target.scrollHeight + 2}px`;
}; };
} }