2023-05-26 15:54:43 +02:00
|
|
|
// Dependencies
|
2023-08-28 16:23:50 +02:00
|
|
|
import { capitalize, shortDate, notEmpty, horFlexClassesNoSm } from 'shared/utils.mjs'
|
|
|
|
import yaml from 'js-yaml'
|
2023-09-04 08:40:05 +02:00
|
|
|
// Context
|
|
|
|
import { LoadingStatusContext } from 'shared/context/loading-status-context.mjs'
|
2023-05-26 15:54:43 +02:00
|
|
|
// Hooks
|
2023-09-04 12:18:52 +02:00
|
|
|
import { useState, useContext } from 'react'
|
2023-05-26 15:54:43 +02:00
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useBackend } from 'shared/hooks/use-backend.mjs'
|
|
|
|
// Components
|
2023-08-28 16:23:50 +02:00
|
|
|
import { AuthWrapper } from 'shared/components/wrappers/auth/index.mjs'
|
|
|
|
import { StringInput, MarkdownInput } from 'shared/components/inputs.mjs'
|
|
|
|
import { UploadIcon, EditIcon, PlusIcon, BookmarkIcon } from 'shared/components/icons.mjs'
|
|
|
|
import { Popout } from 'shared/components/popout/index.mjs'
|
|
|
|
import { PageLink } from 'shared/components/link.mjs'
|
2023-08-28 16:31:43 +02:00
|
|
|
import { DynamicOrgDocs } from 'shared/components/dynamic-docs/org.mjs'
|
2023-05-26 15:54:43 +02:00
|
|
|
|
2023-08-28 16:23:50 +02:00
|
|
|
export const ns = ['workbench', 'status']
|
2023-05-26 15:54:43 +02:00
|
|
|
|
2023-08-28 17:10:03 +02:00
|
|
|
export const SaveView = ({ design, settings }) => {
|
2023-08-28 16:23:50 +02:00
|
|
|
// Hooks
|
|
|
|
const { t } = useTranslation(ns)
|
|
|
|
const backend = useBackend()
|
|
|
|
const router = useRouter()
|
2023-09-04 08:40:05 +02:00
|
|
|
const { setLoadingStatus } = useContext(LoadingStatusContext)
|
2023-05-26 15:54:43 +02:00
|
|
|
|
2023-05-31 15:32:54 +02:00
|
|
|
// State
|
2023-08-28 16:23:50 +02:00
|
|
|
const [name, setName] = useState(`${capitalize(design)} / ${shortDate(router.locale)}`)
|
|
|
|
const [withNotes, setWithNotes] = useState(false)
|
|
|
|
const [notes, setNotes] = useState('')
|
|
|
|
const [savedId, setSavedId] = useState()
|
|
|
|
const [bookmarkedId, setBookmarkedId] = useState()
|
2023-08-28 17:10:03 +02:00
|
|
|
|
|
|
|
// When we have more than 'new' this will come in handy
|
|
|
|
//const action = router.asPath.split('/')[1]
|
2023-08-28 16:23:50 +02:00
|
|
|
|
|
|
|
const addSettingsToNotes = () => {
|
|
|
|
setNotes(notes + '\n```yaml\n' + yaml.dump(settings) + '````')
|
2023-05-31 15:32:54 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 16:23:50 +02:00
|
|
|
const savePattern = async () => {
|
|
|
|
setLoadingStatus([true, 'savingPattern'])
|
|
|
|
const patternData = { design, name, public: false, settings, data: {} }
|
|
|
|
if (withNotes) patternData.notes = notes
|
|
|
|
const result = await backend.createPattern(patternData)
|
2023-05-31 15:32:54 +02:00
|
|
|
if (result.success) {
|
2023-08-28 16:23:50 +02:00
|
|
|
const id = result.data.pattern.id
|
|
|
|
setLoadingStatus([
|
|
|
|
true,
|
|
|
|
<>
|
|
|
|
{t('status:patternSaved')} <small>[#{id}]</small>
|
|
|
|
</>,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
])
|
|
|
|
setSavedId(id)
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-05-31 15:32:54 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 16:23:50 +02:00
|
|
|
const bookmarkPattern = async () => {
|
|
|
|
setLoadingStatus([true, 'creatingBookmark'])
|
|
|
|
const result = await backend.createBookmark({
|
|
|
|
type: 'pattern',
|
|
|
|
title: name,
|
|
|
|
url: window.location.pathname + window.location.search + window.location.hash,
|
|
|
|
})
|
2023-05-31 15:32:54 +02:00
|
|
|
if (result.success) {
|
2023-08-28 16:23:50 +02:00
|
|
|
const id = result.data.bookmark.id
|
|
|
|
setLoadingStatus([
|
|
|
|
true,
|
|
|
|
<>
|
|
|
|
{t('status:bookmarkCreated')} <small>[#{id}]</small>
|
|
|
|
</>,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
])
|
|
|
|
setBookmarkedId(id)
|
|
|
|
} else setLoadingStatus([true, 'backendError', true, false])
|
2023-05-31 15:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-08-28 16:23:50 +02:00
|
|
|
<AuthWrapper>
|
|
|
|
<div className="m-auto mt-8 max-w-2xl px-4">
|
|
|
|
<h2>{t('workbench:savePattern')}</h2>
|
|
|
|
{savedId && (
|
|
|
|
<Popout link>
|
|
|
|
<h5>{t('workbend:patternSaved')}</h5>
|
|
|
|
{t('workbench:see')}:{' '}
|
|
|
|
<PageLink href={`/account/patterns/${savedId}`} txt={`/account/patterns/${savedId}`} />
|
|
|
|
</Popout>
|
|
|
|
)}
|
|
|
|
{bookmarkedId && (
|
|
|
|
<Popout link>
|
|
|
|
<h5>{t('workbench:patternBookmarkCreated')}</h5>
|
|
|
|
{t('workbench:see')}:{' '}
|
|
|
|
<PageLink
|
|
|
|
href={`/account/bookmarks/${bookmarkedId}`}
|
|
|
|
txt={`/account/bookmarks/${bookmarkedId}`}
|
|
|
|
/>
|
|
|
|
</Popout>
|
|
|
|
)}
|
|
|
|
<div className="mb-4">
|
|
|
|
<StringInput
|
|
|
|
label={t('workbench:name')}
|
|
|
|
current={name}
|
|
|
|
update={setName}
|
|
|
|
valid={notEmpty}
|
2023-08-28 16:31:43 +02:00
|
|
|
docs={<DynamicOrgDocs language={router.locale} path={`site/patterns/name`} />}
|
2023-08-28 16:23:50 +02:00
|
|
|
/>
|
|
|
|
{withNotes ? (
|
2023-08-28 16:31:43 +02:00
|
|
|
<MarkdownInput
|
|
|
|
label={t('workbench:notes')}
|
|
|
|
current={notes}
|
|
|
|
update={setNotes}
|
|
|
|
docs={<DynamicOrgDocs language={router.locale} path={`site/patterns/notes`} />}
|
|
|
|
/>
|
2023-08-28 16:23:50 +02:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary w-full mt-2`}
|
|
|
|
onClick={savePattern}
|
|
|
|
>
|
|
|
|
<UploadIcon />
|
|
|
|
{t('workbench:savePattern')}
|
2023-05-31 15:32:54 +02:00
|
|
|
</button>
|
2023-08-28 16:23:50 +02:00
|
|
|
<div className="grid md:grid-cols-2 gap-2 mt-4">
|
|
|
|
{withNotes ? (
|
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary btn-outline`}
|
|
|
|
onClick={addSettingsToNotes}
|
|
|
|
>
|
|
|
|
<PlusIcon />
|
|
|
|
{t('workbench:addSettingsToNotes')}
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary btn-outline`}
|
|
|
|
onClick={() => setWithNotes(true)}
|
|
|
|
>
|
|
|
|
<EditIcon />
|
|
|
|
{t('workbench:addNotes')}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
<button
|
|
|
|
className={`${horFlexClassesNoSm} btn btn-primary btn-outline w-full`}
|
|
|
|
onClick={bookmarkPattern}
|
|
|
|
>
|
|
|
|
<BookmarkIcon />
|
|
|
|
{t('workbench:bookmarkPattern')}
|
|
|
|
</button>
|
|
|
|
</div>
|
2023-05-31 15:32:54 +02:00
|
|
|
</div>
|
2023-08-28 16:23:50 +02:00
|
|
|
</AuthWrapper>
|
2023-05-26 15:54:43 +02:00
|
|
|
)
|
|
|
|
}
|