aboutsummaryrefslogtreecommitdiff
path: root/src/services/NotificationService.ts
blob: c5c843f5f9f6fa8def107411f0d037a69f41b0a0 (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
import AsyncStorage from '@react-native-community/async-storage';
import {NOTIFICATIONS_ENDPOINT} from '../constants';
import {NotificationType} from '../types';

export const getNotificationsData: () => Promise<NotificationType[]> =
  async () => {
    try {
      const token = await AsyncStorage.getItem('token');
      const response = await fetch(NOTIFICATIONS_ENDPOINT, {
        method: 'GET',
        headers: {
          Authorization: 'Token ' + token,
        },
      });
      if (response.status === 200) {
        const data: any[] = await response.json();
        let typedData: NotificationType[] = [];
        for (const o of data) {
          typedData.push({
            ...o.notification,
            unread: false,
          });
        }
        return typedData;
      }
      return [];
    } catch (error) {
      console.log('Unable to fetch notifications');
      return [];
    }
  };