Skip to content

Commit

Permalink
Add read-only property to membershipYear status
Browse files Browse the repository at this point in the history
field, improve user experience
  • Loading branch information
simenandre committed Nov 24, 2023
1 parent 2e8b2b8 commit 882ceed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions schemas/membership-year.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { HiBookOpen } from 'react-icons/hi2';
import { defineType } from 'sanity';

function isDevMode() {
const devInSearch = window ? window.location.search.includes('dev') : false;
const devInLocalStorage = localStorage
? localStorage.getItem('devMode') === 'true'
: false;
const devModeEnabled = devInLocalStorage || devInSearch;
return devModeEnabled;
}

const isDevelopment = process.env.NODE_ENV === 'development';
const showStatus = (isDevelopment || isDevMode());

export default defineType({
name: 'membershipYear',
type: 'document',
Expand All @@ -14,6 +26,7 @@ export default defineType({
validation: Rule => Rule.required(),
// Default to current year
initialValue: new Date().getFullYear(),
readOnly: ({ document }) => Boolean(document?._id),
},
{
name: 'status',
Expand All @@ -26,6 +39,7 @@ export default defineType({
],
},
validation: Rule => Rule.required(),
readOnly: showStatus,
},
],
preview: {
Expand Down
5 changes: 5 additions & 0 deletions schemas/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default defineType({
type: 'document',
title: 'Medlemskap',
icon: HiIdentification,
description: 'Heisann der!',
preview: {
select: {
givenName: 'member.givenName',
Expand Down Expand Up @@ -56,11 +57,15 @@ export default defineType({
],
fields: [
{
title: 'Medlemskapsår',
name: 'year',
type: 'reference',
to: [{ type: 'membershipYear' }],
validation: Rule => Rule.required(),
initialValue: getCurrentYearRef,
options: {
disableNew: true,
}
},
{
name: 'status',
Expand Down

0 comments on commit 882ceed

Please sign in to comment.