Skip to content

Commit

Permalink
👌 IMPROVE: Change window alerts on error for toasts
Browse files Browse the repository at this point in the history
  • Loading branch information
All-Khwarizmi committed Mar 17, 2024
1 parent 2b7fa15 commit afa5430
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/hooks/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LocalStorageCache } from "@/utils/local-storage";
import { Translations } from "@/utils/schemas/types";
import { translateWord } from "@/utils/translate-word";
import { useState, useEffect } from "react";
import { useToast } from "@chakra-ui/react";

export function useSearchWord() {
const [isLoading, setIsLoading] = useState<boolean>(false);
Expand All @@ -11,6 +12,7 @@ export function useSearchWord() {
const [word, setWord] = useState<string>("");
const [search, setSearch] = useState<string>("");
const [isFr, setIsFR] = useState<boolean>(true);
const toast = useToast();

useEffect(() => {
async function wordSearch() {
Expand All @@ -35,10 +37,23 @@ export function useSearchWord() {
setIsError(true);
setIsTranslations(false);
setIsLoading(false);
window.alert(`
Une erreur est survenue: ${error.message}
Veuillez réessayer.`);
setWord("");
if (error.statusCode === 404) {
toast({
title: "Mot introuvable",
status: "error",
duration: 3000,
isClosable: true,
});
return;
}
toast({
title: "Erreur inconnue",
status: "error",
duration: 3000,
isClosable: true,
});

return;
}
if (trads) {
Expand All @@ -56,9 +71,12 @@ export function useSearchWord() {
setIsError(true);
setIsTranslations(false);
setIsLoading(false);
window.alert(`
Une erreur inconnue est survenue.
Veuillez réessayer.`);
toast({
title: "Erreur inconnue, veuillez réessayer.",
status: "error",
duration: 3000,
isClosable: true,
});
setWord("");
}
}
Expand Down

0 comments on commit afa5430

Please sign in to comment.