-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #495 from varianter/feature/v3-expand-benefit-schema
v3 - benefit type field
- Loading branch information
Showing
1 changed file
with
35 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,52 @@ | ||
import { defineType } from 'sanity'; | ||
import { richText, title } from '../fields/text'; | ||
import { defineField, defineType } from "sanity"; | ||
import { richText, title } from "../fields/text"; | ||
|
||
export const benefitId = "benefit"; | ||
export const benefitTypeId = "benefitType"; | ||
|
||
const BENEFIT_TYPE_BASIC_VALUE = "basic"; | ||
const BENEFIT_TYPES = [ | ||
{ title: "Basic", value: BENEFIT_TYPE_BASIC_VALUE }, | ||
{ title: "Gadgets", value: "gadgets" }, | ||
{ title: "Bonus", value: "bonus" }, | ||
{ title: "Pension", value: "pension" }, | ||
{ title: "Salary Growth", value: "salaryGrowth" }, | ||
]; | ||
|
||
const benefitType = defineField({ | ||
name: benefitTypeId, | ||
type: "string", | ||
title: "Benefit Type", | ||
description: | ||
"Choose the type of benefit. Some benefit types include visual graphs that will be displayed together with the text.", | ||
options: { | ||
list: BENEFIT_TYPES, | ||
layout: BENEFIT_TYPES.length > 5 ? "dropdown" : "radio", | ||
}, | ||
initialValue: BENEFIT_TYPE_BASIC_VALUE, | ||
validation: (Rule) => Rule.required(), | ||
}); | ||
|
||
const benefit = defineType({ | ||
name: benefitId, | ||
type: "document", | ||
title: "Benefit", | ||
fields: [ | ||
title, | ||
richText | ||
], | ||
fields: [benefitType, title, richText], | ||
preview: { | ||
select: { | ||
title: title.name, | ||
type: benefitType.name, | ||
}, | ||
prepare({ title }) { | ||
prepare({ title, type }) { | ||
const subtitle = | ||
BENEFIT_TYPES.find((o) => o.value === type)?.title ?? | ||
"Unknown benefit type"; | ||
return { | ||
title, | ||
subtitle: "Benefit", | ||
subtitle, | ||
}; | ||
}, | ||
}, | ||
}) | ||
}); | ||
|
||
export default benefit; | ||
export default benefit; |