aboutsummaryrefslogtreecommitdiff
path: root/src/services/MomentServices.ts
blob: 46ca135161ad5fe3613defaa6857995a2d65fb13 (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
//Common moments api abstracted out here
import {COMMENTS_ENDPOINT, MOMENTS_ENDPOINT} from '../constants';
import {Alert} from 'react-native';
import {MomentType} from '../types';

//Get all comments for a moment
export const getMomentComments = async (
  momentId: string,
  callback: Function,
  token: string,
) => {
  try {
    const response = await fetch(COMMENTS_ENDPOINT + '?moment_id=' + momentId, {
      method: 'GET',
      headers: {
        Authorization: 'Token ' + token,
      },
    });
    const status = response.status;
    if (status === 200) {
      const comments = await response.json();
      callback(comments);
    } else {
      console.log('Could not load comments');
    }
  } catch (error) {
    console.log('Could not load comments', error);
  }
};

//Post a comment on a moment
export const postMomentComment = async (
  commenter: string,
  comment: string,
  momentId: string,
  token: string,
) => {
  try {
    const request = new FormData();
    request.append('moment_id', momentId);
    request.append('commenter', commenter);
    request.append('comment', comment);
    const response = await fetch(COMMENTS_ENDPOINT, {
      method: 'POST',
      headers: {
        Authorization: 'Token ' + token,
      },
      body: request,
    });
    const status = response.status;
    if (status === 200) {
      const response_data = await response.json();
      return response_data;
    } else {
      Alert.alert('Something went wrong! 😭', 'Not able to post a comment');
      return {};
    }
  } catch (error) {
    Alert.alert(
      'Something went wrong! 😭',
      'Not able to post a comment',
      error,
    );
    return {};
  }
};

//Get count of comments for a moment
export const getMomentCommentsCount = async (
  momentId: string,
  callback: Function,
  token: string,
) => {
  try {
    const response = await fetch(COMMENTS_ENDPOINT + `${momentId}/`, {
      method: 'GET',
      headers: {
        Authorization: 'Token ' + token,
      },
    });
    const status = response.status;
    if (status === 200) {
      const response_data = await response.json();
      callback(response_data.count);
    } else {
      console.log(
        'Something went wrong! 😭',
        'Not able to retrieve comments count',
      );
    }
  } catch (error) {
    console.log(
      'Something went wrong! 😭',
      'Not able to retrieve comments count',
      error,
    );
  }
};

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;
};