feat: Added content page updating

pull/7619/head
Ruslan Semak 7 months ago
parent 872e8794dd
commit 4a84db5b08

@ -188,6 +188,16 @@
span {{$t('common:actions.exit')}} span {{$t('common:actions.exit')}}
v-divider(vertical) v-divider(vertical)
//- Update Content
template(v-if='isAuthenticated && isAdmin')
v-tooltip(bottom, v-if='mode !== `admin`')
template(v-slot:activator='{ on }')
v-btn(icon, tile, height='64', v-on='on', @click="updateContent")
v-icon(color='grey') mdi-file-document-refresh
span {{'Обновить Content'}}
v-divider(vertical)
//- ACCOUNT //- ACCOUNT
v-menu(v-if='isAuthenticated', offset-y, bottom, min-width='300', transition='slide-y-transition', left) v-menu(v-if='isAuthenticated', offset-y, bottom, min-width='300', transition='slide-y-transition', left)
@ -254,6 +264,7 @@ import { get, sync } from 'vuex-pathify'
import _ from 'lodash' import _ from 'lodash'
import movePageMutation from 'gql/common/common-pages-mutation-move.gql' import movePageMutation from 'gql/common/common-pages-mutation-move.gql'
import updateContentPageMutation from 'gql/common/common-pages-mutation-update-content.gql'
/* global siteConfig, siteLangs */ /* global siteConfig, siteLangs */
@ -372,6 +383,23 @@ export default {
this.isDevMode = siteConfig.devMode === true this.isDevMode = siteConfig.devMode === true
}, },
methods: { methods: {
async updateContent() {
try {
await this.$apollo.mutate({ mutation: updateContentPageMutation })
this.$store.commit('showNotification', {
message: 'Content page updated successfully',
style: 'success',
icon: 'check'
})
} catch (err) {
this.$store.commit('showNotification', {
message: 'Failed to update content page',
style: 'error',
icon: 'error'
})
}
},
searchFocus () { searchFocus () {
this.searchIsFocused = true this.searchIsFocused = true
}, },

@ -0,0 +1,9 @@
mutation {
pages {
updateContent {
responseResult {
succeeded
}
}
}
}

@ -1,5 +1,6 @@
const _ = require('lodash') const _ = require('lodash')
const graphHelper = require('../../helpers/graph') const graphHelper = require('../../helpers/graph')
const moment = require('moment')
/* global WIKI */ /* global WIKI */
@ -451,13 +452,132 @@ module.exports = {
*/ */
async updateIcon(obj, args, context) { async updateIcon(obj, args, context) {
try { try {
const page = await WIKI.models.pages.updateIcon({ const { id, icon } = args
...args,
user: context.req.user await WIKI.models.pages.query()
}) .where('id', id)
.patch({ icon })
await WIKI.models.pages.rebuildTree()
return { return {
responseResult: graphHelper.generateSuccess('Page icon has been updated.'), responseResult: graphHelper.generateSuccess('Page icon has been updated.')
page }
} catch (err) {
return graphHelper.generateError(err)
}
},
/**
* UPDATE CONTENT PAGE
*/
async updateContent(obj, args, context) {
try {
const openTag = '<ul class="todo-list">\n'
const closeTag = '</ul>\n'
const nbsp = '<p>&nbsp;</p>\n'
const header = `<blockquote><p><strong>⛔️ Технический раздел!</strong> Обновление от <strong>${moment().format('DD.MM.YYYY HH:mm')}</strong></p></blockquote>\n`
const contentPageId = 232
const planPageId = 2
const [page] = await WIKI.models.pages.query()
.select()
.where('id', contentPageId)
if (!page) {
throw Error('Content page not found')
}
const [planPage] = await WIKI.models.pages.query()
.select()
.where('id', planPageId)
let pages = await WIKI.models.pages.query()
.select('id', 'path', 'orderPriority', 'title')
.orderBy(['localeCode', 'path'])
/**
* Sorting order:
* Every not custom path without "/"
* Every not custom path with "/"
* Custom order for paths with "/"
*/
const customSortingOrder = [
'Intro',
'WebStorm',
'Git',
'Software',
'JavaScript',
'TypeScript',
'Backend-1',
'Backend-2'
]
pages = pages
.filter(({ path }) => !path.startsWith('Users'))
.sort((a, b) => {
const aHasSlash = a.path.includes('/')
const bHasSlash = b.path.includes('/')
if (aHasSlash !== bHasSlash) {
return aHasSlash ? 1 : -1
}
const [aPathPart] = a.path.split('/')
const [bPathPart] = b.path.split('/')
const aPriority = customSortingOrder.indexOf(aPathPart)
const bPriority = customSortingOrder.indexOf(bPathPart)
if (aPriority !== bPriority) {
return aPriority > bPriority ? 1 : -1
}
const pathComparison = aPathPart.localeCompare(bPathPart)
if (pathComparison !== 0) {
return pathComparison
}
return a.orderPriority - b.orderPriority
})
let { content } = pages.reduce(
(acc, page) => {
const mentionedInPlan = planPage.content.includes(`data-page-id="${page.id}"`)
const li = `<li><label class="todo-list__label"><input ${mentionedInPlan ? 'checked="checked"' : ''} disabled="disabled" type="checkbox"><span class="todo-list__label__description">&nbsp;<a href="/${page.path}" data-page-id="${page.id}" data-mention="@${page.title}" class="mention is-internal-link is-valid-page">${page.title}</a>&nbsp;</span></label></li>`
const [section] = page.path.split('/')
if (section !== acc.previousSection) {
if (acc.previousSection !== '') {
acc.content += closeTag
}
acc.previousSection = section
acc.content += nbsp
acc.content += `<h1>${section}</h1>\n`
acc.content += openTag
}
acc.content += li
return acc
},
{ content: header, previousSection: '' }
)
content += closeTag
await WIKI.models.pages.query()
.where('id', contentPageId)
.patch({ content })
await WIKI.models.pages.renderPage(page)
return {
responseResult: graphHelper.generateSuccess('Page has been updated.')
} }
} catch (err) { } catch (err) {
return graphHelper.generateError(err) return graphHelper.generateError(err)

@ -126,6 +126,8 @@ type PageMutation {
icon: String! icon: String!
): PageResponse @auth(requires: ["write:pages", "manage:pages", "manage:system"]) ): PageResponse @auth(requires: ["write:pages", "manage:pages", "manage:system"])
updateContent: DefaultResponse @auth(requires: ["write:pages", "manage:pages", "manage:system"])
convert( convert(
id: Int! id: Int!
editor: String! editor: String!

@ -559,21 +559,6 @@ module.exports = class Page extends Model {
await WIKI.models.pages.rebuildTree() await WIKI.models.pages.rebuildTree()
} }
/**
* Update an Icon of Existing Page
* @param {Object} opts Page Properties
* @returns {Promise} Promise of the Page Model Instance
*/
static async updateIcon(opts) {
const { id, icon } = opts
await WIKI.models.pages.query()
.where('id', id)
.patch({ icon })
await WIKI.models.pages.rebuildTree()
}
/** /**
* Convert an Existing Page * Convert an Existing Page
* *

Loading…
Cancel
Save