diff --git a/server/router/fork/index.js b/server/router/fork/index.js index 8d7dd21..38b0d10 100644 --- a/server/router/fork/index.js +++ b/server/router/fork/index.js @@ -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; }; \ No newline at end of file diff --git a/server/views/entry-version.pug b/server/views/entry-version.pug index aba9841..fd4cb48 100644 --- a/server/views/entry-version.pug +++ b/server/views/entry-version.pug @@ -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 diff --git a/server/views/fork-entry-version.pug b/server/views/fork-entry-version.pug new file mode 100644 index 0000000..67dcd52 --- /dev/null +++ b/server/views/fork-entry-version.pug @@ -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 \ No newline at end of file