Skip to content

Commit

Permalink
Add temporary recipe block renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Mar 15, 2020
1 parent a669dbc commit 0d30549
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
8 changes: 7 additions & 1 deletion components/portable-text/serializers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from 'next/link';

import CaptionedImage from '../media/CaptionedImage';
import CaptionedVideo from '../media/CaptionedVideo';
import Recipe from '../recipe/Recipe';

import routesConfig from '../../routes-config';
import { compileDynamicItem } from '../../scripts/compile-routes';
Expand All @@ -13,6 +14,7 @@ import {
SanityCaptionedImage,
SanityCaptionedVideo,
SanityMediaGallery,
SanityRecipe,
} from '../../typings';

const InternalLink: React.FC<SanityMarkNode> = ({ children, mark }) => {
Expand Down Expand Up @@ -71,13 +73,17 @@ const MediaGalleryWrapper: React.FC<SanityBlockType<SanityMediaGallery>> = (prop
</div>
);

const RecipeWrapper: React.FC<SanityBlockType<SanityRecipe>> = (props) => (
<Recipe recipe={props.node} />
);

const richSerializers = {
...simpleSerializers,
types: {
captionedImage: CaptionedImageWrapper,
captionedVideo: CaptionedVideoWrapper,
mediaGallery: MediaGalleryWrapper,
// 'recipe'
recipe: RecipeWrapper,
},
};

Expand Down
17 changes: 17 additions & 0 deletions components/recipe/Recipe.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';

import { SanityRecipe } from '../../typings';

type RecipeProps = {
recipe: SanityRecipe & {
_key: string;
};
};
const Recipe: React.FC<RecipeProps> = ({ recipe, ...props }) => (
<article {...props}>
<h2>{recipe.title}</h2>
<p>{recipe.description}</p>
</article>
);

export default Recipe;
39 changes: 19 additions & 20 deletions sanity/projections.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ const captionedVideoProjection = /* groq */ `{
video->${accessibleVideoProjection}
}`;

const simplePortabletextProjection = /* groq */ `{
const portableTextCustomMarkDefProjection = /* groq */ `{
...,
"markDefs": markDefs[]{
...,
_type == "internalLink" => {
"reference": reference->{
_id,
_type == "internalLink" => {
"reference": reference->{
_id,
"slug": slug.current,
"category": category->{
"slug": slug.current,
"category": category->{
"slug": slug.current,
},
},
},
}
},
}`;

const simplePortabletextProjection = /* groq */ `{
...,
"markDefs": markDefs[] ${portableTextCustomMarkDefProjection}
}`;

const richPortabletextProjection = /* groq */ `{
Expand All @@ -48,18 +50,15 @@ const richPortabletextProjection = /* groq */ `{
_type == "captionedVideo" => ${captionedVideoProjection},
},
},
"markDefs": markDefs[]{
_type == "recipe" => {
...,
_type == "internalLink" => {
"reference": reference->{
_id,
"slug": slug.current,
"category": category->{
"slug": slug.current,
}
}
ingredients[] {
...,
"unit": unit[0],
},
}
method[] ${simplePortabletextProjection},
},
"markDefs": markDefs[] ${portableTextCustomMarkDefProjection},
}`;

const categoryPreviewProjection = /* groq */ `{
Expand Down
1 change: 1 addition & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './routes';
// CMS data
export * from './media';
export * from './portable-text';
export * from './recipe';
export * from './person';
export * from './category';
export * from './tag';
Expand Down
21 changes: 21 additions & 0 deletions typings/recipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SanityBlock } from '.';

export type SanityIngredientUnit = 'kg' | 'ml' | 'l' | 'unitless';

export type SanityIngredient = {
_key: string;
name: string;
quantity: number;
unit: SanityIngredientUnit;
};

export type SanityRecipe = {
_type: string;
title: string;
description: string;
cookingTime: number;
preparationTime: number;
servings: number;
ingredients: SanityIngredient[];
method: SanityBlock[];
};

0 comments on commit 0d30549

Please sign in to comment.