diff --git a/src/entities/error/index.ts b/src/entities/error/index.ts new file mode 100644 index 0000000..1ccc9c5 --- /dev/null +++ b/src/entities/error/index.ts @@ -0,0 +1,2 @@ +export * from './ui' +export * from './model' diff --git a/src/entities/error/model/index.ts b/src/entities/error/model/index.ts new file mode 100644 index 0000000..bac3431 --- /dev/null +++ b/src/entities/error/model/index.ts @@ -0,0 +1 @@ +export * as errorStore from './store' diff --git a/src/entities/error/model/store.ts b/src/entities/error/model/store.ts new file mode 100644 index 0000000..02a52c6 --- /dev/null +++ b/src/entities/error/model/store.ts @@ -0,0 +1,36 @@ +import { atom, onMount, task } from 'nanostores' + +import { getStorage } from '@entities/storage' + +import { TBaseError } from '@shared/libs/operationResult' + +export const ErrorStorage = getStorage('errors', []) + +export const $errors = atom([]) + +onMount($errors, () => { + task(async () => { + const getResult = await ErrorStorage.get() + getResult.data ? $errors.set([]) : $errors.set([getResult.error]) + }) + + const listener = ErrorStorage.onChanged.addListener((changes) => { + changes.data + ? $errors.set(changes.data.newValue) + : $errors.set([changes.error, ...$errors.get()]) + }) + + return () => { + ErrorStorage.onChanged.removeListener(listener) + } +}) + +export const addError = async (error: TBaseError) => { + const errors = $errors.get() + const newErrors = [...errors, error] + + const setResult = await ErrorStorage.set(newErrors) + setResult.data + ? $errors.set(newErrors) + : $errors.set([setResult.error, ...newErrors]) +} diff --git a/src/entities/error/ui/ErrorCard/index.tsx b/src/entities/error/ui/ErrorCard/index.tsx new file mode 100644 index 0000000..b3254b5 --- /dev/null +++ b/src/entities/error/ui/ErrorCard/index.tsx @@ -0,0 +1,13 @@ +import { TBaseError } from '@shared/libs/operationResult' + +interface Props { + error: TBaseError +} + +export const ErrorCard = ({ error }: Props) => { + return ( +
+ Error {error.type} {error.error} +
+ ) +} diff --git a/src/entities/error/ui/ErrorList/index.tsx b/src/entities/error/ui/ErrorList/index.tsx new file mode 100644 index 0000000..e861511 --- /dev/null +++ b/src/entities/error/ui/ErrorList/index.tsx @@ -0,0 +1,19 @@ +import { useStore } from '@nanostores/preact' + +import { $errors } from '@entities/error/model/store' + +import { ErrorCard } from '../ErrorCard' + +export const ErrorList = () => { + const errors = useStore($errors) + + return ( + + ) +} diff --git a/src/entities/error/ui/index.ts b/src/entities/error/ui/index.ts new file mode 100644 index 0000000..0acb133 --- /dev/null +++ b/src/entities/error/ui/index.ts @@ -0,0 +1 @@ +export { ErrorList } from './ErrorList'