Adding fork functionality on entryVersions

This commit is contained in:
2021-12-26 21:00:50 +02:00
parent 8762e623d0
commit 9d8442bca9
3 changed files with 95 additions and 16 deletions

View File

@@ -32,11 +32,19 @@ module.exports = function (options) {
router.route('/e/:id')
.get(function(request, response, next) {
db.TranslationEntryVersion.findByPk(request.params.id, {
}).then(translationEntryVersion => {
if (translationEntryVersion) {
// choose a dialect
next()
db.EntryVersion.findByPk(request.params.id, {
include: [
{
association: db.EntryVersion.Entry
}
]
}).then(entryVersion => {
if (entryVersion) {
response.display("fork-entry-version", {
user: request.user,
pageTitle: "Add translation - Mantra",
entryVersion: entryVersion
})
} else {
next()
}
@@ -44,18 +52,64 @@ module.exports = function (options) {
next(error)
})
})
.post(function(request, response, next) {
db.TranslationEntryVersion.findByPk(request.params.id, {
}).then(translationEntryVersion => {
if (translationEntryVersion) {
// fork...
next()
} else {
next()
.post(async function(request, response, next) {
const dialectTokens = request.body.dialect?.split(":")
if (dialectTokens?.length == 2) {
const countryId = dialectTokens[0].split("-")[0]
const languageId = dialectTokens[0].split("-")[1]
const dialect = await db.Dialect.findOne({
where: {
countryId: countryId,
languageId: languageId
}
})
if (dialect) {
const entryVersion = await db.EntryVersion.findByPk(request.params.id, {
include: [
{
association: db.EntryVersion.Entry
},
{
association: db.EntryVersion.TranslationEntryVersions,
required: false,
where: {
dialectId: dialect.id,
}
}
]
})
if (entryVersion) {
const forkedTranslationEntryVersion = await forkEntryVersion(entryVersion, dialect, request.user.id)
if (forkedTranslationEntryVersion) {
return response.redirect(`/v/${forkedTranslationEntryVersion.entryVersionId}/translations/${forkedTranslationEntryVersion.id}`)
}
}
}
}).catch(error => {
next(error)
})
}
next()
})
const forkEntryVersion = (entryVersion, dialect, userId) => {
if (entryVersion.translationEntryVersions.length == 0) {
// create a translationEntryVersion with a new dialect...
return db.TranslationEntryVersion.create({
name: dialect.name,
entryVersionId: entryVersion.id,
dialectId: dialect.id,
userId: userId,
})
} else {
return db.TranslationEntryVersion.create({
name: entryVersion.translationEntryVersions[0].name,
entryVersionId: entryVersion.translationEntryVersions[0].entryVersionId,
userId: userId,
dialectId: entryVersion.translationEntryVersions[0].dialectId,
forkedFromId: entryVersion.translationEntryVersions[0].id
})
}
}
return router;
};

View File

@@ -6,6 +6,9 @@ block content
h1 #{entryVersion.entry.name}
h2 #{entryVersion.tag}
.row
.col.s12
a.btn.black(href=`/fork/e/${entryVersion.id}`) fork
.divider
.row
a.btn.black(href=`/v/${entryVersion.id}/chapters/add`) add chapter

View File

@@ -0,0 +1,22 @@
extend templates/layout.pug
block content
.container
.center
h1 Fork of #{entryVersion.entry.name}
.row
.col.s12
form.row(action=`/fork/e/${entryVersion.id}`, method="post")
.col.s12.input-field
i.material-icons.prefix record_voice_over
input#dialect-autocomplete.autocomplete(type="text", name="dialect", required, autocomplete="off")
label(for="dialect-autocomplete") Dialect which this translation will be made in.
.col.s12
button.btn.black(type="submit") add translation
block additionalScripts
script
include js/init-dialect-autocomplete.js