From da76c4e9a47fc97b5a1301fcdc2fbf044b9a8233 Mon Sep 17 00:00:00 2001
From: "marta.pancaldi"
Date: Sun, 23 Jul 2023 19:44:07 +0200
Subject: [PATCH] feat: add Book type, schema and query
---
sanity/deskStructure.js | 10 ++
sanity/schema.ts | 15 ++-
src/app/(public)/about/free-time/page.tsx | 20 +++
.../organisms/about-free-time/Books.tsx | 127 +++---------------
src/pages/api/hello.ts | 7 -
src/pages/api/hobbies.ts | 17 +++
src/pages/api/skill-icons.ts | 15 ---
src/queries/books.ts | 11 ++
src/schemas/book.ts | 41 ++++++
src/types/Book.ts | 8 ++
10 files changed, 139 insertions(+), 132 deletions(-)
delete mode 100644 src/pages/api/hello.ts
create mode 100644 src/pages/api/hobbies.ts
delete mode 100644 src/pages/api/skill-icons.ts
create mode 100644 src/queries/books.ts
create mode 100644 src/schemas/book.ts
create mode 100644 src/types/Book.ts
diff --git a/sanity/deskStructure.js b/sanity/deskStructure.js
index a8f07fb..4cddbb9 100644
--- a/sanity/deskStructure.js
+++ b/sanity/deskStructure.js
@@ -1,4 +1,5 @@
const workSection = ['job', 'language', 'publication', 'school', 'skill'];
+const freeTimeSection = ['book'];
const sharedSection = ['shortText', 'skillIcon'];
export const customStructure = (S) =>
@@ -14,6 +15,15 @@ export const customStructure = (S) =>
workSection.map((section) => S.documentTypeListItem(section))
)
),
+ S.listItem()
+ .title('Free Time')
+ .child(
+ S.list()
+ .title('Free Time Documents')
+ .items(
+ freeTimeSection.map((section) => S.documentTypeListItem(section))
+ )
+ ),
S.listItem()
.title('Shared')
.child(
diff --git a/sanity/schema.ts b/sanity/schema.ts
index 6c3d22f..c21d16f 100644
--- a/sanity/schema.ts
+++ b/sanity/schema.ts
@@ -1,14 +1,23 @@
import { type SchemaTypeDefinition } from 'sanity';
-import skillIcon from '@/schemas/skillIcon';
-
+import book from '../src/schemas/book';
import job from '../src/schemas/job';
import language from '../src/schemas/language';
import publication from '../src/schemas/publication';
import school from '../src/schemas/school';
import shortText from '../src/schemas/shortText';
import skill from '../src/schemas/skill';
+import skillIcon from '../src/schemas/skillIcon';
export const schema: { types: SchemaTypeDefinition[] } = {
- types: [job, language, publication, school, shortText, skill, skillIcon],
+ types: [
+ book,
+ job,
+ language,
+ publication,
+ school,
+ shortText,
+ skill,
+ skillIcon,
+ ],
};
diff --git a/src/app/(public)/about/free-time/page.tsx b/src/app/(public)/about/free-time/page.tsx
index d1b3beb..d66746c 100644
--- a/src/app/(public)/about/free-time/page.tsx
+++ b/src/app/(public)/about/free-time/page.tsx
@@ -1,11 +1,29 @@
import * as React from 'react';
+import Books from '@/components/organisms/about-free-time/Books';
+
+import { booksQuery } from '@/queries/books';
+
+import { sanityClient } from '../../../../../sanity/lib/client';
+
+import { Book } from '@/types/Book';
+
export const metadata = {
title: 'About my Free Time | MartaCodes.it',
description: 'About page',
};
+const getData = async () => {
+ const books: Book[] = await sanityClient.fetch(booksQuery);
+
+ return {
+ books,
+ };
+};
+
const AboutFreeTimePage = async () => {
+ const { books } = await getData();
+
return (
@@ -20,6 +38,8 @@ const AboutFreeTimePage = async () => {
my free time :)
+
+
diff --git a/src/components/organisms/about-free-time/Books.tsx b/src/components/organisms/about-free-time/Books.tsx
index 9cdfc93..7ff7d1a 100644
--- a/src/components/organisms/about-free-time/Books.tsx
+++ b/src/components/organisms/about-free-time/Books.tsx
@@ -1,126 +1,39 @@
'use client';
-import { PortableText } from '@portabletext/react';
import Image from 'next/image';
import * as React from 'react';
-import { School } from '@/types/School';
+import { Book } from '@/types/Book';
-export interface EducationProps {
- schools: School[];
+export interface BookProps {
+ books: Book[];
}
-const Education = ({ schools }: EducationProps) => {
+const Books = ({ books }: BookProps) => {
return (
-
Education
+ Best books ever written
-
- {schools.map((school) => (
-
+ {books.map((book) => (
+
- {/* Start School Header - Desktop */}
-
-
-
-
-
-
-
-
{school.degreeName}
-
-
-
-
-
- {school.grade}
-
-
-
-
-
- {school.startYear} — {school.endYear}
-
-
-
-
-
- {/* End School Header - Desktop */}
-
- {/* Start School Header - Mobile */}
-
-
-
-
-
-
-
-
-
{school.degreeName}
-
-
-
-
-
- {school.grade}
-
-
- {school.startYear} — {school.endYear}
-
-
-
- {/* End School Header - Mobile */}
-
- {/* Start School Content */}
-
- {/* End School Content */}
-
+
+
+
+
))}
-
+
);
};
-export default Education;
+export default Books;
diff --git a/src/pages/api/hello.ts b/src/pages/api/hello.ts
deleted file mode 100644
index ac3e359..0000000
--- a/src/pages/api/hello.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
-
-import { NextApiRequest, NextApiResponse } from 'next';
-
-export default function hello(req: NextApiRequest, res: NextApiResponse) {
- res.status(200).json({ name: 'Marta' });
-}
diff --git a/src/pages/api/hobbies.ts b/src/pages/api/hobbies.ts
new file mode 100644
index 0000000..daac1da
--- /dev/null
+++ b/src/pages/api/hobbies.ts
@@ -0,0 +1,17 @@
+import { NextApiRequest, NextApiResponse } from 'next';
+
+import { booksQuery } from '@/queries/books';
+
+import { sanityClient } from '../../../sanity/lib/client';
+
+import { Book } from '@/types/Book';
+
+const hobbiesApi = async (req: NextApiRequest, res: NextApiResponse) => {
+ const books: Book[] = await sanityClient.fetch(booksQuery);
+
+ res.status(200).json({
+ books,
+ });
+};
+
+export default hobbiesApi;
diff --git a/src/pages/api/skill-icons.ts b/src/pages/api/skill-icons.ts
deleted file mode 100644
index 8eccc1a..0000000
--- a/src/pages/api/skill-icons.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import { NextApiRequest, NextApiResponse } from 'next';
-
-import { skillIconQuery } from '@/queries/skill-icons';
-
-import { sanityClient } from '../../../sanity/lib/client';
-
-import { SkillIcon } from '@/types/SkillIcon';
-
-const skillIconsApi = async (req: NextApiRequest, res: NextApiResponse) => {
- const skillIcons: SkillIcon[] = await sanityClient.fetch(skillIconQuery);
-
- res.status(200).json(skillIcons);
-};
-
-export default skillIconsApi;
diff --git a/src/queries/books.ts b/src/queries/books.ts
new file mode 100644
index 0000000..aa0a79d
--- /dev/null
+++ b/src/queries/books.ts
@@ -0,0 +1,11 @@
+import { groq } from 'next-sanity';
+
+export const booksQuery = groq`
+*[_type == "book"] | order(author asc) {
+ _id,
+ title,
+ author,
+ fiction,
+ "cover": cover.asset->url,
+ goodreadsLink,
+}`;
diff --git a/src/schemas/book.ts b/src/schemas/book.ts
new file mode 100644
index 0000000..f8a37bf
--- /dev/null
+++ b/src/schemas/book.ts
@@ -0,0 +1,41 @@
+import { defineField, defineType } from 'sanity';
+
+export default defineType({
+ name: 'book',
+ title: 'Book',
+ type: 'document',
+ fields: [
+ defineField({
+ name: 'title',
+ title: 'Title',
+ type: 'string',
+ }),
+ defineField({
+ name: 'author',
+ title: 'Author',
+ type: 'string',
+ }),
+ defineField({
+ name: 'fiction',
+ title: 'Fiction?',
+ type: 'boolean',
+ }),
+ defineField({
+ name: 'cover',
+ title: 'Cover',
+ type: 'image',
+ }),
+ defineField({
+ name: 'goodreadsLink',
+ title: 'Goodreads Link',
+ type: 'string',
+ }),
+ ],
+ orderings: [
+ {
+ title: 'Author',
+ name: 'authorAsc',
+ by: [{ field: 'author', direction: 'asc' }],
+ },
+ ],
+});
diff --git a/src/types/Book.ts b/src/types/Book.ts
new file mode 100644
index 0000000..80714de
--- /dev/null
+++ b/src/types/Book.ts
@@ -0,0 +1,8 @@
+export interface Book {
+ _id: string;
+ title: string;
+ author: string;
+ fiction: boolean;
+ cover: string;
+ goodreadsLink: string;
+}