Last active
February 26, 2020 08:01
-
-
Save pratikagashe/6de28a7314361ad45fb9051e2d755fb3 to your computer and use it in GitHub Desktop.
client users component updated: postgraphile demo
This file contains hidden or 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
import React, { useState, useEffect } from 'react' | |
import { useGetUsers } from '../utils/services' | |
import { User } from '../generated/graphql' | |
import UpdateUser from './UpdateUser' | |
const Users: React.FC = () => { | |
const [users, setUsers] = useState() | |
const { data, error, loading } = useGetUsers() | |
const [values, setValues] = useState({ | |
id: 0, | |
name: '', | |
}) | |
const editUser = (userId: number, userName: string) => { | |
setValues({ | |
...values, | |
id: userId, | |
name: userName, | |
}) | |
} | |
useEffect(() => { | |
if (data) { | |
if (data && data.allUsers && data.allUsers.nodes) { | |
setUsers(data.allUsers.nodes) | |
} | |
} | |
if (error) { | |
console.log(error) | |
} | |
if (loading) { | |
console.log(loading) | |
} | |
}, [data, error, loading]) | |
return ( | |
<> | |
<h2>Hello users,</h2> | |
{users && | |
users.length > 0 && | |
users.map((user: User, index: number) => ( | |
<p key={`user_${index}`}> | |
{user.name}{' '} | |
<button onClick={() => editUser(user.id, user.name)}> | |
Edit | |
</button> | |
</p> | |
))} | |
<UpdateUser id={values.id} name={values.name} /> | |
</> | |
) | |
} | |
export default Users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment