aboutsummaryrefslogtreecommitdiff
path: root/src/services/ExploreService.ts
blob: 9b0b4f7177097eccc9bf710c4a33e801cf18c8fb (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
import AsyncStorage from '@react-native-community/async-storage';
import {
  ALL_USERS_ENDPOINT,
  DISCOVER_ENDPOINT,
  SEARCH_BUTTONS_ENDPOPINT,
} from '../constants';
import {EMPTY_PROFILE_PREVIEW_LIST} from '../store/initialStates';
import {
  ExploreSectionType,
  ProfilePreviewType,
  SearchCategoryType,
} from '../types';

export const getAllTaggUsers = async (token: string) => {
  try {
    const response = await fetch(ALL_USERS_ENDPOINT, {
      method: 'GET',
      headers: {
        Authorization: 'Token ' + token,
      },
    });
    const status = response.status;
    if (status === 200) {
      const response_data = await response.json();
      return response_data;
    } else {
      console.log(
        'Something went wrong! 😭',
        'Not able to retrieve tagg users list',
      );
    }
  } catch (error) {
    console.log(
      'Something went wrong! 😭',
      'Not able to retrieve tagg users list',
      error,
    );
  }
};

export const getAllExploreSections = async () => {
  try {
    const token = await AsyncStorage.getItem('token');
    const response = await fetch(DISCOVER_ENDPOINT, {
      method: 'GET',
      headers: {
        Authorization: 'Token ' + token,
      },
    });
    if (response.status !== 200) {
      return EMPTY_PROFILE_PREVIEW_LIST;
    }
    const data = await response.json();
    // TODO (if we return to original explore format): get keys from backend API
    const exploreSections: Record<ExploreSectionType, ProfilePreviewType[]> = {
      'New to Tagg': data.categories.new_to_tagg,
      'People You May Know': data.categories.people_you_may_know,
      'Trending on Tagg': data.categories.trending_on_tagg,
      "Brown '21": data.categories.brown_21,
      "Brown '22": data.categories.brown_22,
      "Brown '23": data.categories.brown_23,
      "Brown '24": data.categories.brown_24,
    };

    return exploreSections;
  } catch (error) {
    console.log('Unable to fetch explore data');
  }
};

export const getDiscoverUsers = async (id: number, category_type: string) => {
  try {
    const token = await AsyncStorage.getItem('token');
    let url = DISCOVER_ENDPOINT + `${id}/`;
    if (category_type === 'badges') {
      url += '?type=badge';
    }
    const response = await fetch(url, {
      method: 'GET',
      headers: {
        Authorization: 'Token ' + token,
      },
    });
    if (response.status !== 200) {
      return EMPTY_PROFILE_PREVIEW_LIST;
    }
    const data = await response.json();
    const users: ProfilePreviewType[] = data.users;
    return users;
  } catch (error) {
    console.log('Error fetching SP user data');
    console.log(error);
    return [];
  }
};

export const getButtons = async () => {
  const token = await AsyncStorage.getItem('token');
  const response = await fetch(SEARCH_BUTTONS_ENDPOPINT, {
    method: 'GET',
    headers: {
      Authorization: 'Token ' + token,
    },
  });

  if (response.status !== 200) {
    return [];
  }

  const data: SearchCategoryType[] = await response.json();
  return data;
};