Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lucas Kang Onboarding Project #18

Open
wants to merge 5 commits into
base: startercode
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 109 additions & 27 deletions src/components/UserCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@ import {
Flex,
HStack,
Text,
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
ModalFooter,
ModalBody,
ModalCloseButton,
Button,
useDisclosure,
} from "@chakra-ui/react";
import React from "react";
import userEvent from "@testing-library/user-event";
import axios from "axios";
import React, { useState } from "react";
import { apiUrl, Service } from "@hex-labs/core";

type Props = {
user: any;
};


// TODO: right now, the UserCard only displays the user's name and email. Create a new modal component <UserModal> that
// pops up when the card is clicked. In this modal, list all the user's information including name, email, phoneNumber,
// and userId.
// and userId.

// TODO: Explore if you can display the email as a link to the user's email that will open up the user's
// TODO: Explore if you can display the email as a link to the user's email that will open up the user's
// email client and start a new email to that user. Also explore if you can provide a link to the user's resume.

// TODO: In our database structure, every user has a userId that is unique to them. This is the primary key of the user
Expand All @@ -24,31 +35,102 @@ type Props = {
// and the /hexathons endpoint of the hexathons service to get a list of all the hexathons.

const UserCard: React.FC<Props> = (props: Props) => {
const { isOpen, onClose, onOpen } = useDisclosure();
const [hexathons, setHexathons] = useState<any[]>([]);

const fetchHexathons = async () => {
try {
const response = await axios.get(apiUrl(Service.HEXATHONS, "/hexathons"));
const hexathons = response.data;

const applications = await Promise.all(
hexathons.map(async (hexathon: any) => {
const userApplications = await axios.get(
apiUrl(Service.REGISTRATION, "/applications"),
{
params: {
userId: props.user.userId,
hexathon: hexathon.id,
},
}
);
if (userApplications.data.applications.length > 0) {
return hexathon;
} else {
return null;
}
})
);
setHexathons(applications.filter((hexathon) => hexathon != null));
} catch (error) {
console.log("Error fetching hexathons: ", error);
}
};

return (
<Box
borderWidth="1px"
rounded="lg"
boxShadow="lg"
height="175px"
fontWeight="bold"
alignItems="center"
>
<Flex padding="2" flexDirection="column">
<HStack align="flex-end" justify="space-between">
<Text fontSize='xl'>{`${props.user.name.first} ${props.user.name.last}`}</Text>
</HStack>
<Text
fontSize="sm"
fontWeight="semibold"
justifyContent="justify"
mt="2"
>
{props.user.email}
</Text>
</Flex>
</Box>
<>
<Box
borderWidth="1px"
rounded="lg"
boxShadow="lg"
height="175px"
fontWeight="bold"
alignItems="center"
onClick={onOpen}
>
<Flex padding="2" flexDirection="column">
<HStack align="flex-end" justify="space-between">
<Text fontSize="xl">{`${props.user.name.first} ${props.user.name.last}`}</Text>
</HStack>
<Text
fontSize="sm"
fontWeight="semibold"
justifyContent="justify"
mt="2"
>
{props.user.email}
</Text>
</Flex>
</Box>

<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>
<p>
<a href="https://resumegenius.com/resume-samples">
{props.user.name.first} {props.user.name.last}
</a>
</p>
</ModalHeader>
<ModalCloseButton />
<ModalBody>
<p>
Email:{" "}
<a href={`mailto:${props.user.email}`} style={{ color: "blue" }}>
{props.user.email}
</a>
</p>
<p>Phone numebr: {props.user.phoneNumber}</p>
<p>ID: {props.user.userId}</p>
<p>Hexathons applied: </p>
<ul>
{hexathons.map((hexathon, key) => (
<li key={key}>{hexathon.name}</li>
))}
</ul>
</ModalBody>

<ModalFooter>
<Button onClick={fetchHexathons}>Fetch hexathons</Button>
<Button colorScheme="blue" mr={3} onClick={onClose}>
Close
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
};

export default UserCard;
export default UserCard;
64 changes: 48 additions & 16 deletions src/components/UserData.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useEffect, useState } from "react";
import { apiUrl, Service } from "@hex-labs/core";
import { SimpleGrid, Text } from "@chakra-ui/react";
import { apiUrl, Service, Footer, Header, HeaderItem } from "@hex-labs/core";
import { SimpleGrid, Text, Button, ChakraProvider } from "@chakra-ui/react";
import axios from "axios";
import UserCard from "./UserCard";

const UserData: React.FC = () => {

// The useState hook is used to store state in a functional component. The
// first argument is the initial value of the state, and the second argument
// is a function that can be used to update the state. The useState hook
Expand All @@ -19,14 +18,12 @@ const UserData: React.FC = () => {
// only happen once when the component is loaded.

useEffect(() => {

// This is an example of an async function. The async keyword tells the
// function to wait for the axios request to finish before continuing. This
// is useful because we can't use the data from the request until it is
// finished.

const getUsers = async () => {

// TODO: Use the apiUrl() function to make a request to the /users endpoint of our USERS service. The first argument is the URL
// of the request, which is created for the hexlabs api through our custom function apiUrl(), which builds the request URL based on
// the Service enum and the following specific endpoint URL.
Expand All @@ -37,43 +34,78 @@ const UserData: React.FC = () => {
// Postman will be your best friend here, because it's better to test out the API calls in Postman before implementing them here.

// this is the endpoint you want to hit, but don't just hit it directly using axios, use the apiUrl() function to make the request
const URL = 'https://users.api.hexlabs.org/users/hexlabs';
const URL = "https://users.api.hexlabs.org/users/hexlabs";

try {
const response = await axios.get(
apiUrl(Service.USERS, "/users/hexlabs")
);
const users = response.data;

const filteredUsers = users.filter((user: any) =>
user.phoneNumber?.startsWith("470")
);
setUsers(filteredUsers);
} catch (error) {
console.error("Error fetching users: ", error);
}

// uncomment the line below to test if you have successfully made the API call and retrieved the data. The below line takes
// the raw request response and extracts the actual data that we need from it.
// setUsers(data?.data?.profiles);
};
document.title = "Hexlabs Users"
document.title = "Hexlabs Users";
getUsers();
}, []);
// ^^ The empty array at the end of the useEffect hook tells React that the
// hook should only run once when the component is mounted. If you want it to
// run every time a variable changes, you can put that variable in the array
// and it will run every time that variable changes.


// TODO: Create a function that sorts the users array based on the first name of the users. Then, create a button that
// calls this function and sorts the users alphabetically by first name. You can use the built in sort() function to do this.

const sortUsers = () => {
const sortedUsers = [...users].sort((a, b) => {
if (a.name.first > b.name.first) return -1;
if (a.name.first < b.name.first) return 1;
return 0;
});
setUsers(sortedUsers);
};

return (
<>
<Text fontSize="4xl">Hexlabs Users</Text>
<Text fontSize="2xl">This is an example of a page that makes an API call to the Hexlabs API to get a list of users.</Text>


<ChakraProvider>
<Header
rightItem={<HeaderItem>Sign Out</HeaderItem>}
rightItemMobile={<HeaderItem>Sign Out</HeaderItem>}
>
<HeaderItem>Home</HeaderItem>
<HeaderItem>Profile</HeaderItem>
</Header>
</ChakraProvider>
<div style={{ margin: "40px" }}>
<Text fontSize="4xl">Hexlabs Users</Text>
<Text fontSize="2xl">
This is an example of a page that makes an API call to the Hexlabs API
to get a list of users.
</Text>
<Button onClick={sortUsers}>Sort by first name</Button>
</div>
<SimpleGrid columns={[2, 3, 5]} spacing={6} padding={10}>

{/* Here we are mapping every entry in our users array to a unique UserCard component, each with the unique respective
data of each unique user in our array. This is a really important concept that we use a lot so be sure to familiarize
yourself with the syntax - compartmentalizing code makes your work so much more readable. */}
{ users.map((user) => (
{users.map((user) => (
<UserCard user={user} />
))}

</SimpleGrid>
<ChakraProvider>
<Footer />
</ChakraProvider>
</>
);
};

export default UserData;
export default UserData;