Skip to content

Commit

Permalink
fix draft daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Oct 14, 2024
1 parent 11414fd commit 8910e91
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 69 deletions.
57 changes: 55 additions & 2 deletions otoroshi/javascript/src/components/Drafts/DraftEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import {
QueryClientProvider,
} from 'react-query'
import { nextClient } from '../../services/BackOfficeServices'
import { draftSignal, draftVersionSignal, entityContentSignal, resetDraftSignal } from './DraftEditorSignal'
import { draftSignal, draftVersionSignal, entityContentSignal, resetDraftSignal, updateEntityURLSignal } from './DraftEditorSignal'
import { useSignalValue } from 'signals-react-safe'
import { PillButton } from '../PillButton'
import { withRouter } from 'react-router-dom'
import JsonViewCompare from './Compare'
import { dynamicTitleContent } from '../DynamicTitleSignal'
import { Button } from '../Button'

const queryClient = new QueryClient()

Expand Down Expand Up @@ -128,6 +131,9 @@ export const DraftStateDaemon = withRouter(class _ extends React.Component {
componentDidMount() {
resetDraftSignal(this.props)

if (this.props.updateEntityURL)
this.props.updateEntityURL()

this.unsubscribe = draftVersionSignal.subscribe(() => {
const { value, setValue } = this.props

Expand Down Expand Up @@ -163,6 +169,7 @@ export const DraftStateDaemon = withRouter(class _ extends React.Component {
}
}
if (prevProps.history.location.pathname !== this.props.history.location.pathname) {
console.log('[DraftStateDaemon] : componentDidUpdate')
resetDraftSignal(this.props)
}
}
Expand All @@ -175,4 +182,50 @@ export const DraftStateDaemon = withRouter(class _ extends React.Component {
render() {
return null
}
})
})



function PublisDraftModalContent() {
const draftContext = useSignalValue(draftSignal)
const entityContent = useSignalValue(entityContentSignal)

return <div className='mt-3 d-flex flex-column' style={{ flex: 1 }}>
<JsonViewCompare oldData={entityContent} newData={draftContext.draft} />
</div>
}

export function PublisDraftButton(props) {
const publish = useSignalValue(draftVersionSignal)

if (publish.version === 'published')
return null

return <Button text="Publish draft" className={`btn-sm ${props.className ? props.className : 'ms-auto'}`} type="primaryColor" style={{
borderColor: 'var(--color-primary)'
}} onClick={() => {
window.wizard(
'Publish this draft',
() => <PublisDraftModalContent />,
{
style: { width: '100%' },
noCancel: false,
okClassName: "ms-2",
okLabel: 'I want to publish this route'
}
)
.then((ok) => {
if (ok) {
if (updateEntityURLSignal && typeof updateEntityURLSignal.value === 'function') {
try {
updateEntityURLSignal.value()
.then(() => window.location.reload())
} catch (err) {
console.log(err)
alert("Something bad happened.")
}
}
}
});
}} />
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ export const draftSignal = signal(DRAFT_SIGNAL_TEMPLATE)

export const entityContentSignal = signal(undefined)

export const updateEntityURLSignal = signal(undefined)

export const resetDraftSignal = (props = {}) => {

console.log('reset draft signal')

draftSignal.value = {
...DRAFT_SIGNAL_TEMPLATE,
processCallback: props.processCallback
}
draftVersionSignal.value = DRAFT_VERSION_SIGNAL_TEMPLATE
entityContentSignal.value = undefined
updateEntityURLSignal.value = undefined
}

const saveDraft = debounce(() => {
Expand Down
14 changes: 14 additions & 0 deletions otoroshi/javascript/src/components/DynamicTitleSignal.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import React from 'react';
import Thumbtack from './Thumbtack';
import { signal, useSignalValue } from 'signals-react-safe';
import { draftVersionSignal } from './Drafts/DraftEditorSignal';
import { PublisDraftButton } from './Drafts/DraftEditor';

export const dynamicTitleContent = signal();

const EXCLUDED_DRAFT_PAGES = [
'/routes'
]

export function DynamicTitleSignal(props) {

const content = useSignalValue(dynamicTitleContent)
const draftVersion = useSignalValue(draftVersionSignal);

if (!content) {
return null;
Expand All @@ -16,6 +23,10 @@ export function DynamicTitleSignal(props) {
return (
<div style={{ position: 'relative' }}>
{content}

{draftVersion.version === 'draft' &&
!EXCLUDED_DRAFT_PAGES.find(path => window.location.pathname.includes(path)) &&
<PublisDraftButton />}
</div>
);
}
Expand All @@ -26,6 +37,9 @@ export function DynamicTitleSignal(props) {
<h3 className="page-header_title">
{content}
<Thumbtack {...props} getTitle={() => content} />
{draftVersion.version === 'draft' &&
!EXCLUDED_DRAFT_PAGES.find(path => window.location.pathname.includes(path)) &&
<PublisDraftButton />}
</h3>
</div>
</div>
Expand Down
10 changes: 7 additions & 3 deletions otoroshi/javascript/src/components/inputs/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import _ from 'lodash';
import { Button } from '../Button';
import { firstLetterUppercase } from '../../util'
import { DraftEditorContainer, DraftStateDaemon } from '../Drafts/DraftEditor';
import { updateEntityURLSignal } from '../Drafts/DraftEditorSignal';

function urlTo(url) {
window.history.replaceState({}, '', url);
Expand Down Expand Up @@ -233,6 +234,7 @@ export class Table extends Component {
if (this.props.injectTable) {
this.props.injectTable(this);
}

this.readRoute();
}

Expand Down Expand Up @@ -734,8 +736,6 @@ export class Table extends Component {
});
}

console.log(this.state.currentItem)

return (
<div>
{this.state.currentItem && <>
Expand All @@ -746,7 +746,11 @@ export class Table extends Component {

<DraftStateDaemon
value={this.state.currentItem}
setValue={currentItem => this.setState({ currentItem })} />
setValue={currentItem => this.setState({ currentItem })}
updateEntityURL={() => {
updateEntityURLSignal.value = this.updateItemAndStay
}}
/>
</>}

{!this.state.showEditForm && !this.state.showAddForm && (
Expand Down
110 changes: 48 additions & 62 deletions otoroshi/javascript/src/pages/RouteDesigner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import { FeedbackButton } from './FeedbackButton';
import Loader from '../../components/Loader';
import _ from 'lodash';
import { Button } from '../../components/Button';
import { DraftEditorContainer } from '../../components/Drafts/DraftEditor';
import { DraftEditorContainer, PublisDraftButton } from '../../components/Drafts/DraftEditor';
import { dynamicTitleContent } from '../../components/DynamicTitleSignal';
import { draftSignal, draftVersionSignal, entityContentSignal } from '../../components/Drafts/DraftEditorSignal';
import { useSignalValue } from 'signals-react-safe';
// import { draftSignal, draftVersionSignal, entityContentSignal } from '../../components/Drafts/DraftEditorSignal';
// import { useSignalValue } from 'signals-react-safe';
import PageTitle from '../../components/PageTitle';
import { Dropdown } from '../../components/Dropdown';
import { YAMLExportButton } from '../../components/exporters/YAMLButton';
Expand Down Expand Up @@ -207,64 +207,50 @@ function MoreActionsButton({ value, menu, history }) {
);
}

function PublisDraftModalContent() {
const draftContext = useSignalValue(draftSignal)
const entityContent = useSignalValue(entityContentSignal)

return <div className='mt-3 d-flex flex-column' style={{ flex: 1 }}>
<JsonViewCompare oldData={entityContent} newData={draftContext.draft} />
</div>
}

function PublisDraftButton() {
const publish = useSignalValue(draftVersionSignal)
const context = useSignalValue(draftSignal)

if (publish.version === 'published')
return null

return <Button text="Publish draft" className='btn-sm ms-2 mb-1' type="primaryColor" style={{
borderColor: 'var(--color-primary)'
}} onClick={() => {
window.wizard(
'Publish this draft',
() => <PublisDraftModalContent />,
{
style: { width: '100%' },
noCancel: false,
okClassName: "ms-2",
okLabel: 'I want to publish this route'
}
)
.then((ok) => {
if (ok) {
const what = window.location.pathname.split('/')[3];
const kind = what === 'routes' ? nextClient.ENTITIES.ROUTES : nextClient.ENTITIES.SERVICES;

nextClient
.forEntityNext(kind)
.update(context.draft)
.then(() => {
window.location.reload()
});
}
});
// window.newConfirm(<PublisDraftModalContent />, {
// title: `Publish this draft`,
// yesText: 'I want to publish this route',
// className: 'modal-dialog--lg'
// }).then((ok) => {
// if (ok) {
// nextClient
// .forEntityNext(kind)
// .update(context.draft)
// .then(() => {
// window.location.reload()
// });
// }
// });
}} />
}
// function PublisDraftModalContent() {
// const draftContext = useSignalValue(draftSignal)
// const entityContent = useSignalValue(entityContentSignal)

// return <div className='mt-3 d-flex flex-column' style={{ flex: 1 }}>
// <JsonViewCompare oldData={entityContent} newData={draftContext.draft} />
// </div>
// }

// function PublisDraftButton() {
// const publish = useSignalValue(draftVersionSignal)
// const context = useSignalValue(draftSignal)

// if (publish.version === 'published')
// return null

// return <Button text="Publish draft" className='btn-sm ms-2 mb-1' type="primaryColor" style={{
// borderColor: 'var(--color-primary)'
// }} onClick={() => {
// window.wizard(
// 'Publish this draft',
// () => <PublisDraftModalContent />,
// {
// style: { width: '100%' },
// noCancel: false,
// okClassName: "ms-2",
// okLabel: 'I want to publish this route'
// }
// )
// .then((ok) => {
// if (ok) {
// const what = window.location.pathname.split('/')[3];
// const kind = what === 'routes' ? nextClient.ENTITIES.ROUTES : nextClient.ENTITIES.SERVICES;

// nextClient
// .forEntityNext(kind)
// .update(context.draft)
// .then(() => {
// window.location.reload()
// });
// }
// });
// }} />
// }

function ManagerTitle({
query,
Expand Down Expand Up @@ -353,7 +339,7 @@ function ManagerTitle({
})}
</Dropdown>
{saveButton}
<PublisDraftButton />
<PublisDraftButton className="ms-2 mb-1" />
</PageTitle>
);
}
Expand Down
2 changes: 0 additions & 2 deletions otoroshi/javascript/src/pages/ServiceApiKeysPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,6 @@ export class ApiKeysPage extends Component {
render() {
const { fields } = this.state;

console.log("ApiKeysPage")

const lowercaseFields = Object.entries(fields)
.map(([key, value]) => [key.toLowerCase(), value])

Expand Down
3 changes: 3 additions & 0 deletions otoroshi/javascript/src/style/layout/_pagecontent.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
padding-bottom: 9px;
border-bottom: 1px solid var(--color-primary);
color: var(--color-primary);

display: flex;
align-items: center;
}

.sub-container {
Expand Down

0 comments on commit 8910e91

Please sign in to comment.