aboutsummaryrefslogtreecommitdiff
path: root/src/routes/viewProfile/ProfileProvider.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/viewProfile/ProfileProvider.tsx')
-rw-r--r--src/routes/viewProfile/ProfileProvider.tsx41
1 files changed, 24 insertions, 17 deletions
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<string, SocialAccountType>;
socialsNeedUpdate: (_: string[]) => void;
moments: MomentType[];
@@ -54,11 +51,8 @@ const NO_SOCIAL_ACCOUNTS: Record<string, SocialAccountType> = {
export const ProfileContext = createContext<ProfileContextProps>({
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<ProfileContextProps>({
/**
* 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<UserType>(NO_USER);
+
+type ProfileProviderProps = {
+ uname: string;
+ uId: string;
+};
+
+const ProfileProvider: React.FC<ProfileProviderProps> = ({
+ children,
+ uId,
+ uname,
+}) => {
+ const [user, setUser] = useState<UserType>({userId: uId, username: uname});
const [profile, setProfile] = useState<ProfileType>(NO_PROFILE);
const [avatar, setAvatar] = useState<string | null>(null);
const [cover, setCover] = useState<string | null>(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);
},