Skip to content
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
4 changes: 3 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Routes, Route } from "react-router-dom";
import axios from "axios";
import { initializeApp } from "firebase/app";
import { setPersistence, getAuth, inMemoryPersistence } from "firebase/auth";
import { useLogin, LoadingScreen, AuthProvider } from "@hex-labs/core";
import { useLogin, LoadingScreen, AuthProvider, Header, Footer } from "@hex-labs/core";

import UserData from './components/UserData';

Expand Down Expand Up @@ -48,12 +48,14 @@ export const App = () => {
// useAuth hook to retrieve the user's login details.
return (
<AuthProvider app={app}>
<Header children={null} />

{/* Setting up our React Router to route to all the different pages we may have */}
<Routes>
<Route path="/" element={<UserData />} />
</Routes>

<Footer />
</AuthProvider>
);
};
Expand Down
79 changes: 78 additions & 1 deletion src/components/UserCard.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import {
Box,
Button,
Code,
Flex,
HStack,
ListItem,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
Text,
UnorderedList,
useDisclosure,
} from "@chakra-ui/react";
import React from "react";
import { apiUrl, Service } from "@hex-labs/core";
import axios from "axios";
import React, { useEffect, useState } from "react";

type Props = {
user: any;
};

type ModalProps = {
isOpen: boolean;
onClose: () => void;
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,
Expand All @@ -23,16 +42,72 @@ type Props = {
// the hexathons that the user has applied to. You can use the /applications endpoint of the registration service to do this
// and the /hexathons endpoint of the hexathons service to get a list of all the hexathons.

const UserModal: React.FC<ModalProps> = (props: ModalProps) => {
const [hexathons, setHexathons] = useState<any[]>([]);

const getHexathons = async () => {
const res = await axios.get(apiUrl(Service.HEXATHONS, "/hexathons"));
const hexathons = res.data;
const userApps = await Promise.all(hexathons.map(async (hexathon: any) => {
const res = await axios.get(apiUrl(Service.REGISTRATION, "/applications"), {
params: {
hexathon: hexathon.id,
userId: props.user.userId
}
});
return res.data.length > 0 ? hexathon : null;
}));
setHexathons(userApps.filter((hexathon) => hexathon !== null));
}

useEffect(() => {
if (props.isOpen) {
getHexathons();
}
}, [props.isOpen]);

return (
<Modal isOpen={props.isOpen} onClose={props.onClose}>
<ModalOverlay/>
<ModalContent>
<ModalHeader>User Details</ModalHeader>
<ModalCloseButton/>
<ModalBody>
<Text><strong>Name:</strong> {`${props.user.name.first} ${props.user.name.last}`}</Text>
<Text><strong>Email:</strong> {props.user.email}</Text>
<Text><strong>Phone Number:</strong> {props.user.phoneNumber}</Text>
<Text><strong>User ID:</strong> <Code>{props.user.userId}</Code></Text>
<Box>
<Text><strong>Applied Hexathons:</strong></Text>
{ hexathons.length > 0?
(<UnorderedList>
{hexathons.map((hexathon) => (
<ListItem key={hexathon.id}>
{hexathon.name}
</ListItem>
))}
</UnorderedList>) : <Text>No hexathons applied</Text>
}
</Box>
</ModalBody>
</ModalContent>
</Modal>
);
};

const UserCard: React.FC<Props> = (props: Props) => {
const { isOpen, onOpen, onClose } = useDisclosure();

return (
<>
<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">
Expand All @@ -48,6 +123,8 @@ const UserCard: React.FC<Props> = (props: Props) => {
</Text>
</Flex>
</Box>
<UserModal isOpen={isOpen} onClose={onClose} user={props.user} />
</>
);
};

Expand Down
18 changes: 16 additions & 2 deletions src/components/UserData.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react";
import { apiUrl, Service } from "@hex-labs/core";
import { SimpleGrid, Text } from "@chakra-ui/react";
import { SimpleGrid, Text, Button } from "@chakra-ui/react";
import axios from "axios";
import UserCard from "./UserCard";

Expand Down Expand Up @@ -39,9 +39,17 @@ const UserData: React.FC = () => {
// 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 res = await axios.get(apiUrl(Service.USERS, "/users/hexlabs"));
const data = res.data;
const filtered = data.filter((u: any) =>
String(u?.phoneNumber ?? "")
.replace(/\D/g, "")
.startsWith("470")
);

// 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);
setUsers(filtered);
};
document.title = "Hexlabs Users"
getUsers();
Expand All @@ -55,11 +63,17 @@ const UserData: React.FC = () => {
// 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.

function sortByFirst() {
const sorted = [...users].sort((a, b) => a.name.first.localeCompare(b.name.first));
setUsers(sorted);
}

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>
<Button onClick={sortByFirst}>Sort by First Name</Button>


<SimpleGrid columns={[2, 3, 5]} spacing={6} padding={10}>
Expand Down