From 56d33e4bbe29ffe9e973b3937399f2d951ebf246 Mon Sep 17 00:00:00 2001 From: Ananya-Bhardwaj Date: Tue, 29 Oct 2024 23:24:35 +0530 Subject: [PATCH] feat(plays): add dictionary play Add new dictionary play to fetch and display the definition, pronunciation, antonyms and synonyms of a word entered by user --- src/plays/dictionary/Dictionary.js | 46 ++++++++++++++++ src/plays/dictionary/DictionaryContainer.jsx | 57 ++++++++++++++++++++ src/plays/dictionary/Readme.md | 25 +++++++++ 3 files changed, 128 insertions(+) create mode 100644 src/plays/dictionary/Dictionary.js create mode 100644 src/plays/dictionary/DictionaryContainer.jsx create mode 100644 src/plays/dictionary/Readme.md diff --git a/src/plays/dictionary/Dictionary.js b/src/plays/dictionary/Dictionary.js new file mode 100644 index 0000000000..5d3c367d36 --- /dev/null +++ b/src/plays/dictionary/Dictionary.js @@ -0,0 +1,46 @@ +import PlayHeader from 'common/playlists/PlayHeader'; +import { useEffect, useState } from 'react'; +import axios from 'axios'; +import DictionaryContainer from './DictionaryContainer'; + +// WARNING: Do not change the entry componenet name +function Dictionary(props) { + const [word, setWord] = useState(''); + const [dicData, setDicData] = useState([]); + + const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`; + + // const { data, loading, error } = useFetch(DIC_API_URI); + // setDicData(data); + + const handleClick = () => { + axios.get(url).then((res) => setDicData(res.data)); + }; + + return ( + <> +
+ +
+ + + setWord(e.target.value)} + /> + + + + {dicData.length > 0 && } +
+
+ + ); +} + +export default Dictionary; diff --git a/src/plays/dictionary/DictionaryContainer.jsx b/src/plays/dictionary/DictionaryContainer.jsx new file mode 100644 index 0000000000..fe1c911a37 --- /dev/null +++ b/src/plays/dictionary/DictionaryContainer.jsx @@ -0,0 +1,57 @@ +import React from 'react'; +import { AiFillSound } from 'react-icons/ai'; + +const handlePronunciation = (data) => { + const audioUrl = data[0]['phonetics'][0]['audio']; + const audio = new Audio(audioUrl); + + audio.play(); +}; + +const DictionaryContainer = ({ data }) => { + return ( +
+ +

{data[0]['word'].toUpperCase()}

+ handlePronunciation(data)} + /> +
+ {data[0]['meanings'].map((item, index) => { + return ( +
+

{item['partOfSpeech']}

+ {item['definitions'].map((defn, index) => { + return ( +
+

: {defn['definition']}

+
+ ); + })} +

Synonyms:

+ {item['synonyms'] && + item['synonyms'].map((syn, index) => { + return ( + + {syn} + + ); + })} +

Antonyms:

+ {item['antonyms'] && + item['antonyms'].map((ant, index) => { + return ( + + {ant} + + ); + })} +
+ ); + })} +
+ ); +}; + +export default DictionaryContainer; diff --git a/src/plays/dictionary/Readme.md b/src/plays/dictionary/Readme.md new file mode 100644 index 0000000000..ad215b77fc --- /dev/null +++ b/src/plays/dictionary/Readme.md @@ -0,0 +1,25 @@ +# Dictionary + +Display the definition, pronunciation, antonyms and synonyms of the word + +## Play Demographic + +- Language: js +- Level: Intermediate + +## Creator Information + +- User: ananya-bhardwaj +- Gihub Link: https://github.com/ananya-bhardwaj + +## Implementation Details + +This implenmetation uses axios to call a remote API. The remote API returns the meaning as well as synonyms and antonyms of the word entered by the user, which is rendered. + +## Consideration + +- API call using axios + +## Resources + +- NA