aboutsummaryrefslogtreecommitdiff
path: root/src/services/ReportingService.ts
blob: 76883e8126c444ce6387cdfae0580d5da020d657 (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
//Common moments api abstracted out here

import {REPORT_ISSUE_ENDPOINT} from '../constants';
import {Alert} from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import {ERROR_SOMETHING_WENT_WRONG, MARKED_AS_MSG} from '../constants/strings';

export const sendReport = async (
  moment_id: string,
  message: string,
  callback?: Function,
) => {
  try {
    let token = await AsyncStorage.getItem('token');
    let response = await fetch(REPORT_ISSUE_ENDPOINT, {
      method: 'POST',
      body: JSON.stringify({
        resource_id: moment_id,
        type: 'content',
        reason: message,
      }),
      headers: {
        Authorization: 'Token ' + token,
      },
    });

    let statusCode = response.status;
    if (statusCode === 200) {
      Alert.alert(MARKED_AS_MSG(message.split(' ')[2]));
    } else {
      Alert.alert(ERROR_SOMETHING_WENT_WRONG);
    }
    if (callback) {
      callback();
    }
  } catch (error) {
    Alert.alert(ERROR_SOMETHING_WENT_WRONG);
    console.log(
      'Something went wrong! 😭',
      'Unable able to retrieve data',
      error,
    );
  }
};