-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.jsx
40 lines (32 loc) · 977 Bytes
/
User.jsx
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
// Libraries
import React, { useEffect, useState } from 'react'
import { API, graphqlOperation } from 'aws-amplify'
import { listUsers } from '../graphql/queries'
const User = ({ children, history }) => {
const [userData, setUserData] = useState()
useEffect(() => {
fetchUser()
}, [])
const fetchUser = async () => {
try {
const userId = localStorage.getItem('user_id')
// Fetching all users from our database
const response = await API.graphql(graphqlOperation(listUsers))
// Filtering users based on the twitter id and setting is as the data
setUserData(
response.data.listUsers.items.filter(
(user) => user.twitterId === userId
)
)
console.log(userData.data.listUsers.items)
} catch (err) {
console.error(err)
}
}
if (!userData) {
window.location.href = '/'
} else {
return <React.Fragment>{children(userData)}</React.Fragment>
}
}
export default User