(0);
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) => {
if (text.length > 0) {
@@ -24,7 +35,7 @@ export const Categories: React.FC = () => {
console.log(result)
setCategories(result);
} else {
- setCategories(userCategories);
+ initUserProfile();
}
}
@@ -40,7 +51,7 @@ export const Categories: React.FC = () => {
@@ -69,6 +80,9 @@ export const Categories: React.FC = () => {
})}
- {isAddCategoryPopupOpen && setAddCategoryPopupState(false)} />}
+ {isAddCategoryPopupOpen && {
+ setAddCategoryPopupState(false);
+ setRenderKey(renderKey + 1);
+ }} />}
}
\ No newline at end of file
diff --git a/src/components/home/Home.tsx b/src/components/home/Home.tsx
index 69e5f7c..88fc008 100644
--- a/src/components/home/Home.tsx
+++ b/src/components/home/Home.tsx
@@ -16,55 +16,25 @@ import { NavLink } from "react-router-dom";
import { AddCategory } from "../add-category/AddCategory";
import { useHistory } from "react-router-dom";
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 = () => {
const [isAddCategoryPopupOpen, setAddCategoryPopupState] = useState(false);
const history = useHistory();
- const [userProfile, setUserProfile] = useState()
- const [allCatagories, setAllCatagories] = useState>([])
-
- const getUserProfileData = () => {
- getUserProfile().then((data: any) => {
- let userData: MongoUser = data.data;
-
- setUserProfile(userData);
-
- if (userData.categories) {
- getUserCategoryDetails(userData.categories)
- }
+ const [userProfile, setUserProfile] = useState();
+ const [renderKey, setRenderKey] = useState(0);
+
+ useEffect(() => {
+ getAllData().then((response: any) => {
+ localStorage.setItem('userProfile', JSON.stringify(response.data));
+ setUserProfile(response.data);
}, (err) => {
- console.log(err)
- })
- }
-
- const getUserCategoryDetails = (categoryIds: Array) => {
- let allCategoryDetails: Array = [];
-
- 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");
- }, [localStorage.getItem('categories')]);
+ }, [renderKey]);
return
@@ -108,7 +78,7 @@ export const Home: React.FC = () => {
-
+ {/*
@@ -147,9 +117,9 @@ export const Home: React.FC = () => {
-
+ */}
-
+ {/*
@@ -188,13 +158,13 @@ export const Home: React.FC = () => {
})
})}
-
+ */}
-
+ { userProfile &&
- Categories
+ Categories { ({userProfile.categories?.length}) }
@@ -204,7 +174,7 @@ export const Home: React.FC = () => {
- {allCatagories.map((category, index) => {
+ {userProfile.categories?.map((category: any, index) => {
return - {
history.push('/category-details/category_id=' + index);
}}>
@@ -218,8 +188,8 @@ export const Home: React.FC = () => {
})}
-
+ }
- {isAddCategoryPopupOpen && setAddCategoryPopupState(false)} />}
+ {isAddCategoryPopupOpen && { setAddCategoryPopupState(false); setRenderKey(renderKey + 1)} } />}
}
\ No newline at end of file
diff --git a/src/services/user.ts b/src/services/user.ts
index cfabbf6..9db2541 100644
--- a/src/services/user.ts
+++ b/src/services/user.ts
@@ -1,9 +1,17 @@
import axios from "axios";
import { SERVER_URL, API_TOKEN } from "../App";
-export const getUserProfile = async() =>{
+export const getUserProfile = async() => {
return await axios.get(SERVER_URL + '/user/profile/',
{
headers: API_TOKEN
})
}
+
+
+export const getAllData = async() => {
+ return await axios.get(SERVER_URL + '/user/deep-copy/',
+ {
+ headers: API_TOKEN
+ })
+}
\ No newline at end of file