@@ -5,8 +5,8 @@ import { ReactComponent as BrainIcon } from '../../assets/icons/bx-brain.svg'; | |||||
import { ReactComponent as TimeIcon } from '../../assets/icons/time.svg'; | import { ReactComponent as TimeIcon } from '../../assets/icons/time.svg'; | ||||
import { ReactComponent as TimerIcon } from '../../assets/icons/timer.svg'; | import { ReactComponent as TimerIcon } from '../../assets/icons/timer.svg'; | ||||
import { ReactComponent as TextIcon } from '../../assets/icons/text.svg'; | import { ReactComponent as TextIcon } from '../../assets/icons/text.svg'; | ||||
import { MobileCategory } from "../../shared/models/category"; | |||||
import { addCategory, getCategoryDetails } from "../../services/category"; | import { addCategory, getCategoryDetails } from "../../services/category"; | ||||
import { User } from "../../shared/models/user"; | |||||
type ownProps = { | type ownProps = { | ||||
hidePopup: () => void | hidePopup: () => void | ||||
@@ -17,14 +17,13 @@ const IconSet = [<BrainIcon />, <TimeIcon />, <TimerIcon />, <TextIcon />] | |||||
export const AddCategory: React.FC<ownProps> = (props: ownProps) => { | export const AddCategory: React.FC<ownProps> = (props: ownProps) => { | ||||
const [newCategory, setnewCategory] = useState<string>(''); | const [newCategory, setnewCategory] = useState<string>(''); | ||||
const [showIconSet, setShowIconSet] = useState<Boolean>(false); | const [showIconSet, setShowIconSet] = useState<Boolean>(false); | ||||
const userProfile: User = JSON.parse(localStorage.getItem('userProfile') || ""); | |||||
function addNewCategory() { | |||||
let currentCategories: Array<MobileCategory> = JSON.parse(localStorage.getItem("categories") || "[]"); | |||||
const addNewCategory = () => { | |||||
addCategory(newCategory).then((response: any) => { | addCategory(newCategory).then((response: any) => { | ||||
getCategoryDetails(response.data).then((data: any) => { | getCategoryDetails(response.data).then((data: any) => { | ||||
currentCategories.push(data.data); | |||||
localStorage.setItem("categories", JSON.stringify(currentCategories)); | |||||
userProfile.categories?.push(data.data); | |||||
localStorage.setItem('userProfile', JSON.stringify(userProfile)); | |||||
}); | }); | ||||
}, err => { | }, err => { | ||||
console.log("Unable to Add category", err) | console.log("Unable to Add category", err) | ||||
@@ -59,7 +58,6 @@ export const AddCategory: React.FC<ownProps> = (props: ownProps) => { | |||||
// userProfileData.categories.push(newCategory); | // userProfileData.categories.push(newCategory); | ||||
addNewCategory(); | addNewCategory(); | ||||
props.hidePopup(); | props.hidePopup(); | ||||
}}> | }}> | ||||
Add | Add | ||||
</button> | </button> | ||||
@@ -6,15 +6,26 @@ import { ReactComponent as PlusCircledIcon } from '../../assets/icons/plus-circl | |||||
import { NavLink } from "react-router-dom"; | import { NavLink } from "react-router-dom"; | ||||
import { AddCategory } from "../add-category/AddCategory"; | import { AddCategory } from "../add-category/AddCategory"; | ||||
import { Category } from "../../shared/models/category"; | import { Category } from "../../shared/models/category"; | ||||
import { User } from "../../shared/models/user"; | |||||
export const Categories: React.FC = () => { | export const Categories: React.FC = () => { | ||||
const [isAddCategoryPopupOpen, setAddCategoryPopupState] = useState<Boolean>(false); | const [isAddCategoryPopupOpen, setAddCategoryPopupState] = useState<Boolean>(false); | ||||
const [categoryResult, setCategories] = useState<Array<Category>>([]); | const [categoryResult, setCategories] = useState<Array<Category>>([]); | ||||
const userCategories = JSON.parse(localStorage.getItem("categories") || "[]"); | |||||
const [renderKey, setRenderKey] = useState<number>(0); | |||||
useEffect(() => { | useEffect(() => { | ||||
setCategories(userCategories); | |||||
}, []) | |||||
setTimeout(() => { | |||||
initUserProfile(); | |||||
}, 100); | |||||
console.log("rendering categories"); | |||||
}, [renderKey]) | |||||
const initUserProfile = () => { | |||||
const userProfile: User = JSON.parse(localStorage.getItem('userProfile') || ""); | |||||
if (userProfile.categories) { | |||||
setCategories(userProfile.categories); | |||||
} | |||||
} | |||||
const filterCategories = (text: string) => { | const filterCategories = (text: string) => { | ||||
if (text.length > 0) { | if (text.length > 0) { | ||||
@@ -24,7 +35,7 @@ export const Categories: React.FC = () => { | |||||
console.log(result) | console.log(result) | ||||
setCategories(result); | setCategories(result); | ||||
} else { | } else { | ||||
setCategories(userCategories); | |||||
initUserProfile(); | |||||
} | } | ||||
} | } | ||||
@@ -40,7 +51,7 @@ export const Categories: React.FC = () => { | |||||
<header className={styles.tabHeading}> | <header className={styles.tabHeading}> | ||||
<h4> Categories </h4> | <h4> Categories </h4> | ||||
<p> {userCategories.length} Types </p> | |||||
<p> {categoryResult.length} Types </p> | |||||
</header> | </header> | ||||
<section className={styles.searchHolder}> | <section className={styles.searchHolder}> | ||||
@@ -69,6 +80,9 @@ export const Categories: React.FC = () => { | |||||
})} | })} | ||||
</ul> | </ul> | ||||
</section> | </section> | ||||
{isAddCategoryPopupOpen && <AddCategory hidePopup={() => setAddCategoryPopupState(false)} />} | |||||
{isAddCategoryPopupOpen && <AddCategory hidePopup={() => { | |||||
setAddCategoryPopupState(false); | |||||
setRenderKey(renderKey + 1); | |||||
}} />} | |||||
</section> | </section> | ||||
} | } |
@@ -16,55 +16,25 @@ import { NavLink } from "react-router-dom"; | |||||
import { AddCategory } from "../add-category/AddCategory"; | import { AddCategory } from "../add-category/AddCategory"; | ||||
import { useHistory } from "react-router-dom"; | import { useHistory } from "react-router-dom"; | ||||
import { userProfileData } from "../../App"; | import { userProfileData } from "../../App"; | ||||
import { getUserProfile } from "../../services/user"; | |||||
import { MongoUser } from "../../shared/models/user" | |||||
import { getCategoryDetails } from "../../services/category"; | |||||
import { MobileCategory } from "../../shared/models/category"; | |||||
import { User } from "../../shared/models/user"; | |||||
import { getAllData } from "../../services/user"; | |||||
export const Home: React.FC = () => { | export const Home: React.FC = () => { | ||||
const [isAddCategoryPopupOpen, setAddCategoryPopupState] = useState<Boolean>(false); | const [isAddCategoryPopupOpen, setAddCategoryPopupState] = useState<Boolean>(false); | ||||
const history = useHistory(); | const history = useHistory(); | ||||
const [userProfile, setUserProfile] = useState<MongoUser>() | |||||
const [allCatagories, setAllCatagories] = useState<Array<MobileCategory>>([]) | |||||
const getUserProfileData = () => { | |||||
getUserProfile().then((data: any) => { | |||||
let userData: MongoUser = data.data; | |||||
setUserProfile(userData); | |||||
if (userData.categories) { | |||||
getUserCategoryDetails(userData.categories) | |||||
} | |||||
const [userProfile, setUserProfile] = useState<User>(); | |||||
const [renderKey, setRenderKey] = useState<number>(0); | |||||
useEffect(() => { | |||||
getAllData().then((response: any) => { | |||||
localStorage.setItem('userProfile', JSON.stringify(response.data)); | |||||
setUserProfile(response.data); | |||||
}, (err) => { | }, (err) => { | ||||
console.log(err) | |||||
}) | |||||
} | |||||
const getUserCategoryDetails = (categoryIds: Array<string>) => { | |||||
let allCategoryDetails: Array<MobileCategory> = []; | |||||
categoryIds.forEach((catagoryId) => { | |||||
getCategoryDetails(catagoryId).then((categoryDetails: any) => { | |||||
allCategoryDetails.push(categoryDetails.data); | |||||
if (categoryIds.length === allCategoryDetails.length) { | |||||
setAllCatagories(allCategoryDetails); | |||||
localStorage.setItem("categories", JSON.stringify(allCategoryDetails)) | |||||
} | |||||
}, (err) => { | |||||
console.log("Error fetching Category Details", err) | |||||
}); | |||||
window.alert("Failed to fetch deep copy of user profile"); | |||||
console.log(err); | |||||
}); | }); | ||||
} | |||||
useEffect(() => { | |||||
getUserProfileData(); | |||||
console.log("rendering Home"); | console.log("rendering Home"); | ||||
}, [localStorage.getItem('categories')]); | |||||
}, [renderKey]); | |||||
return <section className="page"> | return <section className="page"> | ||||
@@ -108,7 +78,7 @@ export const Home: React.FC = () => { | |||||
<SearchIcon /> | <SearchIcon /> | ||||
</NavLink> | </NavLink> | ||||
<section className={styles.List}> | |||||
{/* <section className={styles.List}> | |||||
<header className={styles.blockHeader}> | <header className={styles.blockHeader}> | ||||
<h4> | <h4> | ||||
<BookIcon /> | <BookIcon /> | ||||
@@ -147,9 +117,9 @@ export const Home: React.FC = () => { | |||||
</div> | </div> | ||||
</li> | </li> | ||||
</ul> | </ul> | ||||
</section> | |||||
</section> */} | |||||
<section className={styles.Shelf}> | |||||
{/* <section className={styles.Shelf}> | |||||
<header className={styles.blockHeader}> | <header className={styles.blockHeader}> | ||||
<h4> | <h4> | ||||
<BookShelfIcon /> | <BookShelfIcon /> | ||||
@@ -188,13 +158,13 @@ export const Home: React.FC = () => { | |||||
}) | }) | ||||
})} | })} | ||||
</ul> | </ul> | ||||
</section> | |||||
</section> */} | |||||
<section className={styles.Grid}> | |||||
{ userProfile && <section className={styles.Grid}> | |||||
<header className={styles.blockHeader}> | <header className={styles.blockHeader}> | ||||
<h4> | <h4> | ||||
<GridIcon /> | <GridIcon /> | ||||
Categories | |||||
Categories { <span>({userProfile.categories?.length})</span> } | |||||
</h4> | </h4> | ||||
<NavLink to={'/categories'} className={styles.expandButton}> | <NavLink to={'/categories'} className={styles.expandButton}> | ||||
<ExpandIcon /> | <ExpandIcon /> | ||||
@@ -204,7 +174,7 @@ export const Home: React.FC = () => { | |||||
</button> | </button> | ||||
</header> | </header> | ||||
<ul> | <ul> | ||||
{allCatagories.map((category, index) => { | |||||
{userProfile.categories?.map((category: any, index) => { | |||||
return <li key={index} onClick={() => { | return <li key={index} onClick={() => { | ||||
history.push('/category-details/category_id=' + index); | history.push('/category-details/category_id=' + index); | ||||
}}> | }}> | ||||
@@ -218,8 +188,8 @@ export const Home: React.FC = () => { | |||||
</li> | </li> | ||||
})} | })} | ||||
</ul> | </ul> | ||||
</section> | |||||
</section> } | |||||
{isAddCategoryPopupOpen && <AddCategory hidePopup={() => setAddCategoryPopupState(false)} />} | |||||
{isAddCategoryPopupOpen && <AddCategory hidePopup={() => { setAddCategoryPopupState(false); setRenderKey(renderKey + 1)} } />} | |||||
</section> | </section> | ||||
} | } |
@@ -1,9 +1,17 @@ | |||||
import axios from "axios"; | import axios from "axios"; | ||||
import { SERVER_URL, API_TOKEN } from "../App"; | import { SERVER_URL, API_TOKEN } from "../App"; | ||||
export const getUserProfile = async() =>{ | |||||
export const getUserProfile = async() => { | |||||
return await axios.get(SERVER_URL + '/user/profile/', | return await axios.get(SERVER_URL + '/user/profile/', | ||||
{ | { | ||||
headers: API_TOKEN | headers: API_TOKEN | ||||
}) | }) | ||||
} | } | ||||
export const getAllData = async() => { | |||||
return await axios.get(SERVER_URL + '/user/deep-copy/', | |||||
{ | |||||
headers: API_TOKEN | |||||
}) | |||||
} |