Skip to content

Commit

Permalink
feat: add RandomFact types and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc-resourcify committed Jul 26, 2023
1 parent b07fa9a commit 610fedf
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sanity/deskStructure.js
Original file line number Diff line number Diff line change
@@ -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) =>
Expand Down
3 changes: 3 additions & 0 deletions sanity/schema.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,6 +21,7 @@ export const schema: { types: SchemaTypeDefinition[] } = {
language,
podcast,
publication,
randomFact,
school,
shortText,
skill,
Expand Down
27 changes: 27 additions & 0 deletions src/pages/api/random-facts.ts
Original file line number Diff line number Diff line change
@@ -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;
24 changes: 24 additions & 0 deletions src/queries/random-facts.ts
Original file line number Diff line number Diff line change
@@ -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
}`;
1 change: 1 addition & 0 deletions src/schemas/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default defineType({
name: 'fiction',
title: 'Fiction?',
type: 'boolean',
initialValue: false,
}),
defineField({
name: 'cover',
Expand Down
38 changes: 38 additions & 0 deletions src/schemas/randomFact.ts
Original file line number Diff line number Diff line change
@@ -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' }],
}),
],
});
7 changes: 7 additions & 0 deletions src/types/RandomFact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface RandomFact {
_id: string;
name: string;
description: string;
trueFact: boolean;
explanation: string;
}

0 comments on commit 610fedf

Please sign in to comment.