Skip to content

Commit

Permalink
new entry
Browse files Browse the repository at this point in the history
  • Loading branch information
rucsi committed Jan 12, 2024
1 parent 3eb0bd3 commit dc29b57
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
15 changes: 11 additions & 4 deletions apps/web/src/components/editorial/editor.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react'
import { useNavigate } from "react-router-dom";
import { useAuth } from '../../lib/auth'
import { useClient, endpoints, isSchema } from '../../lib/moonbase'
import { useClient, defaultLocale, endpoints, isSchema } from '../../lib/moonbase'
import { CloseIcon, SaveIcon } from '../common'
import Error from '../error'
import Loader from '../loader'
Expand All @@ -16,22 +16,29 @@ export default ({ owner, repo, branch, collection, entry }) => {
const navigate = useNavigate()

const backUrl = `/cms/${owner}/${repo}/${branch}/${collection}`
const apiUrl = endpoints.entry(owner, repo, branch, collection, entry)
const isNew = (entry == "_new")

const cancel = () => {
navigate(backUrl)
}

const save = (schema) => {
client.put(apiUrl + (schema ? '?save_schema=true' : ''), { login: user.login, name: entry, contents: jsonContent() }).then(res => {
let name = entry
if (isNew) {
name = data.content.fields[data.schema.displayField][defaultLocale]
}
if (!name) {
return setError("Display field for entry is mandatory")
}
client.put(endpoints.entry(owner, repo, branch, collection, name) + (schema ? '?save_schema=true' : ''), { login: user.login, name: name, contents: jsonContent() }).then(res => {
if (res.error)
return setError(res.error)
navigate(backUrl)
}).catch(err => setError(error.message))
}

useEffect(() => {
client.get(apiUrl).then(data => {
client.get(endpoints.entry(owner, repo, branch, collection, entry)).then(data => {
if (data.error) {
return setError(data.error)
}
Expand Down
23 changes: 4 additions & 19 deletions apps/web/src/components/editorial/entries.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ function fmtDisplayName(name) {

export default ({ owner, repo, branch, collection }) => {
const client = useClient()
const navigate = useNavigate()
const { user } = useAuth()
const [addNew, setAddNew] = useState(false)
const [entries, setEntries] = useState(null)
const [error, setError] = useState(null)

Expand All @@ -30,19 +27,6 @@ export default ({ owner, repo, branch, collection }) => {
}).catch(err => setError(err.message))
}, [])

const newEntry = (name) => {
if (!name)
return setAddNew(false)

client.post(endpoints.collection(owner, repo, branch, collection), { login: user.login, name, contents: '{}' }).then(data => {
if (data.error) {
return setError(data.error)
}
navigate(`/cms/${owner}/${repo}/${branch}/${collection}/${name}`, { state: { isNew: true } })
}).catch(err => setError(err.message))
.finally(() => setAddNew(false))
}

const deleteEntry = (name) => (e) => {
e.preventDefault()
e.stopPropagation()
Expand Down Expand Up @@ -73,10 +57,12 @@ export default ({ owner, repo, branch, collection }) => {
{/* <!-- branches selector dropdown --> */}
</div>
<div className="flex items-center space-x-2">
<button className="border border-gray-400 rounded-md px-4 py-1 hover:bg-gray-200 flex items-center space-x-1" onClick={() => setAddNew(true)}>
{/* </div></div><button className="border border-gray-400 rounded-md px-4 py-1 hover:bg-gray-200 flex items-center space-x-1" onClick={() => setAddNew(true)}> */}
<Link to={`/cms/${owner}/${repo}/${branch}/${collection}/_new`} className="border border-gray-400 rounded-md px-4 py-1 hover:bg-gray-200 flex items-center space-x-1">
<NewDocumentIcon />
<div className="font-semibold">New entry</div>
</button>
{/* </button> */}
</Link>
</div>
</div> {/* <!-- end of branch-navigation --> */}
<div className="commits-container bg-blue-100 rounded-md rounded-b-none border-blue-200 border-b-0 flex items-center justify-between px-4 py-4 mt-5">
Expand Down Expand Up @@ -109,6 +95,5 @@ export default ({ owner, repo, branch, collection }) => {
</div>{/* <!-- end of file-explorer --> */}
</div>
</div>
{addNew && <CreateNew title="entry" onClose={newEntry} owner={owner} repo={repo} branch={branch} collection={collection} />}
</div>)
}

0 comments on commit dc29b57

Please sign in to comment.