Skip to content

Commit

Permalink
Merge pull request #17 from Rue-pro/core-add-create-read-errors-compo…
Browse files Browse the repository at this point in the history
…nent

Core add create read errors component
  • Loading branch information
Rue-pro authored Apr 8, 2024
2 parents 61fe9e0 + d17787a commit 5675461
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/entities/error/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ui'
export * from './model'
1 change: 1 addition & 0 deletions src/entities/error/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as errorStore from './store'
36 changes: 36 additions & 0 deletions src/entities/error/model/store.ts
Original file line number Diff line number Diff line change
@@ -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<TBaseError[]>('errors', [])

export const $errors = atom<TBaseError[]>([])

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])
}
13 changes: 13 additions & 0 deletions src/entities/error/ui/ErrorCard/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { TBaseError } from '@shared/libs/operationResult'

interface Props {
error: TBaseError
}

export const ErrorCard = ({ error }: Props) => {
return (
<div>
Error {error.type} {error.error}
</div>
)
}
19 changes: 19 additions & 0 deletions src/entities/error/ui/ErrorList/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<ul>
<li>
{errors.map((error) => (
<ErrorCard error={error} />
))}
</li>
</ul>
)
}
1 change: 1 addition & 0 deletions src/entities/error/ui/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ErrorList } from './ErrorList'

0 comments on commit 5675461

Please sign in to comment.