From 32a70881badf93add5f08a75366ebcfc9a565a83 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sun, 19 Dec 2021 23:54:40 +0200 Subject: [PATCH] Main functionality for translation entries --- server/router/api/index.js | 15 +++ server/router/index.js | 3 + server/router/library/index.js | 101 +++++++++++++++++++ server/views/entry.pug | 8 +- server/views/js/init-dialect-autocomplete.js | 16 +++ server/views/translation-entry.pug | 7 ++ server/views/translation-form.pug | 22 ++++ 7 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 server/router/api/index.js create mode 100644 server/views/js/init-dialect-autocomplete.js create mode 100644 server/views/translation-entry.pug create mode 100644 server/views/translation-form.pug diff --git a/server/router/api/index.js b/server/router/api/index.js new file mode 100644 index 0000000..5d18af8 --- /dev/null +++ b/server/router/api/index.js @@ -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; +}; \ No newline at end of file diff --git a/server/router/index.js b/server/router/index.js index 51238ad..ae389ae 100644 --- a/server/router/index.js +++ b/server/router/index.js @@ -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; }; \ No newline at end of file diff --git a/server/router/library/index.js b/server/router/library/index.js index f4a542a..6ac1c60 100644 --- a/server/router/library/index.js +++ b/server/router/library/index.js @@ -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; }; \ No newline at end of file diff --git a/server/views/entry.pug b/server/views/entry.pug index 216b079..ed36f4d 100644 --- a/server/views/entry.pug +++ b/server/views/entry.pug @@ -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 \ No newline at end of file + p.flow-text= chapter.name + + .divider + .row + a.btn.black(href=`/library/${entry.id}/translations/add`) add translation \ No newline at end of file diff --git a/server/views/js/init-dialect-autocomplete.js b/server/views/js/init-dialect-autocomplete.js new file mode 100644 index 0000000..c5e5564 --- /dev/null +++ b/server/views/js/init-dialect-autocomplete.js @@ -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 + }); + }) + +}); \ No newline at end of file diff --git a/server/views/translation-entry.pug b/server/views/translation-entry.pug new file mode 100644 index 0000000..8627320 --- /dev/null +++ b/server/views/translation-entry.pug @@ -0,0 +1,7 @@ +extend templates/layout.pug + +block content + .container + .center + h1= translationEntry.name + h2= translationEntry.entry.name \ No newline at end of file diff --git a/server/views/translation-form.pug b/server/views/translation-form.pug new file mode 100644 index 0000000..da2d331 --- /dev/null +++ b/server/views/translation-form.pug @@ -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 \ No newline at end of file