From f347ef180ece9235380f2225243beddaececa949 Mon Sep 17 00:00:00 2001 From: Ashm Walia <40498934+ashmgarv@users.noreply.github.com> Date: Mon, 9 Nov 2020 12:16:44 -0800 Subject: [FOR MASS REVIEW] Multiple contexts(Searched user gets replaced) (#97) * First commit towards clean code * Tested things * Some final touch * View updates posts * Cleaned up followers / following * You won't believe but it works * Pass avatar uri via props * Small change * Small change * Removed unnecessary jargon Co-authored-by: Ivan Chen --- src/routes/viewProfile/ProfileProvider.tsx | 41 +++++++++++++++++------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'src/routes/viewProfile') diff --git a/src/routes/viewProfile/ProfileProvider.tsx b/src/routes/viewProfile/ProfileProvider.tsx index f9b29dc6..f2d27a84 100644 --- a/src/routes/viewProfile/ProfileProvider.tsx +++ b/src/routes/viewProfile/ProfileProvider.tsx @@ -22,11 +22,8 @@ import { interface ProfileContextProps { user: UserType; profile: ProfileType; - loadProfile: (userId: string, username: string) => void; avatar: string | null; cover: string | null; - newMomentsAvailable: boolean; - updateMoments: (value: boolean) => void; socialAccounts: Record; socialsNeedUpdate: (_: string[]) => void; moments: MomentType[]; @@ -54,11 +51,8 @@ const NO_SOCIAL_ACCOUNTS: Record = { export const ProfileContext = createContext({ user: NO_USER, profile: NO_PROFILE, - loadProfile: () => {}, avatar: null, cover: null, - newMomentsAvailable: true, - updateMoments: () => {}, socialAccounts: NO_SOCIAL_ACCOUNTS, socialsNeedUpdate: () => {}, moments: [], @@ -70,9 +64,20 @@ export const ProfileContext = createContext({ /** * This is the context provider for user profiles that the logged in user wants to see + * The ProfileProviderProps is used to initialise data as soon as the component is initialised. */ -const ProfileProvider: React.FC = ({children}) => { - const [user, setUser] = useState(NO_USER); + +type ProfileProviderProps = { + uname: string; + uId: string; +}; + +const ProfileProvider: React.FC = ({ + children, + uId, + uname, +}) => { + const [user, setUser] = useState({userId: uId, username: uname}); const [profile, setProfile] = useState(NO_PROFILE); const [avatar, setAvatar] = useState(null); const [cover, setCover] = useState(null); @@ -157,8 +162,17 @@ const ProfileProvider: React.FC = ({children}) => { if (socialsNeedUpdate.length > 0 && userId) { for (let social of socialsNeedUpdate) { loadSocialPosts(userId, social).then((accountData) => { - socialAccounts[social] = accountData; - setSocialAccounts(socialAccounts); + /** + * Please use the following syntax when updating an object, fixing this problem broke our head. LOLs + * ref1 : https://stackoverflow.com/questions/56423256/set-dynamic-key-in-state-via-usestate-react-hooks + * ref2: https://stackoverflow.com/questions/43638938/updating-an-object-with-setstate-in-react/43639228 + * The spread operator {...} helps us make a simple copy of the object + * And form there on we can use the [] to specify the dynamically constructed key and set its value. + */ + setSocialAccounts((prevSocialAccounts) => ({ + ...prevSocialAccounts, + [social]: accountData, + })); console.log('Updated posts data', social); }); } @@ -173,18 +187,11 @@ const ProfileProvider: React.FC = ({children}) => { profile, avatar, cover, - newMomentsAvailable, socialAccounts, moments, followers, following, followersNeedUpdate, - loadProfile: (id, username) => { - setUser({...user, userId: id, username}); - }, - updateMoments: (value) => { - setNewMomentsAvailable(value); - }, socialsNeedUpdate: (socials: string[]) => { setSocialsNeedUpdate(socials); }, -- cgit v1.2.3-70-g09d2