Skip to content

Commit

Permalink
feat: fetching country quizzes and listing
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEscaleira committed Feb 13, 2024
1 parent 75a0f0f commit 573247a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .graphqlrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"schema": "http://localhost:8000/graphql"
"schema": "http://localhost:8000/graphql",
"documents": "src/**/*.{graphql,js,ts,jsx,tsx}"
}
13 changes: 9 additions & 4 deletions src/__generated__/gql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions src/__generated__/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 59 additions & 7 deletions src/pages/Game/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
import { useState } from "react";
import { Typography, Button } from "@material-tailwind/react";
import { useCallback, useEffect, useState } from "react";
import { useLazyQuery } from "@apollo/client";
import { Typography, Button, Card } from "@material-tailwind/react";
import { Loader2 } from "lucide-react";
import { Tooltip } from "react-tooltip";
import { useCountries } from "use-react-countries";
import { gql } from "@generated/gql.ts";
import { MapChart } from "./MapChart.tsx";

const GET_COUNTRY_QUIZZES = gql(/* GraphQL */ `
query CountryQuizzes($country: String!) {
quizList(country: $country) {
id
title
description
difficulty
timeLimit
image
tags
questions {
question
type
options {
correct
text
}
}
createdAt
updatedAt
creator {
lastName
}
}
}
`);

export function Component() {
const [fetchCountryDetails, { data, loading }] = useLazyQuery(GET_COUNTRY_QUIZZES);
const [selectedTooltipContent, setSelectedTooltipContent] = useState("");
const [selectedCountry, setSelectedCountry] = useState("");

const { countries } = useCountries();
const countyDetails = countries.find(({ name }: { name: string }) => name.toLowerCase() === selectedCountry.toLowerCase());
const countyDetails = useCallback(
() => countries.find(({ name }: { name: string }) => name.toLowerCase() === selectedCountry.toLowerCase()),
[selectedCountry]
);

console.log(countries);
useEffect(() => {
if (selectedCountry) {
fetchCountryDetails({ variables: { country: selectedCountry } });
}
}, [selectedCountry]);

return (
<div className="flex h-full w-full flex-col items-center">
Expand All @@ -24,14 +62,28 @@ export function Component() {

{selectedCountry && (
<>
<div className="mb-4 mt-8 flex items-center gap-8">
<Typography variant="h2" className="text-2xl flex items-center gap-2">
<img src={countyDetails.flags.svg} alt={selectedCountry} className="h-5 w-5 rounded-full object-cover" />
<div className="mb-4 mt-8 flex items-center gap-8 border-b-2 pb-4">
<Typography variant="h2" className="flex items-center gap-2 text-2xl">
<img
src={countyDetails().flags.svg}
alt={selectedCountry}
className="h-5 w-5 rounded-full object-cover"
/>

{selectedCountry}
</Typography>
<Button className="h-10">View country information</Button>
</div>
{loading && <Loader2 size={60} className="mt-10 animate-spin" />}

{!loading && data?.quizList && data?.quizList.length === 0 && (
<Typography>No quizzes found for this country :(</Typography>
)}

{!loading &&
data?.quizList &&
data?.quizList.length > 0 &&
data?.quizList.map(({ title }) => <Card key={title}>{title}</Card>)}
</>
)}
</div>
Expand Down

0 comments on commit 573247a

Please sign in to comment.