Skip to content

Commit

Permalink
fix(frontend): fix publishing redirection
Browse files Browse the repository at this point in the history
it was not happening because we were not calling the appropiate actions
  • Loading branch information
horacioh committed Feb 27, 2023
1 parent 48f9783 commit 01f4e10
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 35 deletions.
56 changes: 39 additions & 17 deletions frontend/app/src/components/titlebar/publish-share.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {useState} from 'react'
import {useRoute} from 'wouter'
import * as PopoverPrimitive from '@radix-ui/react-popover'
import {Box} from '@components/box'
import {useSelector} from '@xstate/react'
import {Icon} from '@components/icon'
import {isProduction, MINTTER_GATEWAY_URL} from '@app/constants'
import {PublicationActor} from '@app/publication-machine'
import {MainActor} from '@app/hooks/main-actor'
import {useDocPublications, useSiteList} from '@app/hooks/sites'
import {Button} from '@components/button'
import {PublicationActor} from '@app/publication-machine'
import {styled} from '@app/stitches.config'
import {AccessURLRow} from '@components/url'
import {usePublicationDialog} from './publication-dialog'
import {MainActor} from '@app/hooks/main-actor'
import {WebPublicationRecord} from '@mintter/shared'
import {EXPERIMENTS} from '@app/utils/experimental'
import {useNostr} from '@app/utils/nostr'
import * as DialogPrimitive from '@radix-ui/react-dialog'
import {toast} from 'react-hot-toast'
import {hostnameStripProtocol} from '@app/utils/site-hostname'
import {Box} from '@components/box'
import {Button} from '@components/button'
import {dialogContentStyles, overlayStyles} from '@components/dialog-styles'
import {Icon} from '@components/icon'
import {TextField} from '@components/text-field'
import {hostnameStripProtocol} from '@app/utils/site-hostname'
import {AccessURLRow} from '@components/url'
import {WebPublicationRecord} from '@mintter/shared'
import * as DialogPrimitive from '@radix-ui/react-dialog'
import * as PopoverPrimitive from '@radix-ui/react-popover'
import {useSelector} from '@xstate/react'
import {useEffect, useRef, useState} from 'react'
import {toast} from 'react-hot-toast'
import {useRoute} from 'wouter'
import {usePublicationDialog} from './publication-dialog'

const StyledOverlay = styled(DialogPrimitive.Overlay, overlayStyles)
const StyledContent = styled(DialogPrimitive.Content, dialogContentStyles)
Expand Down Expand Up @@ -208,6 +208,27 @@ export function PublishShareButton({mainActor}: {mainActor: MainActor}) {
const publicationDialog = usePublicationDialog(mainActor)
const nostrPostDialog = useNostrPostDialog()
const publications = useDocPublications(docId)
let isSaving = useRef(false)
useEffect(() => {
if (mainActor.type == 'publication') {
isSaving.current = false
} else {
mainActor.actor.subscribe((state) => {
if (state.matches('editing.saving')) {
console.log('subscribe change TRUE!', state.value)
isSaving.current = true
} else {
console.log('subscribe change FALSE!', state.value)
isSaving.current = false
}
})
mainActor.actor.onTransition((state) => {
if (state.changed && state.matches('editing.saving')) {
} else {
}
})
}
}, [mainActor])

if (!isDraft && !isPublic && !isPublicB) return null
return (
Expand All @@ -224,6 +245,7 @@ export function PublishShareButton({mainActor}: {mainActor: MainActor}) {
>
<PopoverPrimitive.Trigger asChild>
<button
disabled={isSaving.current}
onClick={(e) => {
e.preventDefault()
if (isOpen) {
Expand All @@ -238,7 +260,7 @@ export function PublishShareButton({mainActor}: {mainActor: MainActor}) {
}}
className={`titlebar-button success outlined ${
isOpen ? 'active' : ''
}`}
} ${isSaving.current ? 'disabled' : ''}`}
data-testid="button-publish"
>
{mainActor.actor?.id === 'editor' ? (
Expand All @@ -254,7 +276,7 @@ export function PublishShareButton({mainActor}: {mainActor: MainActor}) {
)}
</button>
</PopoverPrimitive.Trigger>
<PopoverPrimitive.Portal style={{}}>
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content
style={{
zIndex: 200000,
Expand Down
18 changes: 11 additions & 7 deletions frontend/app/src/draft-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,17 @@ export function createDraftMachine({
]
: contentChanges

await updateDraft({
documentId: context.documentId,
changes,
})
// TODO: update document
client.removeQueries([queryKeys.GET_DRAFT, context.documentId])
client.invalidateQueries([queryKeys.GET_DRAFT_LIST])
if (changes.length != 0) {
await updateDraft({
documentId: context.documentId,
changes,
})
// TODO: update document
client.removeQueries([queryKeys.GET_DRAFT, context.documentId])
client.invalidateQueries([queryKeys.GET_DRAFT_LIST])
} else {
console.log('NO CHANGES TO SEND')
}
}

return getDraftQuery(client, context.documentId)
Expand Down
18 changes: 9 additions & 9 deletions frontend/app/src/hooks/main-actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PublicationActor,
} from '@app/publication-machine'
import {appQueryClient} from '@app/query-client'
import {publishDraft} from '@mintter/shared'
import {Publication, publishDraft} from '@mintter/shared'
import {useMemo} from 'react'
import {toast} from 'react-hot-toast'
import {Editor} from 'slate'
Expand All @@ -24,7 +24,7 @@ export type MainActorOptions = Partial<{
editor: Editor
client: QueryClient
//TODO: add proper type
publishDraft: any
publishDraft?: ReturnType<typeof publishDraft>
}>

export function useMainActor(props: MainActorOptions = {}) {
Expand Down Expand Up @@ -57,28 +57,28 @@ export function useMainActor(props: MainActorOptions = {}) {
editor,
}).withConfig({
actions: {
afterPublish: (_, event) => {
console.log('draftMachine afterPublish')
afterPublish: (c, event) => {
console.log('===== PUBLISHING: AFTER PUBLISH ===', c, event)

// invoke('emit_all', {
// event: 'document_published',
// })
setLocation(`/p/${event.data.document?.id}/${event.data.version}`, {
replace: true,
})

republishDoc.mutateAsync(event.data)
toast.success('Draft published Successfully!')
},
},
services: {
// @ts-ignore
publishDraft: props.publishDraft
? props.publishDraft
: async (context) => {
console.log('draftMachine publishDraft', context)
: (context) => {
console.log('===== PUBLISHING: PUBLISH SERVICE === ', context)

const pub = await publishDraft(context.documentId)
await republishDoc.mutateAsync(pub)
return pub
return publishDraft(context.documentId)
},
},
})
Expand Down
4 changes: 4 additions & 0 deletions frontend/app/src/styles/titlebar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
max-height: 1em;
line-height: 1;
}

&.disabled {
opacity: 0.5;
}
}

.button-group {
Expand Down
16 changes: 14 additions & 2 deletions frontend/shared/src/client/drafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export async function getDraft(
documentId: string,
rpc: Transport = transport,
): Promise<Document> {
return await createPromiseClient(Drafts, rpc).getDraft({documentId})
try {
let draft = await createPromiseClient(Drafts, rpc).getDraft({documentId})
return draft
} catch (error) {
throw new Error(`DetDraft ERROR: ${JSON.stringify(error)}`)
}
}

export type DocumentChanges = {
Expand All @@ -78,5 +83,12 @@ export async function updateDraftV2(
documentChanges: DocumentChanges,
rpc: Transport = transport,
) {
return await createPromiseClient(Drafts, rpc).updateDraftV2(documentChanges)
try {
let result = await createPromiseClient(Drafts, rpc).updateDraftV2(
documentChanges,
)
return result
} catch (error) {
throw new Error(`UPDATE DRAFT ERROR: ${JSON.stringify(error)}`)
}
}

1 comment on commit 01f4e10

@vercel
Copy link

@vercel vercel bot commented on 01f4e10 Feb 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.