-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Rue-pro/core-add-create-read-errors-compo…
…nent Core add create read errors component
- Loading branch information
Showing
6 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ui' | ||
export * from './model' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as errorStore from './store' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ErrorList } from './ErrorList' |