Skip to content

Commit

Permalink
feat: send publish request on button press
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn authored Sep 2, 2021
1 parent 83d2704 commit 75d5cfc
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/my-gatsby-site/jaen-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
remote: 'snek-at/jaen-template',
plugins: {
pages: {
resolve: require('@snek-at/jaen-pages'),
Expand Down
1 change: 1 addition & 0 deletions packages/jaen-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"gatsby-plugin-image": "^1.12.0",
"gatsby-plugin-sharp": "^3.12.0",
"gatsby-transformer-sharp": "^3.12.0",
"graphql-tag": "^2.12.5",
"immutability-helper": "^3.1.1",
"ipfs-http-client": "^52.0.2",
"juice": "^8.0.0",
Expand Down
59 changes: 53 additions & 6 deletions packages/jaen-pages/src/containers/ui/hotbar/PublishButton.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,68 @@
import {useToast} from '@chakra-ui/react'
import {BifrostBridge} from '@snek-at/bridge'
import {useJaenCoreContext} from '@snek-at/jaen'
import {PublishButton} from '@snek-at/jaen-shared-ui/dist/components/molecules/buttons'
import {useAppDispatch} from '@snek-at/jaen/src/store'
import gql from 'graphql-tag'
import React from 'react'

import {upload} from '../../../ipfs'
import {withRedux} from '../../../store/withRedux'

const Bridge = new BifrostBridge({
httpUrl: 'https://origin.snek.at/graphql'
})

const Button: React.FC = () => {
const core = useJaenCoreContext()
const toast = useToast()

const isDisabled = core.getAuthState().isGuest

const publish = async () => {
const res = (await core.onPublish()) as {'jaen-pages': object}

const url = await upload(JSON.stringify(res['jaen-pages']))

const publishRes = await Bridge.session.mutate<{
jaenPublishFormPage: {result: string}
}>(
gql`
mutation JaenPublishMutation($values: GenericScalar!, $url: String!) {
jaenPublishFormPage(values: $values, url: $url) {
result
errors {
name
errors
}
}
}
`,
{
url: '/jaen-publish',
values: {git_remote: core.remote, jaendata_url: url}
}
)

if (publishRes.data?.jaenPublishFormPage?.result === 'OK') {
toast({
title: 'Sucessfully published.',
status: 'success',
duration: 9000,
isClosable: true
})
} else {
toast({
title: 'Publish failed.',
status: 'error',
duration: 9000,
isClosable: true
})
}
}

return (
<PublishButton
disabled={isDisabled}
onPublishClick={() => {
core.onPublish().then(res => console.log('PUBLISH', res))
}}
/>
<PublishButton disabled={isDisabled} onPublishClick={() => publish()} />
)
}

Expand Down
6 changes: 5 additions & 1 deletion packages/jaen/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ export const wrapRootElement = ({element}) => {
const config = require(___JAEN_CONFIG___)
const plugins = config.plugins || []

console.log("config", config)

const resolvedPlugins = Object.keys(plugins).map(key => {
const plugin = plugins[key]

return plugin.resolve.default
})

return (
<JaenCoreProvider plugins={resolvedPlugins}>{element}</JaenCoreProvider>
<JaenCoreProvider plugins={resolvedPlugins} remote={config.remote}>
{element}
</JaenCoreProvider>
)
}
8 changes: 7 additions & 1 deletion packages/jaen/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
import {JaenCoreProvider} from './src'

export const wrapRootElement = ({element}) => {
return <JaenCoreProvider plugins={[]}>{element}</JaenCoreProvider>
// @ts-ignore
const config = require(___JAEN_CONFIG___)
return (
<JaenCoreProvider plugins={[]} remote={config.remote}>
{element}
</JaenCoreProvider>
)
}
6 changes: 4 additions & 2 deletions packages/jaen/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MainUI from './containers/MainUI'
import {PluginCallbacks, getPublishValue, getUI, Plugin} from './plugin'
import {store} from './store'

export type JaenCoreContextType = {} & PluginCallbacks
export type JaenCoreContextType = {remote: string} & PluginCallbacks
export const JaenCoreContext = React.createContext<
JaenCoreContextType | undefined
>(undefined)
Expand All @@ -22,11 +22,13 @@ export const useJaenCoreContext = (): JaenCoreContextType => {
}

type JaenCoreProviderProps = {
remote: string
plugins: Plugin[]
}

export const JaenCoreProvider: React.FC<JaenCoreProviderProps> = ({
children,
remote,
plugins
}) => {
const ui = getUI(plugins)
Expand All @@ -35,7 +37,7 @@ export const JaenCoreProvider: React.FC<JaenCoreProviderProps> = ({

return (
<>
<JaenCoreContext.Provider value={{onPublish, getAuthState}}>
<JaenCoreContext.Provider value={{onPublish, getAuthState, remote}}>
<MainUI ui={ui} />
{children}
</JaenCoreContext.Provider>
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13717,7 +13717,7 @@ graphql-subscriptions@^1.1.0:
dependencies:
iterall "^1.3.0"

graphql-tag@^2.10.3, graphql-tag@^2.12.3:
graphql-tag@^2.10.3, graphql-tag@^2.12.3, graphql-tag@^2.12.5:
version "2.12.5"
resolved "https://registry.yarnpkg.com/graphql-tag/-/graphql-tag-2.12.5.tgz#5cff974a67b417747d05c8d9f5f3cb4495d0db8f"
integrity sha512-5xNhP4063d16Pz3HBtKprutsPrmHZi5IdUGOWRxA2B6VF7BIRGOHZ5WQvDmJXZuPcBg7rYwaFxvQYjqkSdR3TQ==
Expand Down

0 comments on commit 75d5cfc

Please sign in to comment.