Skip to content

Commit

Permalink
feat: move type RandomFact
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Aug 23, 2023
1 parent 15e27d2 commit 6ecec28
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 27 deletions.
30 changes: 19 additions & 11 deletions src/app/(public)/about/free-time/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DocumentNode } from 'graphql/language';
import * as React from 'react';

import { flattenToArray } from '@/lib/graphqlUtils';
Expand All @@ -14,19 +15,18 @@ import VideoGames from '@/components/organisms/about-free-time/VideoGames';
import { booksQueryQL } from '@/queries/books';
import { podcastsQueryQL } from '@/queries/podcasts';
import {
falseRandomFactsQuery,
selectedTrueRandomFactsQuery,
trueRandomFactsQuery,
falseRandomFactsQueryQL,
selectedTrueRandomFactsQueryQL,
trueRandomFactsQueryQL,
} from '@/queries/random-facts';
import { tvShowsQueryQL } from '@/queries/tv-shows';
import { videoGamesQueryQL } from '@/queries/video-games';
import { RandomFact } from '@/sanityTypes/RandomFact';

import apolloClient from '../../../../../apollo/apollo-client';
import { sanityClient } from '../../../../../sanity/lib/client';

import { Book } from '@/types/Book';
import { Podcast } from '@/types/Podcast';
import { RandomFact } from '@/types/RandomFact';
import { TvShow } from '@/types/TvShow';
import { VideoGame } from '@/types/VideoGame';

Expand Down Expand Up @@ -59,6 +59,12 @@ async function queryVideoGames() {
return flattenToArray<VideoGame>(data.videoGames);
}

export async function queryRandomFacts(query: DocumentNode) {
const { data } = await apolloClient.query({ query });

return flattenToArray<RandomFact>(data.randomFacts);
}

const getData = async () => {
const randomFacts: QuizData = await loadRandomFactsForQuiz();

Expand All @@ -82,14 +88,16 @@ const queryData = async () => {
};

const loadRandomFactsForQuiz = async () => {
const falseFacts: RandomFact[] = await sanityClient.fetch(
falseRandomFactsQuery
const falseFacts: RandomFact[] = await queryRandomFacts(
falseRandomFactsQueryQL
);
const selectedTrueFacts: RandomFact[] = await sanityClient.fetch(
selectedTrueRandomFactsQuery

const selectedTrueFacts: RandomFact[] = await queryRandomFacts(
selectedTrueRandomFactsQueryQL
);
const trueFacts: RandomFact[] = await sanityClient.fetch(
trueRandomFactsQuery

const trueFacts: RandomFact[] = await queryRandomFacts(
trueRandomFactsQueryQL
);

const oneFalseFact: RandomFact = shuffleArray(falseFacts)[0];
Expand Down
6 changes: 3 additions & 3 deletions src/components/molecules/RandomFacts/GeneralView.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use client';

import { PortableText } from '@portabletext/react';
import * as React from 'react';
import { useState } from 'react';
import ReactMarkdown from 'react-markdown';

import Button from '@/components/buttons/Button';

import { RandomFact } from '@/sanityTypes/RandomFact';
import { RandomFact } from '@/types/RandomFact';

export interface GeneralViewProps {
randomFacts: RandomFact[];
Expand All @@ -25,7 +25,7 @@ const GeneralView = ({ randomFacts }: GeneralViewProps) => {
<div className=''>
<div className='mb-5 min-h-fit rounded bg-sky-200 p-6 dark:bg-slate-800'>
{factIndex < randomFacts.length && (
<PortableText value={randomFacts[factIndex].description} />
<ReactMarkdown>{randomFacts[factIndex].description}</ReactMarkdown>
)}
{factIndex === randomFacts.length && (
<div className='text-center'>
Expand Down
2 changes: 1 addition & 1 deletion src/components/molecules/RandomFacts/Quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { saveToLocalStorage } from '@/lib/helper';
import Button from '@/components/buttons/Button';
import { localStorageKey } from '@/components/organisms/about-free-time/RandomFacts';

import { RandomFact } from '@/sanityTypes/RandomFact';
import { RandomFact } from '@/types/RandomFact';

export interface QuizOption {
headline: string;
Expand Down
6 changes: 2 additions & 4 deletions src/components/molecules/RandomFacts/QuizAnswers.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use client';

import { PortableText } from '@portabletext/react';
import * as React from 'react';
import { BsFillCheckCircleFill, BsFillXSquareFill } from 'react-icons/bs';
import ReactMarkdown from 'react-markdown';

import { QuizOption } from '@/components/molecules/RandomFacts/Quiz';

Expand Down Expand Up @@ -41,9 +41,7 @@ const QuizAnswers = ({
<span>{answer.headline}</span>
</div>
<div className='rounded bg-blue-100 px-3 py-2 text-xs dark:bg-blue-950'>
{answer.explanation && (
<PortableText value={answer.explanation} />
)}
<ReactMarkdown>{answer.explanation}</ReactMarkdown>
</div>
</li>
))}
Expand Down
16 changes: 8 additions & 8 deletions src/pages/api/random-facts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { NextApiRequest, NextApiResponse } from 'next';

import { shuffleArray } from '@/lib/helper';

import { queryRandomFacts } from '@/app/(public)/about/free-time/page';
import {
falseRandomFactsQuery,
selectedTrueRandomFactsQuery,
falseRandomFactsQueryQL,
selectedTrueRandomFactsQueryQL,
} from '@/queries/random-facts';
import { RandomFact } from '@/sanityTypes/RandomFact';

import { sanityClient } from '../../../sanity/lib/client';
import { RandomFact } from '@/types/RandomFact';

const randomFactsApi = async (req: NextApiRequest, res: NextApiResponse) => {
const trueFacts: RandomFact[] = await sanityClient.fetch(
selectedTrueRandomFactsQuery
const trueFacts: RandomFact[] = await queryRandomFacts(
selectedTrueRandomFactsQueryQL
);

const falseFacts: RandomFact[] = await sanityClient.fetch(
falseRandomFactsQuery
const falseFacts: RandomFact[] = await queryRandomFacts(
falseRandomFactsQueryQL
);

const oneFalseRandomFact: RandomFact = shuffleArray(falseFacts)[0];
Expand Down
55 changes: 55 additions & 0 deletions src/queries/random-facts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { gql } from '@apollo/client';
import { groq } from 'next-sanity';

export const trueRandomFactsQuery = groq`
Expand All @@ -10,6 +11,23 @@ export const trueRandomFactsQuery = groq`
explanation,
}`;

export const trueRandomFactsQueryQL = gql`
query {
randomFacts(locale: "en", filters: { isFactTrue: { eq: true } }) {
data {
id
attributes {
name
isFactTrue
headline
description
explanation
}
}
}
}
`;

export const selectedTrueRandomFactsQuery = groq`
*[_type == "randomFact" && isFactTrue == true && name != "Correspondence"]
{
Expand All @@ -20,6 +38,26 @@ export const selectedTrueRandomFactsQuery = groq`
explanation,
}`;

export const selectedTrueRandomFactsQueryQL = gql`
query {
randomFacts(
locale: "en"
filters: { isFactTrue: { eq: true }, name: { notContains: "(True)" } }
) {
data {
id
attributes {
name
isFactTrue
headline
description
explanation
}
}
}
}
`;

export const falseRandomFactsQuery = groq`
*[_type == "randomFact" && isFactTrue == false]
{
Expand All @@ -30,6 +68,23 @@ export const falseRandomFactsQuery = groq`
explanation,
}`;

export const falseRandomFactsQueryQL = gql`
query {
randomFacts(locale: "en", filters: { isFactTrue: { eq: false } }) {
data {
id
attributes {
name
isFactTrue
headline
description
explanation
}
}
}
}
`;

export const randomFactsQuery = groq`
*[_type == "randomFact"] {
_id,
Expand Down
8 changes: 8 additions & 0 deletions src/types/RandomFact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface RandomFact {
id: string;
name: string;
headline: string;
description: TypedObject;
isFactTrue: boolean;
explanation: TypedObject;
}

0 comments on commit 6ecec28

Please sign in to comment.