diff options
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/BlockUserService.ts | 98 | ||||
| -rw-r--r-- | src/services/index.ts | 1 |
2 files changed, 99 insertions, 0 deletions
diff --git a/src/services/BlockUserService.ts b/src/services/BlockUserService.ts new file mode 100644 index 00000000..56243729 --- /dev/null +++ b/src/services/BlockUserService.ts @@ -0,0 +1,98 @@ +//Abstracted common block user api calls out here + +import {Alert} from 'react-native'; +import {BLOCK_USER_ENDPOINT} from '../constants'; + +export const loadBlockedUsers = async ( + userId: string, + token: string, + callback: Function, +) => { + try { + const response = await fetch(BLOCK_USER_ENDPOINT + `?user_id=${userId}`, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + if (response.status === 200) { + const body = await response.json(); + callback(body); + } else { + throw new Error(await response.json()); + } + } catch (error) { + console.log(error); + } +}; + +export const blockOrUnblockUser = async ( + blocker: string, + blocked: string, + token: string, + isBlocked: boolean, +) => { + try { + const endpoint = BLOCK_USER_ENDPOINT + (isBlocked ? `${blocker}/` : ''); + + const response = await fetch(endpoint, { + method: isBlocked ? 'DELETE' : 'POST', + headers: { + Authorization: 'Token ' + token, + }, + body: JSON.stringify({ + blocked, + }), + }); + if (Math.floor(response.status / 100) === 2) { + return true; + } else { + console.log(await response.json()); + Alert.alert( + 'Something went wrong! ðŸ˜', + "Would you believe me if I told you that I don't know what happened?", + ); + return false; + } + } catch (error) { + console.log(error); + Alert.alert( + 'Something went wrong! ðŸ˜', + "Would you believe me if I told you that I don't know what happened?", + ); + return false; + } +}; + +export const isUserBlocked = async ( + blocker: string, + blocked: string, + token: string, +) => { + try { + const ext = `${blocked}/?blocker=${blocker}`; + + const response = await fetch(BLOCK_USER_ENDPOINT + ext, { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + + if (Math.floor(response.status / 100) === 2) { + const data = await response.json(); + return data['is_blocked']; + } else { + console.log(await response.json()); + Alert.alert( + 'Something went wrong! ðŸ˜', + "Would you believe me if I told you that I don't know what happened?", + ); + } + } catch (error) { + Alert.alert( + 'Something went wrong! ðŸ˜', + "Would you believe me if I told you that I don't know what happened?", + ); + } +}; diff --git a/src/services/index.ts b/src/services/index.ts index 6d0f4314..3d74290c 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -2,3 +2,4 @@ export * from './UserProfileService'; export * from './SocialLinkingService'; export * from './MomentServices'; export * from './UserFollowServices'; +export * from './BlockUserService'; |
