aboutsummaryrefslogtreecommitdiff
path: root/src/services/UserProfileService.ts
blob: 6e1d1ef533e00ec97394cac781adfb8851a18578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//Abstracted common profile api calls out here

import AsyncStorage from '@react-native-community/async-storage';
import {Alert} from 'react-native';
import RNFetchBlob from 'rn-fetch-blob';
import {SocialAccountType, MomentType} from 'src/types';
import {
  AVATAR_PHOTO_ENDPOINT,
  COVER_PHOTO_ENDPOINT,
  GET_FB_POSTS_ENDPOINT,
  GET_IG_POSTS_ENDPOINT,
  GET_TWITTER_POSTS_ENDPOINT,
  PROFILE_INFO_ENDPOINT,
  MOMENTS_ENDPOINT,
} from '../constants';

export const loadProfileInfo = async (
  token: string,
  userId: string,
  callback: Function,
) => {
  try {
    const response = await fetch(PROFILE_INFO_ENDPOINT + `${userId}/`, {
      method: 'GET',
      headers: {
        Authorization: 'Token ' + token,
      },
    });
    const status = response.status;
    if (status === 200) {
      const info = await response.json();
      let {name, biography, website} = info;
      callback({name, biography, website});
    }
  } catch (error) {
    Alert.alert(
      'Something went wrong! 😭',
      "Would you believe me if I told you that I don't know what happened?",
    );
  }
};

export const loadAvatar = async (
  token: string,
  userId: string,
  callback: Function,
) => {
  try {
    const response = await RNFetchBlob.config({
      fileCache: true,
      appendExt: 'jpg',
    }).fetch('GET', AVATAR_PHOTO_ENDPOINT + `${userId}/`, {
      Authorization: 'Token ' + token,
    });
    const status = response.info().status;
    if (status === 200) {
      callback(response.path());
    } else {
      callback('');
    }
  } catch (error) {
    console.log(error);
  }
};

export const loadCover = async (
  token: string,
  userId: string,
  callback: Function,
) => {
  try {
    let response = await RNFetchBlob.config({
      fileCache: true,
      appendExt: 'jpg',
    }).fetch('GET', COVER_PHOTO_ENDPOINT + `${userId}/`, {
      Authorization: 'Token ' + token,
    });
    const status = response.info().status;
    if (status === 200) {
      callback(response.path());
    } else {
      callback('');
    }
  } catch (error) {
    console.log(error);
  }
};

const integratedSocialPostsEndpoints: {[social: string]: string} = {
  Facebook: GET_FB_POSTS_ENDPOINT,
  Instagram: GET_IG_POSTS_ENDPOINT,
  Twitter: GET_TWITTER_POSTS_ENDPOINT,
};

export const loadSocialPosts: (
  userId: string,
  socialType: string,
) => Promise<SocialAccountType> = async (userId, socialType) => {
  const token = await AsyncStorage.getItem('token');
  const endpoint = integratedSocialPostsEndpoints[socialType];
  const accountData: SocialAccountType = {};
  accountData.posts = [];
  try {
    const response = await fetch(endpoint + `${userId}/`, {
      method: 'GET',
      headers: {
        Authorization: 'Token ' + token,
      },
    });
    if (response.status === 200) {
      const body = await response.json();
      accountData.handle = body.handle;
      accountData.posts = body.posts;
      accountData.profile_pic = body.profile_pic;
    } else {
      throw 'Unable to fetch posts data from ' + socialType;
    }
  } catch (error) {
    console.log(error);
  }
  return accountData;
};

export const loadMoments: (
  userId: string,
  token: string,
) => Promise<MomentType[]> = async (userId, token) => {
  let moments: MomentType[] = [];
  try {
    const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, {
      method: 'GET',
      headers: {
        Authorization: 'Token ' + token,
      },
    });
    const status = response.status;
    if (status === 200) {
      const data = await response.json();
      moments = data;
    } else {
      console.log('Could not load moments!');
      return [];
    }
  } catch (err) {
    console.log(err);
    return [];
  }
  return moments;
};

export const loadRecentlySearchedUsers = async (callback: Function) => {
  try {
    const asyncCache = await AsyncStorage.getItem('@recently_searched_users');
    asyncCache != null ? callback(JSON.parse(asyncCache)) : null;
  } catch (e) {
    console.log(e);
  }
};