forked from b00tc4mp/isdi-parttime-202403
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrated follow user service and functionality to the app b00tc4mp#175
- Loading branch information
1 parent
fc259b3
commit 83810ae
Showing
6 changed files
with
103 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
staff/daniel-hernandez/project/app/src/services/user/followUser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Config from 'react-native-config'; | ||
import * as SecureStore from 'expo-secure-store'; | ||
import validate from 'com/validation'; | ||
import errors, { FetchError, ParseError, RetrievalError } from 'com/errors'; | ||
|
||
const followUser = targetUserId => { | ||
validate.inputs(targetUserId); | ||
validate.objectId(targetUserId); | ||
|
||
return (async () => { | ||
let userToken, res, body; | ||
|
||
try { | ||
userToken = await SecureStore.getItemAsync(Config.USER_TOKEN_KEY); | ||
} catch (error) { | ||
throw new RetrievalError(`Token retrieval failed: ${error.message}`); | ||
} | ||
|
||
if (!userToken) throw new RetrievalError('No token found'); | ||
|
||
try { | ||
res = await fetch(`${Config.API_URL}/api/v1/user/follow/${targetUserId}`, { | ||
method: 'PATCH', | ||
headers: { Authorization: `Bearer ${userToken}` } | ||
}); | ||
} catch (error) { | ||
throw new FetchError(`Fetch failed: ${error.message}`); | ||
} | ||
|
||
if (res.status === 204) return; | ||
|
||
try { | ||
body = await res.json(); | ||
} catch (error) { | ||
throw new ParseError(`Error parse failed: ${error.message}`); | ||
} | ||
|
||
const { error, message } = body; | ||
const constructor = errors[error]; | ||
|
||
throw new constructor(message); | ||
})(); | ||
}; | ||
|
||
export default followUser; |