Main functionality for translation entries

This commit is contained in:
2021-12-19 23:54:40 +02:00
parent 3515a687a2
commit 32a70881ba
7 changed files with 171 additions and 1 deletions

View File

@@ -0,0 +1,15 @@
const express = require('express')
module.exports = function (options) {
const db = options.db;
var router = express.Router();
router.route('/dialects')
.get(function(request, response, next) {
db.Dialect.findAll({
}).then(dialects => {
response.json(dialects)
})
})
return router;
};

View File

@@ -21,11 +21,14 @@ module.exports = function (options) {
var libraryRouter = require('./library')(options);
var pledgesRouter = require('./pledges/')(options);
var campaignsRouter = require('./campaigns/')(options);
var apiRouter = require('./api/')(options);
router.use('/library', libraryRouter);
router.use('/pledges', pledgesRouter);
router.use('/campaigns', campaignsRouter);
router.use('/account', accountRouter);
router.use('/api', apiRouter);
return router;
};

View File

@@ -184,5 +184,106 @@ module.exports = function (options) {
next(error)
})
})
router.route('/:id/translations/:translationEntryid')
.get(function(request, response, next) {
db.TranslationEntry.findByPk(request.params.translationEntryid, {
include: [
{
association: db.TranslationEntry.Entry
}
]
}).then(translationEntry => {
if (translationEntry) {
response.display("translation-entry", {
user: request.user,
pageTitle: "Translation Entry - Mantra",
translationEntry: translationEntry,
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/translations/add')
.get(function(request, response, next) {
db.Entry.findByPk(request.params.id, {
include: [
{
association: db.Entry.EntryApproval
},
{
association: db.Entry.TranslationEntries
}
]
}).then(entry => {
if (entry) {
response.display("translation-form", {
user: request.user,
pageTitle: "Library - Mantra",
entry: entry
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
.post(function(request, response, next) {
db.Entry.findByPk(request.params.id, {
include: [
{
association: db.Entry.EntryApproval
},
{
association: db.Entry.TranslationEntries
},
{
association: db.Entry.Chapters,
include: [
{
association: db.Chapter.Chunks
}
]
}
]
}).then(async (entry) => {
if (entry) {
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 translationEntry = await db.TranslationEntry.create({
name: dialect.name,
entryId: entry.id,
dialectId: dialect.id
})
if (translationEntry) {
return response.redirect(`/library/${entry.id}/translations/${translationEntry.id}`)
}
}
}
}
next()
}).catch(error => {
next(error)
})
})
return router;
};

View File

@@ -5,6 +5,8 @@ block content
.center
h1= entry.name
.divider
.row
a.btn.black(href=`/library/${entry.id}/chapters/add`) add chapter
if entry.chapters.length == 0
@@ -15,4 +17,8 @@ block content
.col.s12
a(href=`/library/${entry.id}/chapters/${chapter.id}`)
.card-panel
p.flow-text= chapter.name
p.flow-text= chapter.name
.divider
.row
a.btn.black(href=`/library/${entry.id}/translations/add`) add translation

View File

@@ -0,0 +1,16 @@
document.addEventListener('DOMContentLoaded', function() {
fetch("http://localhost:7878/api/dialects")
.then(response => response.json())
.then(dialects => {
const dialectsData = dialects.reduce((accumulator, dialect) => {
accumulator[`${dialect.countryId}-${dialect.languageId}: ${dialect.name}`] = null
return accumulator
}, {})
var elems = document.querySelectorAll('#dialect-autocomplete');
var instances = M.Autocomplete.init(elems, {
data: dialectsData
});
})
});

View File

@@ -0,0 +1,7 @@
extend templates/layout.pug
block content
.container
.center
h1= translationEntry.name
h2= translationEntry.entry.name

View File

@@ -0,0 +1,22 @@
extend templates/layout.pug
block content
.container
.center
h1 Translation of #{entry.name}
.row
.col.s12
form.row(action=`/library/${entry.id}/translations/add`, method="post")
.col.s12.input-field
i.material-icons.prefix record_voice_over
input#dialect-autocomplete.autocomplete(type="text", name="dialect", required)
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