-
Notifications
You must be signed in to change notification settings - Fork 5
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 #69 from nepalcodes/SansCaar/issue42
Hook for using APi for translation
- Loading branch information
Showing
7 changed files
with
116 additions
and
3 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 @@ | ||
VITE_NEPALBHASA_API_URL=https://subhash.net.np |
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 @@ | ||
# Create a .env file based on this file in your local repo | ||
VITE_NEPALBHASA_API_URL=<--URL_FOR_THE_NEPAL_BHASA_API--> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,42 @@ | ||
import useNewari from './useNewari' | ||
|
||
const Languages = [ | ||
'newari', | ||
'tajpuriya', | ||
'maithili' | ||
] | ||
|
||
export type DictionaryProps = { | ||
language: typeof Languages[number], | ||
word: string | ||
} | ||
|
||
export type DictionaryResponse = { | ||
language: string; | ||
word: string; | ||
meanings: [{ | ||
audio?: { file: string, directory: string }, | ||
image?: string, | ||
language: string, | ||
meaningOriginal?: string, | ||
meaningNp?: string, | ||
meaningEn: string, | ||
}] | ||
} | ||
|
||
///Use case | ||
//const {data, isLoading, error} = useDictionary('newari', "Hello") | ||
|
||
const useDictionary = ({ language, ...otherProps }: DictionaryProps) => { | ||
switch (language) { | ||
case 'newari': | ||
return useNewari(otherProps) | ||
case 'tajpuriya': | ||
return | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
|
||
export default useDictionary |
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,37 @@ | ||
import useSWR from 'swr' | ||
import { DictionaryProps, DictionaryResponse } from './useDictionary' | ||
|
||
|
||
const fetcher = (url: string) => fetch(import.meta.env.VITE_NEPALBHASA_API_URL + url, { | ||
}).then(r => r.json()) | ||
|
||
const useNewari = (props: Omit<DictionaryProps, 'language'>) => { | ||
let { data, error, isLoading } = useSWR(`/dict/en/search/${props.word}`, fetcher) | ||
Check failure on line 9 in nepalingo-web/src/hooks/useNewari.tsx GitHub Actions / test_build
Check failure on line 9 in nepalingo-web/src/hooks/useNewari.tsx GitHub Actions / test_build
|
||
|
||
let response: DictionaryResponse = { | ||
language: 'newari', | ||
word: props.word, | ||
//Mapping the meanings from the api to create a custom response based on DictionaryResponse | ||
meanings: | ||
data?.meanings?.map((meaning: { | ||
audio?: { file: string, directory: string }, | ||
image?: string, | ||
meaning_np?: string, | ||
meaning_nb?: string, | ||
meaning_en?: string, | ||
}) => ( | ||
{ | ||
audio: meaning?.audio, | ||
image: meaning?.image, | ||
meaningOriginal: meaning?.meaning_nb, | ||
meaningNp: meaning?.meaning_np, | ||
meaningEn: meaning?.meaning_en, | ||
|
||
}) | ||
) | ||
} | ||
|
||
return { response, error, isLoading } | ||
} | ||
|
||
export default useNewari; |