From cfce58a00bf469fc7792edd52ff5a78c96753870 Mon Sep 17 00:00:00 2001 From: Igor Aralov Date: Mon, 24 Feb 2025 22:48:31 +0300 Subject: [PATCH] Fix linter errors --- .../homework-10/AccountService.test.ts | 13 +-- src/homeworks/homework-10/Database.ts | 4 +- src/homeworks/homework-10/Entities.ts | 23 +++--- src/pages/ProductForm/ProductForm.tsx | 2 +- src/shared/collapse/Collapse.tsx | 2 +- .../collapse/hooks/useCollapseHeight.ts | 46 +++++------ src/shared/tooltip-buttons/TooltipButtons.tsx | 2 +- src/shared/tooltip/hooks/useTooltip.ts | 4 +- src/shared/tooltip/utils/tooltipPosition.ts | 82 +++++++++---------- 9 files changed, 86 insertions(+), 92 deletions(-) diff --git a/src/homeworks/homework-10/AccountService.test.ts b/src/homeworks/homework-10/AccountService.test.ts index 9ed718dc0..b2cc09884 100644 --- a/src/homeworks/homework-10/AccountService.test.ts +++ b/src/homeworks/homework-10/AccountService.test.ts @@ -31,18 +31,13 @@ describe('AccountService', () => { }); test('should throw table not found error', async () => { + let err; try { await accountService.userDiscount.loadDiscounts(); } catch (e) { - expect(e.message).toBe('Failed to load user_discount discounts'); - } - }); - - test('should throw table not found error', async () => { - try { - await accountService.userDiscount.loadDiscounts(); - } catch (e) { - expect(e.message).toBe('Failed to load user_discount discounts'); + err = e; + } finally { + expect(err.message).toBe('Failed to load user_discount discounts'); } }); diff --git a/src/homeworks/homework-10/Database.ts b/src/homeworks/homework-10/Database.ts index d053ce4b9..4eee64319 100644 --- a/src/homeworks/homework-10/Database.ts +++ b/src/homeworks/homework-10/Database.ts @@ -13,7 +13,7 @@ export class Database implements IDatabase { save(key: string, value: T): Promise { return new Promise((resolve) => setTimeout(() => { - resolve(!!this.data.set(key, value)); + resolve(!!this.data.set(key, value)); }, 500) ); } @@ -30,4 +30,4 @@ export class Database implements IDatabase { }, 500) ); } -} \ No newline at end of file +} diff --git a/src/homeworks/homework-10/Entities.ts b/src/homeworks/homework-10/Entities.ts index 2c06b867a..c3d92df78 100644 --- a/src/homeworks/homework-10/Entities.ts +++ b/src/homeworks/homework-10/Entities.ts @@ -1,13 +1,12 @@ export enum User { - Standard = 'Standard', - Premium = 'Premium', - Gold = 'Gold', - Free = 'Free', - } - - export enum Product { - Car = 'Car', - Toy = 'Toy', - Food = 'Food', - } - \ No newline at end of file + Standard = 'Standard', + Premium = 'Premium', + Gold = 'Gold', + Free = 'Free', +} + +export enum Product { + Car = 'Car', + Toy = 'Toy', + Food = 'Food', +} diff --git a/src/pages/ProductForm/ProductForm.tsx b/src/pages/ProductForm/ProductForm.tsx index 00bf326fd..0fe98bc6b 100644 --- a/src/pages/ProductForm/ProductForm.tsx +++ b/src/pages/ProductForm/ProductForm.tsx @@ -24,7 +24,7 @@ export const ProductForm = () => { return ( - Товар + Товар Название diff --git a/src/shared/collapse/Collapse.tsx b/src/shared/collapse/Collapse.tsx index d61e5934c..da893a0b2 100644 --- a/src/shared/collapse/Collapse.tsx +++ b/src/shared/collapse/Collapse.tsx @@ -9,7 +9,7 @@ type CollapseProps = { export const Collapse: React.FC = ({ title, children }) => { const [isOpen, setIsOpen] = useState(false); - const { height, contentRef } = useCollapseHeight(isOpen); + const { height, contentRef } = useCollapseHeight(); const toggleCollapse = () => { setIsOpen((prev) => !prev); diff --git a/src/shared/collapse/hooks/useCollapseHeight.ts b/src/shared/collapse/hooks/useCollapseHeight.ts index 7a9eec406..4c4fb237c 100644 --- a/src/shared/collapse/hooks/useCollapseHeight.ts +++ b/src/shared/collapse/hooks/useCollapseHeight.ts @@ -1,26 +1,26 @@ -import React, { useEffect, useRef, useState } from "react"; +import React, { useEffect, useRef, useState } from 'react'; -export const useCollapseHeight = (isOpen: boolean) => { - const [height, setHeight] = useState(0); - const contentRef = useRef(null); - - useEffect(() => { - const resizeObserver = new ResizeObserver((entries) => { - entries.forEach((entry) => setHeight(entry.borderBoxSize[0].blockSize)); - }); - - const currentContentRef = contentRef.current; - +export const useCollapseHeight = () => { + const [height, setHeight] = useState(0); + const contentRef = useRef(null); + + useEffect(() => { + const resizeObserver = new ResizeObserver((entries) => { + entries.forEach((entry) => setHeight(entry.borderBoxSize[0].blockSize)); + }); + + const currentContentRef = contentRef.current; + + if (currentContentRef) { + resizeObserver.observe(currentContentRef); + } + + return () => { if (currentContentRef) { - resizeObserver.observe(currentContentRef); + resizeObserver.unobserve(currentContentRef); } - - return () => { - if (currentContentRef) { - resizeObserver.unobserve(currentContentRef); - } - }; - }, []); - - return { height, contentRef }; - }; \ No newline at end of file + }; + }, []); + + return { height, contentRef }; +}; diff --git a/src/shared/tooltip-buttons/TooltipButtons.tsx b/src/shared/tooltip-buttons/TooltipButtons.tsx index 26e3deaa4..87021d8fb 100644 --- a/src/shared/tooltip-buttons/TooltipButtons.tsx +++ b/src/shared/tooltip-buttons/TooltipButtons.tsx @@ -1,6 +1,6 @@ import React, { ReactNode } from 'react'; import { Position } from '../tooltip/utils/tooltipPosition'; -import {Tooltip} from "../tooltip/Tooltip"; +import { Tooltip } from '../tooltip/Tooltip'; import { Button } from '../button/Button'; import s from './TooltipButtons.module.scss'; diff --git a/src/shared/tooltip/hooks/useTooltip.ts b/src/shared/tooltip/hooks/useTooltip.ts index 0304c6fd8..f814037a7 100644 --- a/src/shared/tooltip/hooks/useTooltip.ts +++ b/src/shared/tooltip/hooks/useTooltip.ts @@ -1,7 +1,7 @@ import { useState, useRef, useLayoutEffect } from 'react'; import { positionMap, Position } from '../utils/tooltipPosition'; -export const useTooltip = (position: Position = 'bottom', duration: number = 1000) => { +export const useTooltip = (position: Position = 'bottom', duration = 1000) => { const [visible, setVisible] = useState(false); const [mounted, setMounted] = useState(false); const [coords, setCoords] = useState({ top: 0, left: 0 }); @@ -56,4 +56,4 @@ export const useTooltip = (position: Position = 'bottom', duration: number = 100 handleMouseEnter, handleMouseLeave, }; -}; \ No newline at end of file +}; diff --git a/src/shared/tooltip/utils/tooltipPosition.ts b/src/shared/tooltip/utils/tooltipPosition.ts index c71520b60..870e76073 100644 --- a/src/shared/tooltip/utils/tooltipPosition.ts +++ b/src/shared/tooltip/utils/tooltipPosition.ts @@ -1,42 +1,42 @@ type Coords = { - top: number; - left: number; - }; - - type CoordProps = { - targetRect: DOMRect; - tooltipRect: DOMRect; - offset: number; - }; - - export type Position = 'top' | 'bottom' | 'left' | 'right'; - - const getCenterCoord = (primary: number, secondary: number) => (primary - secondary) / 2; - - const YLeft = (primary: DOMRect, secondary: DOMRect) => - primary.left + window.scrollX + getCenterCoord(primary.width, secondary.width); - - const XTop = (primary: DOMRect, secondary: DOMRect) => - primary.top + window.scrollY + getCenterCoord(primary.height, secondary.height); - - export const positionMap: Record Coords> = { - top: ({ targetRect, tooltipRect, offset }) => ({ - top: targetRect.top + window.scrollY - tooltipRect.height - offset, - left: targetRect.left + window.scrollX + getCenterCoord(targetRect.width, tooltipRect.width), - }), - - bottom: ({ targetRect, tooltipRect, offset }) => ({ - top: targetRect.bottom + window.scrollY + offset, - left: YLeft(targetRect, tooltipRect), - }), - - left: ({ targetRect, tooltipRect, offset }) => ({ - top: XTop(targetRect, tooltipRect), - left: targetRect.left + window.scrollX - tooltipRect.width - offset, - }), - - right: ({ targetRect, tooltipRect, offset }) => ({ - top: XTop(targetRect, tooltipRect), - left: targetRect.left + window.scrollX + targetRect.width + offset, - }), - }; \ No newline at end of file + top: number; + left: number; +}; + +type CoordProps = { + targetRect: DOMRect; + tooltipRect: DOMRect; + offset: number; +}; + +export type Position = 'top' | 'bottom' | 'left' | 'right'; + +const getCenterCoord = (primary: number, secondary: number) => (primary - secondary) / 2; + +const YLeft = (primary: DOMRect, secondary: DOMRect) => + primary.left + window.scrollX + getCenterCoord(primary.width, secondary.width); + +const XTop = (primary: DOMRect, secondary: DOMRect) => + primary.top + window.scrollY + getCenterCoord(primary.height, secondary.height); + +export const positionMap: Record Coords> = { + top: ({ targetRect, tooltipRect, offset }) => ({ + top: targetRect.top + window.scrollY - tooltipRect.height - offset, + left: targetRect.left + window.scrollX + getCenterCoord(targetRect.width, tooltipRect.width), + }), + + bottom: ({ targetRect, tooltipRect, offset }) => ({ + top: targetRect.bottom + window.scrollY + offset, + left: YLeft(targetRect, tooltipRect), + }), + + left: ({ targetRect, tooltipRect, offset }) => ({ + top: XTop(targetRect, tooltipRect), + left: targetRect.left + window.scrollX - tooltipRect.width - offset, + }), + + right: ({ targetRect, tooltipRect, offset }) => ({ + top: XTop(targetRect, tooltipRect), + left: targetRect.left + window.scrollX + targetRect.width + offset, + }), +};