Skip to content

Commit

Permalink
chore: remove toast usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Rue-pro committed Apr 13, 2024
1 parent 48d42bc commit b4d0cda
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 50 deletions.
9 changes: 0 additions & 9 deletions src/__tests__/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import { vi } from 'vitest'

import * as toastModule from '@popup/shared/ui/Toast'
import { getErrorToastMock } from '@popup/shared/ui/Toast/helpers/__mock__/getErrorToast'
import { addToastMock } from '@popup/shared/ui/Toast/model/__mock__/store'

import { chromeMock } from '@shared/shared/browser/__mocks__/chrome'

global.chrome = chromeMock

vi.spyOn(toastModule, 'getErrorToast').mockImplementation(getErrorToastMock)
vi.spyOn(toastModule, 'addToast').mockImplementation(addToastMock)
4 changes: 2 additions & 2 deletions src/popup/app/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { NoteList } from '@popup/widgets/note/NoteList'
import { Settings } from '@popup/widgets/settings'

import { Toasts } from '@popup/shared/ui/Toast'
import { ErrorAlert } from '@popup/entities/error'

export const Popup = () => {
return (
<main>
<ErrorAlert />
<Settings />
<NoteList />
<Toasts />
</main>
)
}
7 changes: 1 addition & 6 deletions src/popup/entities/tab/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { atom, onMount, task } from 'nanostores'

import { browser } from '@popup/shared/browser'
import { addToast, getErrorToast } from '@popup/shared/ui/Toast'

import { TTab } from '@shared/shared/browser/tabs'

Expand All @@ -11,10 +10,6 @@ onMount($activeTab, () => {
task(async () => {
const getResult = await browser.tabs.getActiveTab()

if (getResult.data) {
$activeTab.set(getResult.data)
} else {
addToast(getErrorToast(getResult.error))
}
getResult.data && $activeTab.set(getResult.data)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DictionaryCard } from '@popup/entities/dictionary'

import { browser } from '@popup/shared/browser'
import { Button } from '@popup/shared/ui/Button'
import { addToast } from '@popup/shared/ui/Toast'

import {
IDictionaryWithVariants,
Expand All @@ -18,14 +17,8 @@ interface GroupProps {
}

export const SelectDictionaryVariant = ({ dictionary, lang }: GroupProps) => {
const onSelectVariant = async (variant: string) => {
const result = await dictionaryStore.selectVariant(
lang,
dictionary.id,
variant,
)

result.error && addToast({ title: result.error.type, type: 'error' })
const onSelectVariant = (variant: string) => {
dictionaryStore.selectVariant(lang, dictionary.id, variant)
}

return (
Expand Down
18 changes: 10 additions & 8 deletions src/popup/features/language/SelectLanguages/ui/SelectLanguages.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useStore } from '@nanostores/preact'
import { JSXInternal } from 'node_modules/preact/src/jsx'
import { useState } from 'preact/hooks'

import { browser } from '@popup/shared/browser'
import { Button } from '@popup/shared/ui/Button'
import { addToast, getErrorToast } from '@popup/shared/ui/Toast'
import { Result } from '@popup/shared/ui/Result'

import { ILanguage } from '@shared/entities/language'

import { TResult } from '@shared/shared/libs/operationResult'

import {
checkIsSelected,
localStore,
Expand All @@ -20,28 +23,25 @@ interface Props {
}

export const SelectLanguages = ({ languages }: Props) => {
const [result, setResult] = useState<TResult | null>(null)
useStore(localStore.languageCodes)

const onSubmit: JSXInternal.SubmitEventHandler<HTMLFormElement> = async (
e,
) => {
e.preventDefault()
const result = await syncLocalStoreWithLanguageStore()
result.data &&
addToast({
type: 'success',
title: result.data,
})
result.error && addToast(getErrorToast(result.error))
setResult(result)
}

return (
<form
className="form"
name="select_languages"
aria-labelledby="selectLanguageFormTitle"
onSubmit={onSubmit}
>
<h2 id="selectLanguageFormTitle" className="form__title">
<h2 id="selectLanguageFormTitle">
{browser.i18n.getMessage('SELECT_LANGUAGES_FORM_TITLE')}
</h2>

Expand All @@ -63,6 +63,8 @@ export const SelectLanguages = ({ languages }: Props) => {
))}
</ul>

<Result result={result} />

<footer className="form__footer">
<Button variant="secondary" onClick={reset}>
{browser.i18n.getMessage('CANCEL')}
Expand Down
6 changes: 2 additions & 4 deletions src/popup/features/note/DeleteNote/ui/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { browser } from '@popup/shared/browser'
import { Button } from '@popup/shared/ui/Button'
import { addToast, getErrorToast } from '@popup/shared/ui/Toast'
import { DeleteIcon } from '@popup/shared/ui/icons/DeleteIcon'

import { TLanguageCode } from '@shared/entities/language'
Expand All @@ -13,9 +12,8 @@ interface Props {
}

export const DeleteNote = ({ lang, noteId, noteText }: Props) => {
const onDelete = async (lang: TLanguageCode, noteId: string) => {
const result = await noteStore.deleteNote(lang, noteId)
result.error && addToast(getErrorToast(result.error))
const onDelete = (lang: TLanguageCode, noteId: string) => {
noteStore.deleteNote(lang, noteId)
}

return (
Expand Down
22 changes: 11 additions & 11 deletions src/popup/features/note/EditNote/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { useState } from 'preact/hooks'

import { browser } from '@popup/shared/browser'
import { Button } from '@popup/shared/ui/Button'
import { Result } from '@popup/shared/ui/Result'
import { Textarea } from '@popup/shared/ui/Textarea'
import { addToast, getErrorToast } from '@popup/shared/ui/Toast'

import { TLanguageCode } from '@shared/entities/language'
import { INote, noteStore } from '@shared/entities/note'

import { TResult } from '@shared/shared/libs/operationResult'

import styles from './styles.module.scss'

interface Props {
lang: TLanguageCode
note: INote
onCancel: () => void
onSubmit: () => void
onSubmit?: () => void
}

export const EditNote = ({
Expand All @@ -24,6 +26,7 @@ export const EditNote = ({
onCancel,
onSubmit: outerOnSubmit,
}: Props) => {
const [result, setResult] = useState<TResult | null>(null)
const [fields, setFields] = useState<INote>(note)

const onChange =
Expand All @@ -41,23 +44,18 @@ export const EditNote = ({
e.preventDefault()

const result = await noteStore.editNote(lang, fields)
if (result.data) {
addToast({
type: 'success',
title: result.data,
})
outerOnSubmit()
}
result.error && addToast(getErrorToast(result.error))
setResult(result)
if (result.data && outerOnSubmit) outerOnSubmit()
}

return (
<form
className="form"
name="edit_note"
aria-labelledby="editNoteFormTitle"
onSubmit={onSubmit}
>
<h1 id="editNoteFormTitle" className="form__title">
<h1 id="editNoteFormTitle">
{browser.i18n.getMessage('EDIT_NOTE_FORM_TITLE', fields.text)}
</h1>

Expand Down Expand Up @@ -93,6 +91,8 @@ export const EditNote = ({
/>
</div>

<Result result={result} />

<footer className="form__footer">
<Button variant="secondary" onClick={onCancel}>
{browser.i18n.getMessage('CANCEL')}
Expand Down
1 change: 0 additions & 1 deletion src/popup/widgets/note/NoteList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export const NoteList = () => {
lang={editNote.lang}
note={editNote.note}
onCancel={closeEditNoteModal}
onSubmit={closeEditNoteModal}
/>
</Modal>
) : null}
Expand Down

0 comments on commit b4d0cda

Please sign in to comment.