diff --git a/sanity/deskStructure.js b/sanity/deskStructure.js index 7c9e504..a8564fc 100644 --- a/sanity/deskStructure.js +++ b/sanity/deskStructure.js @@ -1,5 +1,11 @@ const workSection = ['job', 'language', 'publication', 'school', 'skill']; -const freeTimeSection = ['book', 'podcast', 'tvSeries', 'videoGame']; +const freeTimeSection = [ + 'book', + 'podcast', + 'randomFact', + 'tvSeries', + 'videoGame', +]; const sharedSection = ['shortText', 'skillIcon']; export const customStructure = (S) => diff --git a/sanity/schema.ts b/sanity/schema.ts index ec7ad89..f0258dd 100644 --- a/sanity/schema.ts +++ b/sanity/schema.ts @@ -1,5 +1,7 @@ import { type SchemaTypeDefinition } from 'sanity'; +import randomFact from '@/schemas/randomFact'; + import book from '../src/schemas/book'; import job from '../src/schemas/job'; import language from '../src/schemas/language'; @@ -19,6 +21,7 @@ export const schema: { types: SchemaTypeDefinition[] } = { language, podcast, publication, + randomFact, school, shortText, skill, diff --git a/src/pages/api/random-facts.ts b/src/pages/api/random-facts.ts new file mode 100644 index 0000000..debe2a4 --- /dev/null +++ b/src/pages/api/random-facts.ts @@ -0,0 +1,27 @@ +import { NextApiRequest, NextApiResponse } from 'next'; + +import { + falseRandomFactsQuery, + trueRandomFactsQuery, +} from '@/queries/random-facts'; + +import { sanityClient } from '../../../sanity/lib/client'; + +import { RandomFact } from '@/types/RandomFact'; + +const randomFactsApi = async (req: NextApiRequest, res: NextApiResponse) => { + const trueFacts: RandomFact[] = await sanityClient.fetch( + trueRandomFactsQuery + ); + + const falseFacts: RandomFact[] = await sanityClient.fetch( + falseRandomFactsQuery + ); + + res.status(200).json({ + true: trueFacts, + false: falseFacts, + }); +}; + +export default randomFactsApi; diff --git a/src/queries/random-facts.ts b/src/queries/random-facts.ts new file mode 100644 index 0000000..bf573db --- /dev/null +++ b/src/queries/random-facts.ts @@ -0,0 +1,24 @@ +import { groq } from 'next-sanity'; + +export const trueRandomFactsQuery = groq` +*[_type == "randomFact" && isFactTrue == true] { + _id, + name, + description +}`; + +export const falseRandomFactsQuery = groq` +*[_type == "randomFact" && isFactTrue == false] { + _id, + name, + description, + explanation +}`; + +export const randomFactsQuery = groq` +*[_type == "randomFact"] { + _id, + name, + description, + isFactTrue +}`; diff --git a/src/schemas/book.ts b/src/schemas/book.ts index f8a37bf..4d4c7dc 100644 --- a/src/schemas/book.ts +++ b/src/schemas/book.ts @@ -19,6 +19,7 @@ export default defineType({ name: 'fiction', title: 'Fiction?', type: 'boolean', + initialValue: false, }), defineField({ name: 'cover', diff --git a/src/schemas/randomFact.ts b/src/schemas/randomFact.ts new file mode 100644 index 0000000..398c1b8 --- /dev/null +++ b/src/schemas/randomFact.ts @@ -0,0 +1,38 @@ +import { defineField, defineType } from 'sanity'; + +export default defineType({ + name: 'randomFact', + title: 'Random Fact', + type: 'document', + fields: [ + defineField({ + name: 'name', + title: 'Name', + type: 'string', + }), + defineField({ + name: 'headline', + title: 'Headline', + type: 'string', + description: 'What will appear as MC option in the quiz', + }), + defineField({ + name: 'description', + title: 'Description', + type: 'array', + of: [{ type: 'block' }], + }), + defineField({ + name: 'isFactTrue', + title: 'Is fact true?', + type: 'boolean', + initialValue: true, + }), + defineField({ + name: 'explanation', + title: 'Explanation', + type: 'array', + of: [{ type: 'block' }], + }), + ], +}); diff --git a/src/types/RandomFact.ts b/src/types/RandomFact.ts new file mode 100644 index 0000000..0c37ae3 --- /dev/null +++ b/src/types/RandomFact.ts @@ -0,0 +1,7 @@ +export interface RandomFact { + _id: string; + name: string; + description: string; + trueFact: boolean; + explanation: string; +}