Skip to content

Commit

Permalink
feat: remove Sanity from Hobbies types
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Aug 26, 2023
1 parent 5cab6f7 commit 901bdb1
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 195 deletions.
61 changes: 11 additions & 50 deletions src/app/(public)/about/free-time/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { DocumentNode } from 'graphql/language';
import * as React from 'react';

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

import { QuizData, QuizOption } from '@/components/molecules/RandomFacts/Quiz';
Expand All @@ -12,59 +10,24 @@ import RandomFacts from '@/components/organisms/about-free-time/RandomFacts';
import TvSeries from '@/components/organisms/about-free-time/TvSeries';
import VideoGames from '@/components/organisms/about-free-time/VideoGames';

import { booksQueryQL } from '@/queries/books';
import { podcastsQueryQL } from '@/queries/podcasts';
import { queryBooks } from '@/queries/books';
import { queryPodcasts } from '@/queries/podcasts';
import {
falseRandomFactsQueryQL,
selectedTrueRandomFactsQueryQL,
trueRandomFactsQueryQL,
falseRandomFactsQuery,
queryRandomFacts,
selectedTrueRandomFactsQuery,
trueRandomFactsQuery,
} from '@/queries/random-facts';
import { tvShowsQueryQL } from '@/queries/tv-shows';
import { videoGamesQueryQL } from '@/queries/video-games';
import { queryTvShows } from '@/queries/tv-shows';
import { queryVideoGames } from '@/queries/video-games';

import apolloClient from '../../../../../apollo/apollo-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';

export const metadata = {
title: 'About my Free Time | MartaCodes.it',
description: 'About page',
};

async function queryBooks() {
const { data } = await apolloClient.query({ query: booksQueryQL });

return flattenToArray<Book>(data.books);
}

async function queryPodcasts() {
const { data } = await apolloClient.query({ query: podcastsQueryQL });

return flattenToArray<Podcast>(data.podcasts);
}

async function queryTvShows() {
const { data } = await apolloClient.query({ query: tvShowsQueryQL });

return flattenToArray<TvShow>(data.tvShows);
}

async function queryVideoGames() {
const { data } = await apolloClient.query({ query: videoGamesQueryQL });

return flattenToArray<VideoGame>(data.videoGames);
}

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 @@ -89,16 +52,14 @@ const queryData = async () => {

const loadRandomFactsForQuiz = async () => {
const falseFacts: RandomFact[] = await queryRandomFacts(
falseRandomFactsQueryQL
falseRandomFactsQuery
);

const selectedTrueFacts: RandomFact[] = await queryRandomFacts(
selectedTrueRandomFactsQueryQL
selectedTrueRandomFactsQuery
);

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

const oneFalseFact: RandomFact = shuffleArray(falseFacts)[0];
const threeTrueFacts: RandomFact[] = shuffleArray(selectedTrueFacts).slice(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import GeneralView, {
GeneralViewProps,
} from '@/components/molecules/RandomFacts/GeneralView';

import { trueRandomFactsQueryQL } from '@/queries/random-facts';
import { trueRandomFactsQuery } from '@/queries/random-facts';

import apolloClient from '../../../../../apollo/apollo-client';

Expand All @@ -29,7 +29,7 @@ export const SampleStory = (args: GeneralViewProps) => {
useEffect(() => {
const fetchFacts = async () => {
try {
const randomFactsData = await queryRandomFacts(trueRandomFactsQueryQL);
const randomFactsData = await queryRandomFacts(trueRandomFactsQuery);

setRandomFacts(shuffleArray(randomFactsData));
} catch (error) {
Expand Down
12 changes: 0 additions & 12 deletions src/lib/graphqlUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { DocumentNode } from 'graphql/language';
import {
flattenEntityResponse,
flattenEntityResponseCollection,
StrapiEntityResponse,
StrapiEntityResponseCollection,
} from 'strapi-flatten-graphql';

import apolloClient from '../../apollo/apollo-client';

export function flattenToArray<T extends object>(
entityResponseCollection: StrapiEntityResponseCollection<T>
): T[] {
Expand All @@ -23,12 +20,3 @@ export function flattenToObject<T extends object>(
const flattenedData = flattenEntityResponse(entityResponse);
return flattenedData as T;
}

export async function queryData<T>(
query: DocumentNode,
dataKey: string
): Promise<T[]> {
const { data } = await apolloClient.query({ query });

return flattenToArray<T>(data[dataKey]);
}
25 changes: 12 additions & 13 deletions src/pages/api/hobbies.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { NextApiRequest, NextApiResponse } from 'next';

import { booksQuery } from '@/queries/books';
import { podcastsQuery } from '@/queries/podcasts';
import { tvSeriesQuery } from '@/queries/tv-shows';
import { videoGamesQuery } from '@/queries/video-games';
import { Book } from '@/sanityTypes/Book';
import { Podcast } from '@/sanityTypes/Podcast';
import { TvShow } from '@/sanityTypes/TvSeries';
import { VideoGame } from '@/sanityTypes/VideoGame';
import { queryBooks } from '@/queries/books';
import { queryPodcasts } from '@/queries/podcasts';
import { queryTvShows } from '@/queries/tv-shows';
import { queryVideoGames } from '@/queries/video-games';

import { sanityClient } from '../../../sanity/lib/client';
import { Book } from '@/types/Book';
import { Podcast } from '@/types/Podcast';
import { TvShow } from '@/types/TvShow';
import { VideoGame } from '@/types/VideoGame';

const hobbiesApi = async (req: NextApiRequest, res: NextApiResponse) => {
const books: Book[] = await sanityClient.fetch(booksQuery);
const books: Book[] = await queryBooks();

const podcasts: Podcast[] = await sanityClient.fetch(podcastsQuery);
const podcasts: Podcast[] = await queryPodcasts();

const tvShows: TvShow[] = await sanityClient.fetch(tvSeriesQuery);
const tvShows: TvShow[] = await queryTvShows();

const videoGames: VideoGame[] = await sanityClient.fetch(videoGamesQuery);
const videoGames: VideoGame[] = await queryVideoGames();

res.status(200).json({
books,
Expand Down
16 changes: 0 additions & 16 deletions src/pages/api/home.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/pages/api/jobs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextApiRequest, NextApiResponse } from 'next';

import { queryJobs } from '@/queries/jobs';
import { Job } from '@/sanityTypes/Job';

import { Job } from '@/types/Job';

const jobsApi = async (req: NextApiRequest, res: NextApiResponse) => {
const jobs: Job[] = await queryJobs();
Expand Down
3 changes: 2 additions & 1 deletion src/pages/api/publications.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextApiRequest, NextApiResponse } from 'next';

import { queryPublications } from '@/queries/publications';
import { Publication } from '@/sanityTypes/Publication';

import { Publication } from '@/types/Publication';

const publicationsApi = async (req: NextApiRequest, res: NextApiResponse) => {
const publications: Publication[] = await queryPublications();
Expand Down
8 changes: 4 additions & 4 deletions src/pages/api/random-facts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { flattenToArray } from '@/lib/graphqlUtils';
import { shuffleArray } from '@/lib/helper';

import {
falseRandomFactsQueryQL,
selectedTrueRandomFactsQueryQL,
falseRandomFactsQuery,
selectedTrueRandomFactsQuery,
} from '@/queries/random-facts';

import apolloClient from '../../../apollo/apollo-client';
Expand All @@ -15,11 +15,11 @@ import { RandomFact } from '@/types/RandomFact';

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

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

const oneFalseRandomFact: RandomFact = shuffleArray(falseFacts)[0];
Expand Down
3 changes: 2 additions & 1 deletion src/pages/api/schools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextApiRequest, NextApiResponse } from 'next';

import { querySchools } from '@/queries/schools';
import { School } from '@/sanityTypes/School';

import { School } from '@/types/School';

const schoolsApi = async (req: NextApiRequest, res: NextApiResponse) => {
const schools: School[] = await querySchools();
Expand Down
3 changes: 2 additions & 1 deletion src/pages/api/skills.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextApiRequest, NextApiResponse } from 'next';

import { querySkills } from '@/queries/skills';
import { Skill } from '@/sanityTypes/Skill';

import { Skill } from '@/types/Skill';

const skillsApi = async (req: NextApiRequest, res: NextApiResponse) => {
const skills: Skill[] = await querySkills();
Expand Down
23 changes: 12 additions & 11 deletions src/queries/books.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { gql } from '@apollo/client';
import { groq } from 'next-sanity';

export const booksQuery = groq`
*[_type == "book"] | order(author asc) {
_id,
title,
author,
fiction,
"cover": cover.asset->url,
goodreadsLink,
}`;
import { flattenToArray } from '@/lib/graphqlUtils';

export const booksQueryQL = gql`
import apolloClient from '../../apollo/apollo-client';

import { Book } from '@/types/Book';

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

return flattenToArray<Book>(data.books);
}

export const booksQuery = gql`
query {
books(locale: "en", sort: "author:ASC") {
data {
Expand Down
4 changes: 2 additions & 2 deletions src/queries/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import apolloClient from '../../apollo/apollo-client';
import { Job } from '@/types/Job';

export async function queryJobs() {
const { data } = await apolloClient.query({ query: jobsQueryQL });
const { data } = await apolloClient.query({ query: jobsQuery });

return flattenToArray<Job>(data.jobs);
}

export const jobsQueryQL = gql`
const jobsQuery = gql`
query {
jobs(locale: "en", sort: "startDate:desc") {
data {
Expand Down
4 changes: 2 additions & 2 deletions src/queries/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import apolloClient from '../../apollo/apollo-client';
import { Language } from '@/types/Language';

export async function queryLanguages() {
const { data } = await apolloClient.query({ query: languagesQueryQL });
const { data } = await apolloClient.query({ query: languagesQuery });

return flattenToArray<Language>(data.languages);
}

export const languagesQueryQL = gql`
const languagesQuery = gql`
query {
languages(locale: "en") {
data {
Expand Down
23 changes: 12 additions & 11 deletions src/queries/podcasts.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { gql } from '@apollo/client';
import { groq } from 'next-sanity';

export const podcastsQuery = groq`
*[_type == "podcast"] | order(author asc) {
_id,
name,
author,
language,
"cover": cover.asset->url,
mediaLink,
}`;
import { flattenToArray } from '@/lib/graphqlUtils';

export const podcastsQueryQL = gql`
import apolloClient from '../../apollo/apollo-client';

import { Podcast } from '@/types/Podcast';

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

return flattenToArray<Podcast>(data.podcasts);
}

const podcastsQuery = gql`
query {
podcasts(sort: "author:ASC") {
data {
Expand Down
4 changes: 2 additions & 2 deletions src/queries/publications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import apolloClient from '../../apollo/apollo-client';
import { Publication } from '@/types/Publication';

export async function queryPublications() {
const { data } = await apolloClient.query({ query: publicationQueryQL });
const { data } = await apolloClient.query({ query: publicationQuery });

return flattenToArray<Publication>(data.publications);
}

export const publicationQueryQL = gql`
const publicationQuery = gql`
query {
publications(locale: "en", sort: "rank") {
data {
Expand Down
Loading

0 comments on commit 901bdb1

Please sign in to comment.