|
|
@@ -6,20 +6,22 @@ import { ReactComponent as SpeakerIcon } from '../../assets/icons/speaker.svg'; |
|
|
|
import { ReactComponent as AddIcon } from '../../assets/icons/plus.svg'; |
|
|
|
|
|
|
|
import { NavLink, useHistory } from "react-router-dom"; |
|
|
|
import { userProfileData } from "../../App"; |
|
|
|
import { getWordResult } from "../../services/words"; |
|
|
|
import { Word } from "../../shared/models/word"; |
|
|
|
import { MobileWord } from "../../shared/models/word"; |
|
|
|
import { MobileUser } from "../../shared/models/user"; |
|
|
|
import { updateShelf } from "../../services/shelf"; |
|
|
|
|
|
|
|
export const AddWord: React.FC = () => { |
|
|
|
const [searchResult, setSearchResult] = useState<Array<Word>>(); |
|
|
|
const [selectedWord, setSelectedWord] = useState<Word>(); |
|
|
|
const [searchResult, setSearchResult] = useState<Array<MobileWord>>(); |
|
|
|
const [selectedWord, setSelectedWord] = useState<MobileWord>(); |
|
|
|
const [searchWord, setSearchWord] = useState<string>(''); |
|
|
|
const userProfile: MobileUser = JSON.parse(localStorage.getItem('userProfile') || ""); |
|
|
|
const history = useHistory(); |
|
|
|
|
|
|
|
const searchWords = () => { |
|
|
|
if (searchWord.length > 0) { |
|
|
|
getWordResult(searchWord).then((wordResult: any) => { |
|
|
|
let result: Array<Word> = wordResult.data; |
|
|
|
let result: Array<MobileWord> = wordResult.data; |
|
|
|
setSearchResult(result); |
|
|
|
}, err => { |
|
|
|
console.log("Unable to search Words", err) |
|
|
@@ -41,11 +43,6 @@ export const AddWord: React.FC = () => { |
|
|
|
</button> |
|
|
|
{!selectedWord && <h5> Add a Word </h5>} |
|
|
|
{selectedWord && <h5> Add to Shelf </h5>} |
|
|
|
|
|
|
|
<figure style={{ opacity: selectedWord ? 0 : 1 }}> |
|
|
|
{/* eslint-disable-next-line */} |
|
|
|
<img src={userProfileData.image} alt="profile-image" /> |
|
|
|
</figure> |
|
|
|
</header> |
|
|
|
|
|
|
|
{!selectedWord && <section> |
|
|
@@ -90,16 +87,38 @@ export const AddWord: React.FC = () => { |
|
|
|
<header> |
|
|
|
<h5> All Shelves </h5> |
|
|
|
</header> |
|
|
|
{userProfileData.categories.map((category, categoryIndex) => { |
|
|
|
return category.shelves.map((shelf, shelfIndex) => { |
|
|
|
return <li key={shelf.name} onClick={() => { |
|
|
|
let findWord = userProfileData.categories[categoryIndex].shelves[shelfIndex].words.find(word => word.name.toLowerCase().includes(selectedWord.name.toLowerCase())); |
|
|
|
{userProfile.categories.map((category) => { |
|
|
|
return category?.shelves?.map((shelf, shelfIndex) => { |
|
|
|
return <li key={shelfIndex} onClick={() => { |
|
|
|
|
|
|
|
let words: any = shelf.words ? shelf.words : []; |
|
|
|
|
|
|
|
if (findWord) { |
|
|
|
if (words.findIndex((word: any) => word.word.name === selectedWord.name) >= 0) { |
|
|
|
alert("Word already Present!"); |
|
|
|
} else { |
|
|
|
// userProfileData.categories[categoryIndex].shelves[shelfIndex].words.push(selectedWord); |
|
|
|
history.push('/shelf-details/category_id=' + categoryIndex + '&&shelf_id=' + shelfIndex); |
|
|
|
words.push({ |
|
|
|
word: selectedWord, |
|
|
|
notes: [], |
|
|
|
isArchived: false, |
|
|
|
spaceBetweenRecall: 1, |
|
|
|
nextRevisionDateTime: new Date() |
|
|
|
}) |
|
|
|
|
|
|
|
let updatedWords: any = []; |
|
|
|
|
|
|
|
updatedWords = words.map((word: any) => { |
|
|
|
return { |
|
|
|
...word, |
|
|
|
word: word.word._id, |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
updateShelf(shelf._id, updatedWords).then(() => { |
|
|
|
history.push('/shelf-details/category_id=' + category._id + '&&shelf_id=' + shelf._id); |
|
|
|
}, (err) => { |
|
|
|
window.alert("Failed to update"); |
|
|
|
console.log(err); |
|
|
|
}); |
|
|
|
} |
|
|
|
}}> |
|
|
|
<div className={styles.icon}> |
|
|
|