Skip to content

Commit

Permalink
Merge pull request #69 from nepalcodes/SansCaar/issue42
Browse files Browse the repository at this point in the history
Hook for using APi for translation
  • Loading branch information
SansCaar authored Jul 7, 2024
2 parents 360ed00 + 650c91f commit d829919
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
1 change: 1 addition & 0 deletions nepalingo-web/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_NEPALBHASA_API_URL=https://subhash.net.np
2 changes: 2 additions & 0 deletions nepalingo-web/.env.example
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-->
2 changes: 2 additions & 0 deletions nepalingo-web/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ node_modules
dist
dist-ssr
*.local
#ignoring the .env
# .env

# Editor directories and files
.vscode/*
Expand Down
3 changes: 2 additions & 1 deletion nepalingo-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"@supabase/supabase-js": "^2.44.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.24.1"
"react-router-dom": "^6.24.1",
"swr": "^2.2.5"
},
"devDependencies": {
"@types/node": "^20.14.9",
Expand Down
32 changes: 30 additions & 2 deletions nepalingo-web/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions nepalingo-web/src/hooks/useDictionary.tsx
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
37 changes: 37 additions & 0 deletions nepalingo-web/src/hooks/useNewari.tsx
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

View workflow job for this annotation

GitHub Actions / test_build

'data' is never reassigned. Use 'const' instead

Check failure on line 9 in nepalingo-web/src/hooks/useNewari.tsx

View workflow job for this annotation

GitHub Actions / test_build

'error' is never reassigned. Use 'const' instead

Check failure on line 9 in nepalingo-web/src/hooks/useNewari.tsx

View workflow job for this annotation

GitHub Actions / test_build

'isLoading' is never reassigned. Use 'const' instead

let response: DictionaryResponse = {

Check failure on line 11 in nepalingo-web/src/hooks/useNewari.tsx

View workflow job for this annotation

GitHub Actions / test_build

'response' is never reassigned. Use 'const' instead
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;

0 comments on commit d829919

Please sign in to comment.