Skip to content

Commit

Permalink
Merge branch 'main' into vaash/2528 and resolved merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
VarunVAshrit committed Nov 5, 2024
2 parents d59882a + b6fc9ff commit 544d5d8
Show file tree
Hide file tree
Showing 124 changed files with 9,462 additions and 9,650 deletions.
387 changes: 215 additions & 172 deletions e2e/pnpm-lock.yaml

Large diffs are not rendered by default.

1,139 changes: 713 additions & 426 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

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])

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),
}
}
4 changes: 2 additions & 2 deletions sanityv3/plugins/asset-source-fotoware/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const getAuthURL = (requestState: string): string | false => {

const CLIENT_ID = process.env.SANITY_STUDIO_FOTOWARE_CLIENT_ID
const TENANT_URL = process.env.SANITY_STUDIO_FOTOWARE_TENANT_URL

return `${TENANT_URL}/fotoweb/oauth2/authorize?response_type=token&client_id=${CLIENT_ID}&state=${requestState}`
const REDIRECT_URI = process.env.SANITY_STUDIO_FOTOWARE_REDIRECT_ORIGIN
return `${TENANT_URL}/fotoweb/oauth2/authorize?response_type=token&client_id=${CLIENT_ID}&state=${requestState}&redirect_uri=${REDIRECT_URI}`
}

export const getAccessToken = (): string | false => {
Expand Down
Loading

0 comments on commit 544d5d8

Please sign in to comment.