Skip to content

Commit

Permalink
Merge branch 'vaash/2528' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
VarunVAshrit committed Nov 5, 2024
2 parents a686530 + 544d5d8 commit c0cb700
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 81 deletions.
5 changes: 4 additions & 1 deletion FeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const CAREERS_CONTACT_FORM = [...GLOBAL_PROD, ...GLOBAL_DEV]
const CONTACT_EQUINOR_FORM = [...GLOBAL_PROD, ...GLOBAL_DEV]
const ORDER_REPORT_FORM = [...GLOBAL_PROD, ...GLOBAL_DEV]
const CAREER_FAIR_AND_VISITS_FORM = [...GLOBAL_PROD, ...GLOBAL_DEV, 'brazil']
const PENSION_FORM = [...GLOBAL_PROD, ...GLOBAL_DEV]

const FANCY_MENU = [...GLOBAL_PROD, ...GLOBAL_DEV]
/* LANDING_PAGE requires FANCY_MENU to work */
Expand Down Expand Up @@ -73,13 +74,15 @@ export default (dataset) => ({
CAREER_FAIR_AND_VISITS_FORM.includes(dataset) ||
CONTACT_EQUINOR_FORM.includes(dataset) ||
ORDER_REPORT_FORM.includes(dataset) ||
SUBSCRIBE_FORM.includes(dataset),
SUBSCRIBE_FORM.includes(dataset) ||
PENSION_FORM.includes(dataset),

HAS_SUBSCRIBE_FORM: SUBSCRIBE_FORM.includes(dataset),
HAS_CAREERS_CONTACT_FORM: CAREERS_CONTACT_FORM.includes(dataset),
HAS_CAREER_FAIR_AND_VISITS_FORM: CAREER_FAIR_AND_VISITS_FORM.includes(dataset),
HAS_ORDER_REPORT_FORM: ORDER_REPORT_FORM.includes(dataset),
HAS_CONTACT_EQUINOR_FORM: CONTACT_EQUINOR_FORM.includes(dataset),
HAS_PENSION_FORM: PENSION_FORM.includes(dataset),

HAS_FANCY_MENU: FANCY_MENU.includes(dataset),
/* LANDING_PAGE requires FANCY_MENU to work */
Expand Down
124 changes: 48 additions & 76 deletions sanityv3/actions/CustomPublishAction.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,56 @@
import { useState } from 'react'
import {
DocumentActionComponent,
DocumentActionConfirmDialogProps,
DocumentActionDescription,
DocumentActionProps,
DocumentActionsContext,
SanityClient,
} from 'sanity'
import { apiVersion } from '../sanity.client'
import { useToast } from '@sanity/ui'
import { useState, useEffect } from 'react'
import { DocumentActionConfirmDialogProps, DocumentActionProps, useDocumentOperation } from 'sanity'

const FIRST_PUBLISHED_AT_FIELD_NAME = 'firstPublishedAt'
const LAST_MODIFIED_AT_FIELD_NAME = 'lastModifiedAt'

const requiresConfirm = ['news', 'localNews']
const requiresFirstPublished = ['news', 'localNews']
export function SetAndPublishAction(props: DocumentActionProps) {
const { patch, publish } = useDocumentOperation(props.id, props.type)
const [isPublishing, setIsPublishing] = useState(false)
const [dialogOpen, setDialogOpen] = useState(false)

const updateCustomPublishFields = async (id: string, client: SanityClient, setFirstPublish: boolean) => {
const currentTimeStamp = new Date().toISOString()
const patch = client.patch(id).set({ [LAST_MODIFIED_AT_FIELD_NAME]: currentTimeStamp })
if (setFirstPublish) patch.set({ [FIRST_PUBLISHED_AT_FIELD_NAME]: currentTimeStamp })

await patch.commit().catch((e) => {
throw e
})
}

export function createCustomPublishAction(originalAction: DocumentActionComponent, context: DocumentActionsContext) {
const client = context.getClient({ apiVersion: apiVersion })
return (props: DocumentActionProps) => {
const [dialogOpen, setDialogOpen] = useState(false)
const originalResult = originalAction(props as DocumentActionProps) as DocumentActionDescription
const toast = useToast()

const handlePublish = async () => {
try {
if (requiresFirstPublished.includes(props.type)) {
await updateCustomPublishFields(
props.draft?._id || props.id,
client,
!props.published?.[FIRST_PUBLISHED_AT_FIELD_NAME],
)
}
originalResult.onHandle && originalResult.onHandle()
setDialogOpen(false)
} catch (e) {
console.error(e)
toast.push({
duration: 7000,
status: 'error',
title: 'Failed to publish, you probably miss the mutation token. Check console for details.',
})
setDialogOpen(false)
}
}

const confirmationBox = requiresConfirm.includes(props.type)
? {
onHandle: () => {
setDialogOpen(true)
},
dialog:
dialogOpen &&
props.draft &&
({
type: 'confirm',
onCancel: () => {
props.onComplete()
setDialogOpen(false)
},
onConfirm: handlePublish,
message: 'Are you sure you want to publish?',
} as DocumentActionConfirmDialogProps),
}
: {}

return {
...originalResult,
onHandle: handlePublish,
...confirmationBox,
useEffect(() => {
// if the isPublishing state was set to true and the draft has changed
// to become `null` the document has been published
if (isPublishing && !props.draft) {
setIsPublishing(false)
}
}, [props.draft])

Check warning on line 18 in sanityv3/actions/CustomPublishAction.ts

View workflow job for this annotation

GitHub Actions / check-code

React Hook useEffect has a missing dependency: 'isPublishing'. Either include it or remove the dependency array

return {
disabled: publish.disabled || dialogOpen,
label: isPublishing ? 'Publishing…' : `Publish`,
onHandle: () => {
// This will update the button text
setDialogOpen(true)
},
dialog:
dialogOpen &&
props.draft &&
({
type: 'confirm',
onCancel: () => {
props.onComplete()
setDialogOpen(false)
},
onConfirm: () => {
const currentTimeStamp = new Date().toISOString()
// set lastModifiedAt date.
patch.execute([{ set: { [LAST_MODIFIED_AT_FIELD_NAME]: currentTimeStamp } }])

//set firstPublishedAt date if not published.
if (!props.published?.[FIRST_PUBLISHED_AT_FIELD_NAME])
patch.execute([{ set: { [FIRST_PUBLISHED_AT_FIELD_NAME]: currentTimeStamp } }])

// Perform the publish
publish.execute()

// Signal that the action is completed
props.onComplete()

setDialogOpen(false)
},
message: 'Are you sure you want to publish?',
} as DocumentActionConfirmDialogProps),
}
}
7 changes: 4 additions & 3 deletions sanityv3/sanity.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
PluginOptions,
SchemaTypeDefinition,
Template,
buildLegacyTheme } from 'sanity'
buildLegacyTheme,
} from 'sanity'

import type {
InputProps,
Expand All @@ -27,7 +28,7 @@ import { DeleteTranslationAction } from './actions/customDelete/DeleteTranslatio
import { documentInternationalization } from '@equinor/document-internationalization'
import { FotowareAssetSource } from './plugins/asset-source-fotoware'
import { BrandmasterAssetSource } from './plugins/asset-source-brandmaster'
import { createCustomPublishAction } from './actions/CustomPublishAction'
import { SetAndPublishAction } from './actions/CustomPublishAction'
import { dataset, projectId } from './sanity.client'
import { DatabaseIcon } from '@sanity/icons'
import { crossDatasetDuplicator } from '@sanity/cross-dataset-duplicator'
Expand Down Expand Up @@ -123,7 +124,7 @@ const getConfig = (datasetParam: string, projectIdParam: string, isSecret = fals
.map((originalAction) => {
switch (originalAction.action) {
case 'publish':
return createCustomPublishAction(originalAction, context)
return ['news', 'localNews'].includes(context.schemaType) ? SetAndPublishAction : originalAction
case 'duplicate':
return createCustomDuplicateAction(originalAction)
default:
Expand Down
4 changes: 4 additions & 0 deletions sanityv3/schemas/objects/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type FormType =
| 'careersContactForm'
| 'orderReportsForm'
| 'careerFairAndVisitsForm'
| 'pensionForm'

const ingressContentType = configureBlockContent({
h2: false,
Expand Down Expand Up @@ -67,6 +68,7 @@ export default {
title: 'Career fairs and visits',
value: 'careerFairAndVisitsForm',
},
Flags.HAS_PENSION_FORM && { title: 'Pension form', value: 'pensionForm' },
].filter((e) => e),
layout: 'dropdown',
},
Expand Down Expand Up @@ -110,6 +112,8 @@ export default {
return 'Careers contact form'
} else if (type == 'orderReportsForm') {
return 'Order reports'
} else if(type == 'pensionForm') {
return 'Pension form'
}
return 'Career fairs and visits'
}
Expand Down
76 changes: 76 additions & 0 deletions sanityv3/schemas/textSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const groups = {
contactForm: { title: 'Contact form', hidden: !Flags.HAS_CONTACT_EQUINOR_FORM },
careerContactForm: { title: 'Careers Contact Form', hidden: !Flags.HAS_CAREERS_CONTACT_FORM },
orderAnnualReportsForm: { title: 'Order annual reports form', hidden: !Flags.HAS_ORDER_REPORT_FORM },
pensionForm: { title: 'Pension form', hidden: !Flags.HAS_PENSION_FORM },
form: { title: 'Form', hidden: !Flags.HAS_FORMS },
cookie: { title: 'Cookie' },
others: { title: 'Others' },
Expand Down Expand Up @@ -339,6 +340,81 @@ const snippets: textSnippet = {
defaultValue: 'Submit form',
group: groups.contactForm,
},
pension_form_name: {
title: 'Name',
defaultValue: 'Name *',
group: groups.pensionForm,
},
pension_form_name_placeholder: {
title: 'Name Placeholder',
defaultValue: 'Jane Doe',
group: groups.pensionForm,
},
pension_form_name_validation: {
title: 'Name validation',
defaultValue: 'Please fill out your name',
group: groups.pensionForm,
},

pension_form_email: {
title: 'Email',
defaultValue: 'Email *',
group: groups.pensionForm,
},
pension_form_email_validation: {
title: 'Email validation',
defaultValue: 'Please fill out a valid email address',
group: groups.pensionForm,
},
pension_form_category: {
title: 'Category',
defaultValue: 'Category',
group: groups.pensionForm,
},
pension_form_category_pension: {
title: 'Pension Category',
defaultValue: 'Pension',
group: groups.pensionForm,
},
pension_form_category_travel_insurance: {
title: 'Travel Insurance Category',
defaultValue: 'Travel Insurance',
group: groups.pensionForm,
},
pension_form_category_other: {
title: 'Other Pension/Insurance Related Category',
defaultValue: 'Other Pension/Insurance Related',
group: groups.pensionForm,
},

pension_form_what_is_your_request: {
title: 'What is your request?',
defaultValue: 'What is your request?',
group: groups.pensionForm,
},
pension_form_what_is_your_request_placeholder: {
title: `Requests Placeholder`,
defaultValue: `Please don't enter any personal information`,
group: groups.pensionForm,
},
pension_form_what_is_your_request_validation: {
title: 'Requests Validation',
defaultValue: 'Please let us know how we may help you',
group: groups.pensionForm,
},

pension_form_submit: {
title: 'Submit Button Text',
defaultValue: 'Submit Form',
group: groups.pensionForm,
},

pension_form_all_fields_mandatory: {
title: 'All fields with * are mandatory',
defaultValue: 'All fields with * are mandatory',
group: groups.pensionForm,
},

career_fair_form_organisation: {
title: 'Organisation',
defaultValue: 'School / Organisation',
Expand Down
3 changes: 3 additions & 0 deletions web/pageComponents/topicPages/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import CareersContactForm from './careersContactForm/CareersContactForm'
import type { FormData } from '../../../types/index'
import { twMerge } from 'tailwind-merge'
import CallToActions from '@sections/CallToActions'
import PensionForm from './PensionForm'



Expand All @@ -24,6 +25,8 @@ const Form = ({ data, anchor, className }: { data: FormData; anchor?: string; cl
return <CareerFairForm />
case 'careersContactForm':
return <CareersContactForm />
case 'pensionForm':
return <PensionForm />
case 'orderReportsForm':
return (
<>
Expand Down
Loading

0 comments on commit c0cb700

Please sign in to comment.