From 59980c6355b1d8031b577cdeea8287fc86008dd7 Mon Sep 17 00:00:00 2001 From: prahalad Date: Tue, 26 Oct 2021 16:29:13 +0530 Subject: [PATCH] home Page and Category --Partial commit + pull shared module --- src/components/add-category/AddCategory.tsx | 55 ++++++++------ src/components/add-word/AddWord.tsx | 81 ++++++++++++--------- src/components/categories/Categories.tsx | 41 +++++++---- src/components/home/Home.tsx | 64 +++++++++++----- src/services/category.ts | 2 +- src/shared | 2 +- 6 files changed, 151 insertions(+), 94 deletions(-) diff --git a/src/components/add-category/AddCategory.tsx b/src/components/add-category/AddCategory.tsx index ac6da29..0c1563d 100644 --- a/src/components/add-category/AddCategory.tsx +++ b/src/components/add-category/AddCategory.tsx @@ -1,12 +1,12 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import styles from './AddCategory.module.scss'; import { ReactComponent as PlusIcon } from '../../assets/icons/plus.svg'; import { ReactComponent as BrainIcon } from '../../assets/icons/bx-brain.svg'; import { ReactComponent as TimeIcon } from '../../assets/icons/time.svg'; import { ReactComponent as TimerIcon } from '../../assets/icons/timer.svg'; import { ReactComponent as TextIcon } from '../../assets/icons/text.svg'; -import { ICategory } from "../../structure/category"; -import { userProfileData } from "../../App"; +import { MobileCategory } from "../../shared/models/category"; +import { addCategory, categoryDetails } from "../../services/category"; type ownProps = { hidePopup: () => void @@ -15,14 +15,25 @@ type ownProps = { const IconSet = [, , , ] export const AddCategory: React.FC = (props: ownProps) => { - const [newCategory, setnewCategory] = useState({ - name: '', - icon: , - shelves: [] - }); - + const [newCategory, setnewCategory] = useState(''); const [showIconSet, setShowIconSet] = useState(false); + let temp: Array = JSON.parse(localStorage.getItem("catagories") || "[]") + + function addNewCategory() { + console.log(temp) + addCategory(newCategory).then((category: any) => { + categoryDetails(category.data).then((category: any) => { + temp.push(category.data) + }) + console.log(category.data) + }, err => { + console.log("Unable to Add category", err) + }) + } + console.log(newCategory) + + return
@@ -33,29 +44,25 @@ export const AddCategory: React.FC = (props: ownProps) => {
setShowIconSet(!showIconSet)}> - { newCategory.icon } + {/* {newCategory.icon} */}
{ - setnewCategory({ - ...newCategory, - name: event.currentTarget.value - }); + setnewCategory(event.currentTarget.value); }} />
- - { showIconSet &&
    - { IconSet.map(iconSample =>
  • { - setnewCategory({ - ...newCategory, - icon: iconSample - }); + + {showIconSet &&
      + {IconSet.map(iconSample =>
    • { + setnewCategory(newCategory); setShowIconSet(false); - }}> {iconSample}
    • ) } -
    } + }}> {iconSample}
  • )} +
} diff --git a/src/components/add-word/AddWord.tsx b/src/components/add-word/AddWord.tsx index 6f196bd..9c6a0d6 100644 --- a/src/components/add-word/AddWord.tsx +++ b/src/components/add-word/AddWord.tsx @@ -5,29 +5,40 @@ import { ReactComponent as SearchIcon } from '../../assets/icons/bx-search-alt.s import { ReactComponent as SpeakerIcon } from '../../assets/icons/speaker.svg'; import { ReactComponent as AddIcon } from '../../assets/icons/plus.svg'; -import { IWord } from "../../structure/word"; -import { ALL_WORDS } from "../../data/all-words"; import { NavLink, useHistory } from "react-router-dom"; import { userProfileData } from "../../App"; +import { getWordResult } from "../../services/words"; +import { Word } from "../../shared/models/word"; export const AddWord: React.FC = () => { - const [searchWordResult, setSearchResult] = useState>(ALL_WORDS); - const [selectedWord, setSelectedWord] = useState(); + const [searchWordResult, setSearchResult] = useState>(); + const [selectedWord, setSelectedWord] = useState(); const history = useHistory(); const searchWords = (searchWord: string) => { - if (searchWord.length > 0) { - let result = ALL_WORDS.filter((word) => { - return word.name.toLowerCase().includes(searchWord.toLowerCase()); - }); - - setSearchResult(result); - } else { - setSearchResult(ALL_WORDS); - } + console.log(searchWord.length) + + setTimeout(() => { + if (searchWord.length > 0) { + getWordResult(searchWord).then((wordResult: any) => { + let result: Array = wordResult.data + console.log(result.slice(0,10)) + setSearchResult(result); + }, err => { + console.log("Unable to search Words", err) + }) + + } else { + getWordResult("Aa").then((wordResult: any) => { + let result: Array = wordResult.data + setSearchResult(result); + }) + } + }, 1000) } return
+
- { !selectedWord &&
Add a Word
} - { selectedWord &&
Add to Shelf
} - + {!selectedWord &&
Add a Word
} + {selectedWord &&
Add to Shelf
} +
{/* eslint-disable-next-line */} - profile-image + profile-image
- { !selectedWord &&
+ {!selectedWord &&
searchWords(event.currentTarget.value)} /> @@ -56,29 +67,29 @@ export const AddWord: React.FC = () => {
    - { searchWordResult.map((word) => { + {searchWordResult?.map((word) => { return
  • -

    { word.name } { word.pronounciation }

    +

    {word.name} {word.pronounciation}

    - { word.grammaticalDetails[0].typeName } -

    { word.grammaticalDetails[0].description }

    + {word.grammaticalDetails[0]?.type} +

    {word.grammaticalDetails[0]?.description}

  • - }) } + })}
-
} +
} - { selectedWord &&
+ {selectedWord &&
- +
@@ -87,7 +98,7 @@ export const AddWord: React.FC = () => {
All Shelves
- { userProfileData.categories.map((category, categoryIndex) => { + {userProfileData.categories.map((category, categoryIndex) => { return category.shelves.map((shelf, shelfIndex) => { return
  • { let findWord = userProfileData.categories[categoryIndex].shelves[shelfIndex].words.find(word => word.name.toLowerCase().includes(selectedWord.name.toLowerCase())); @@ -95,24 +106,24 @@ export const AddWord: React.FC = () => { if (findWord) { alert("Word already Present!"); } else { - userProfileData.categories[categoryIndex].shelves[shelfIndex].words.push(selectedWord); + // userProfileData.categories[categoryIndex].shelves[shelfIndex].words.push(selectedWord); history.push('/shelf-details/category_id=' + categoryIndex + '&&shelf_id=' + shelfIndex); - } + } }}>
    - { category.icon } + {category.icon}
    - - { category.name } + + {category.name}
  • }) - }) } + })} Create New Shelf -
    } - +
    } +
    } \ No newline at end of file diff --git a/src/components/categories/Categories.tsx b/src/components/categories/Categories.tsx index e806d28..64fbe92 100644 --- a/src/components/categories/Categories.tsx +++ b/src/components/categories/Categories.tsx @@ -1,28 +1,38 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import styles from './Categories.module.scss'; import { ReactComponent as LogoIcon } from '../../assets/icons/anamnesis.svg'; import { ReactComponent as SearchIcon } from '../../assets/icons/bx-search-alt.svg'; import { ReactComponent as PlusCircledIcon } from '../../assets/icons/plus-circle.svg'; import { NavLink } from "react-router-dom"; -import { ICategory } from "../../structure/category"; import { AddCategory } from "../add-category/AddCategory"; -import { userProfileData } from "../../App"; +import { Category } from "../../shared/models/category"; export const Categories: React.FC = () => { - const [categoryResult, setCategories] = useState>(userProfileData.categories); const [isAddCategoryPopupOpen, setAddCategoryPopupState] = useState(false); + const [categoryResult, setCategories] = useState>([]) + let userCategories = JSON.parse(localStorage.getItem("catagories") || "[]") + + useEffect(() => { + getUserCategory(); + }, []) + const filterCategories = (text: string) => { if (text.length > 0) { - let result = userProfileData.categories.filter(category => { + let result: any = categoryResult.filter(category => { return category.name.toLowerCase().includes(text.toLowerCase()); }); + console.log(result) setCategories(result); } else { - setCategories(userProfileData.categories); + setCategories(userCategories); } } + function getUserCategory() { + setCategories(userCategories) + } + return
    @@ -32,12 +42,12 @@ export const Categories: React.FC = () => { profile-image
    - +

    Categories

    -

    { userProfileData.categories.length } Types

    +

    {userCategories.length} Types

    - +
    filterCategories(event.currentTarget.value)} /> @@ -51,20 +61,19 @@ export const Categories: React.FC = () => {
      - { categoryResult.map((category, index) => { + {categoryResult.map((category, index) => { return
    • - { category.icon } + {category.icon}
      - - { category.shelves.length } Shelves + + {category.shelves?.length} Shelves
    • - })} + })}
    - - { isAddCategoryPopupOpen && setAddCategoryPopupState(false)} /> } + {isAddCategoryPopupOpen && setAddCategoryPopupState(false)} />}
    } \ No newline at end of file diff --git a/src/components/home/Home.tsx b/src/components/home/Home.tsx index 2f395fb..c5ad040 100644 --- a/src/components/home/Home.tsx +++ b/src/components/home/Home.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import styles from './Home.module.scss'; import { ReactComponent as LogoIcon } from '../../assets/icons/anamnesis.svg'; import { ReactComponent as SearchIcon } from '../../assets/icons/bx-search-alt.svg'; @@ -16,26 +16,56 @@ import { NavLink } from "react-router-dom"; import { AddCategory } from "../add-category/AddCategory"; import { useHistory } from "react-router-dom"; import { userProfileData } from "../../App"; -import { addCategory, getCategory, updateCategory } from "../../services/category"; import { getUserProfile } from "../../services/user"; -import { getWordResult } from "../../services/words"; +import { User } from "../../shared/models/user" +import { categoryDetails } from "../../services/category"; +import { MobileCategory } from "../../shared/models/category"; export const Home: React.FC = () => { const [isAddCategoryPopupOpen, setAddCategoryPopupState] = useState(false); const history = useHistory(); + const [userProfile, setUserProfile] = useState() + const [allCatagories, setAllCatagories] = useState>([]) - getUserProfile().then((data) => { - console.log("UserProfile", data) - }, (err) => { - console.log(err) - }) - - getWordResult('Ple').then((data) => { - console.log("Words", data.data) - }, (err) => { - console.log(err) - }) + + useEffect(() => { + User(); + }, []) + + function User() { + getUserProfile().then((data: any) => { + + let userData = data.data + localStorage.setItem("catagoryIds", JSON.stringify(userData.categories)) + setUserProfile(userData) + getUserCategory(); + }, (err) => { + console.log(err) + }) + } + + let allCategoryDetails: Array = [] + + function getUserCategory() { + let categoryIds: Array = JSON.parse(localStorage.getItem("catagoryIds") || "[]") + let catagoryDetails: Array = JSON.parse(localStorage.getItem("catagories") || "[]") + + + categoryIds.map((catagories: any) => { + categoryDetails(catagories).then((categoryDetails: any) => { + allCategoryDetails.push(categoryDetails.data) + localStorage.setItem("catagories", JSON.stringify(allCategoryDetails)) + }, (err) => { + console.log("Error fetching Category Details", err) + }) + }) + + console.log(catagoryDetails) + setAllCatagories(catagoryDetails) + } + + console.log(allCatagories) return
    @@ -62,7 +92,7 @@ export const Home: React.FC = () => { {userProfileData.medal.icon}
    -

    Neymar Junior

    +

    {userProfile?.name}

    1.2K
    @@ -175,7 +205,7 @@ export const Home: React.FC = () => {
      - {userProfileData.categories.map((category, index) => { + {allCatagories.map((category: any, index: any) => { return
    • { history.push('/category-details/category_id=' + index); }}> @@ -184,7 +214,7 @@ export const Home: React.FC = () => {
    - {category.shelves.length} Shelves + {category.shelves?.length > 0 ? category.shelves?.length : 0} Shelves
    })} diff --git a/src/services/category.ts b/src/services/category.ts index 3f04e9c..8a7f71d 100644 --- a/src/services/category.ts +++ b/src/services/category.ts @@ -12,7 +12,7 @@ export const addCategory = async (name: string) => { //Need to be changed -export const getCategory = async (id: string) => { +export const categoryDetails = async (id: string) => { return await axios.get(SERVER_URL + '/category/details/', { params: { _id: id }, headers: API_TOKEN diff --git a/src/shared b/src/shared index 7dc8d39..f4b5742 160000 --- a/src/shared +++ b/src/shared @@ -1 +1 @@ -Subproject commit 7dc8d39f1be0f5159aa473de31620a72c77b98af +Subproject commit f4b5742e27cf554dc922fa1af415bafea7214b30