Skip to content
Merged
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
13 changes: 4 additions & 9 deletions src/homeworks/homework-10/AccountService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});

Expand Down
4 changes: 2 additions & 2 deletions src/homeworks/homework-10/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Database implements IDatabase {
save<T>(key: string, value: T): Promise<boolean> {
return new Promise((resolve) =>
setTimeout(() => {
resolve(!!this.data.set(key, value));
resolve(!!this.data.set(key, value));
}, 500)
);
}
Expand All @@ -30,4 +30,4 @@ export class Database implements IDatabase {
}, 500)
);
}
}
}
23 changes: 11 additions & 12 deletions src/homeworks/homework-10/Entities.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
export enum User {
Standard = 'Standard',
Premium = 'Premium',
Gold = 'Gold',
Free = 'Free',
}

export enum Product {
Car = 'Car',
Toy = 'Toy',
Food = 'Food',
}

Standard = 'Standard',
Premium = 'Premium',
Gold = 'Gold',
Free = 'Free',
}

export enum Product {
Car = 'Car',
Toy = 'Toy',
Food = 'Food',
}
2 changes: 1 addition & 1 deletion src/pages/ProductForm/ProductForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const ProductForm = () => {

return (
<RegularForm onSubmit={handleSubmit(onSubmit)}>
<RegularForm.Title>Товар</RegularForm.Title>
<RegularForm.Title>Товар</RegularForm.Title>
<FormInputField name="name" register={register} type="text" errors={errors.name}>
Название
</FormInputField>
Expand Down
2 changes: 1 addition & 1 deletion src/shared/collapse/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type CollapseProps = {

export const Collapse: React.FC<CollapseProps> = ({ title, children }) => {
const [isOpen, setIsOpen] = useState(false);
const { height, contentRef } = useCollapseHeight(isOpen);
const { height, contentRef } = useCollapseHeight();

const toggleCollapse = () => {
setIsOpen((prev) => !prev);
Expand Down
46 changes: 23 additions & 23 deletions src/shared/collapse/hooks/useCollapseHeight.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLDivElement>(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<HTMLDivElement>(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 };
};
};
}, []);

return { height, contentRef };
};
2 changes: 1 addition & 1 deletion src/shared/tooltip-buttons/TooltipButtons.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 2 additions & 2 deletions src/shared/tooltip/hooks/useTooltip.ts
Original file line number Diff line number Diff line change
@@ -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 });
Expand Down Expand Up @@ -56,4 +56,4 @@ export const useTooltip = (position: Position = 'bottom', duration: number = 100
handleMouseEnter,
handleMouseLeave,
};
};
};
82 changes: 41 additions & 41 deletions src/shared/tooltip/utils/tooltipPosition.ts
Original file line number Diff line number Diff line change
@@ -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<Position, (props: CoordProps) => 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,
}),
};
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<Position, (props: CoordProps) => 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,
}),
};