-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'issue-47' of github.com:typeofweb/polskifrontend into i…
…ssue-47
- Loading branch information
Showing
12 changed files
with
120 additions
and
73 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
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 |
---|---|---|
@@ -1,55 +1,52 @@ | ||
import clsx from 'clsx'; | ||
import { memo } from 'react'; | ||
|
||
import type { Status } from '../../hooks/useAddContentCreatorMutation'; | ||
import type { Status } from '../../hooks/useMutation'; | ||
|
||
import styles from './FormStatus.module.css'; | ||
|
||
type Props = { | ||
readonly status: Status; | ||
readonly errorCode?: number; | ||
}; | ||
|
||
export const FormStatus = memo<Props>(({ status }) => { | ||
const errorToMessage: Record<number, string> = { | ||
409: 'Blog o podanym adresie został już dodany do naszej bazy danych. Jeżeli nie pojawiają się najnowsze wpisy, lub masz inne zastrzeżenia - proszę skontaktuj się z administratorem.', | ||
422: 'Nie udało się odnaleźć pliku RSS na twojej stronie, jeśli ten błąd się powtarza, proszę skontaktuj się z administratorem.', | ||
}; | ||
|
||
const defaultErrorMessage = | ||
'Wystąpił błąd podczas dodawania nowego serwisu, sprawdź poprawność danych i spróbuj ponownie'; | ||
|
||
function getStatusMessage({ status, errorCode }: Props) { | ||
switch (status) { | ||
case 'loading': | ||
return ( | ||
<div className={styles.statusContainer}> | ||
<span className={clsx(styles.statusIcon, 'icon-spinner')}></span> | ||
<span>Oczekiwanie...</span> | ||
</div> | ||
); | ||
case 'error': | ||
return ( | ||
<div className={styles.statusContainer}> | ||
<span className={clsx(styles.statusIcon, 'icon-error')}></span> | ||
<span> | ||
Wystąpił błąd podczas dodawania nowego serwisu, sprawdź poprawność danych i spróbuj | ||
ponownie | ||
</span> | ||
</div> | ||
); | ||
case 'notRssFound': | ||
return ( | ||
<div className={styles.statusContainer}> | ||
<span className={clsx(styles.statusIcon, 'icon-error')}></span> | ||
<span> | ||
Nie udało się odnaleźć pliku rss na twojej stronie, jeśli ten błąd się powtarza, proszę | ||
skontaktuj się z administratorem. | ||
</span> | ||
</div> | ||
); | ||
return 'Oczekiwanie...'; | ||
case 'success': | ||
return ( | ||
<div className={styles.statusContainer}> | ||
<span className={clsx(styles.statusIcon, 'icon-checkmark')}></span> | ||
<span> | ||
Dziękujemy za zgłoszenie, dodany serwis pojawi się na stronie po zaakceptowaniu przez | ||
administrację | ||
</span> | ||
</div> | ||
); | ||
return 'Dziękujemy za zgłoszenie, dodany serwis pojawi się na stronie po zaakceptowaniu przez administrację'; | ||
case 'error': | ||
return errorCode ? errorToMessage[errorCode] ?? defaultErrorMessage : defaultErrorMessage; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
export const FormStatus = memo<Props>(({ status, errorCode }) => { | ||
if (status === 'idle') { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className={styles.statusContainer}> | ||
<span | ||
className={clsx(styles.statusIcon, { | ||
'icon-spinner': status === 'loading', | ||
'icon-checkmark': status === 'success', | ||
'icon-error': status === 'error', | ||
})} | ||
></span> | ||
<span>{getStatusMessage({ status, errorCode })}</span> | ||
</div> | ||
); | ||
}); | ||
FormStatus.displayName = 'FormStatus'; |
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
This file was deleted.
Oops, something went wrong.
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,28 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
export type Status = 'idle' | 'loading' | 'error' | 'success'; | ||
|
||
export function useMutation<Body>(mutation: (body: Body) => Promise<Response>) { | ||
const [status, setStatus] = useState<{ readonly status: Status; readonly errorCode?: number }>({ | ||
status: 'idle', | ||
}); | ||
|
||
const mutate = useCallback( | ||
async (body: Body) => { | ||
setStatus({ status: 'loading' }); | ||
try { | ||
const response = await mutation(body); | ||
if (response.ok) { | ||
setStatus({ status: 'success' }); | ||
} else { | ||
setStatus({ status: 'error', errorCode: response.status }); | ||
} | ||
} catch (err) { | ||
setStatus({ status: 'error' }); | ||
} | ||
}, | ||
[mutation], | ||
); | ||
|
||
return { mutate, ...status } as const; | ||
} |
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
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
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
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,17 @@ | ||
import type { ArticlePageProps } from '../pages/artykuly/[slug]'; | ||
|
||
// Detect YouTube videos by the article URL | ||
const ytRegex = /youtu(\.be|be\.com)\//; | ||
|
||
// Detect podcasts by the anchor.fm in article URL (most popular hosting) or podcast name (common words like talk or podcast) | ||
const podcastRegex = /(anchor\.fm\/|talk|podcast)/i; | ||
|
||
export const detectContentGenre = (article: ArticlePageProps['article']) => { | ||
if (ytRegex.test(article.href)) { | ||
return 'youtube' as const; | ||
} else if (podcastRegex.test(article.href) || podcastRegex.test(article.blog.name)) { | ||
return 'podcast' as const; | ||
} else { | ||
return 'blog' as const; | ||
} | ||
}; |
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
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,3 @@ | ||
{ | ||
"regions": ["dub1"] | ||
} |
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