import posts via Json

This commit is contained in:
Moriz Wahl 2020-10-14 00:08:39 +02:00
parent 21a9094a25
commit 7d6800c113
5 changed files with 133 additions and 15 deletions

View File

@ -0,0 +1,5 @@
<!-- Generated by IcoMoon.io -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
<title>upload</title>
<path d="M16 3.594l0.719 0.688 7 7-1.438 1.438-5.281-5.281v16.563h-2v-16.563l-5.281 5.281-1.438-1.438 7-7zM7 26h18v2h-18v-2z"></path>
</svg>

After

Width:  |  Height:  |  Size: 294 B

View File

@ -650,6 +650,12 @@
"download": {
"name": "Daten herunterladen"
},
"upload": {
"isEmpty": "Input ist leer",
"name": "Beiträge importieren",
"placeholder": "Hier den exportierten Json einfügen",
"submit": "Importieren"
},
"email": {
"change-successful": "Deine E-Mail-Adresse wurde erfolgreich geändert.",
"labelEmail": "E-Mail-Adresse ändern",

View File

@ -650,6 +650,12 @@
"download": {
"name": "Download Data"
},
"upload": {
"isEmpty": "Input is empty",
"name": "Import posts",
"placeholder": "Paste the exported Json here",
"submit": "Import"
},
"email": {
"change-successful": "Your e-mail address has been changed successfully.",
"labelEmail": "Change your e-mail address",

View File

@ -51,32 +51,31 @@ export default {
name: this.$t('settings.embeds.name'),
path: `/settings/embeds`,
},
{
name: this.$t('settings.upload.name'),
path: `/settings/data-upload`,
},
{
name: this.$t('settings.deleteUserAccount.name'),
path: `/settings/delete-account`,
},
// TODO implement
/* {
name: this.$t('settings.invites.name'),
path: `/settings/invites`
}, */
// TODO implement
/* {
name: this.$t('settings.download.name'),
path: `/settings/data-download`
}, */
name: this.$t('settings.invites.name'),
path: `/settings/invites`
}, */
// TODO implement
// TODO implement
/* {
name: this.$t('settings.organizations.name'),
path: `/settings/my-organizations`
}, */
name: this.$t('settings.organizations.name'),
path: `/settings/my-organizations`
}, */
// TODO implement
/* {
name: this.$t('settings.languages.name'),
path: `/settings/languages`
},
} */
name: this.$t('settings.languages.name'),
path: `/settings/languages`
},
} */
]
},
},

View File

@ -0,0 +1,102 @@
<template>
<base-card>
<h2 class="title">{{ $t('settings.upload.name') }}</h2>
<ds-input
id="import"
v-model="dataImport"
type="textarea"
rows="3"
:placeholder="$t('settings.upload.placeholder')"
/>
<base-button secondary filled icon="upload" @click="importData" :disabled="processing">
{{ $t('settings.upload.submit') }}
</base-button>
<ds-space v-if="messagesPresent" margin="large" />
<base-card v-if="messagesPresent">
<ds-text v-for="message in messages" size="small" :key="message.key">
{{ message.text }}
</ds-text>
</base-card>
</base-card>
</template>
<script>
import PostMutations from '~/graphql/PostMutations.js'
import { isEmpty } from 'lodash'
export default {
data() {
return {
dataImport: '',
messages: [],
processing: false,
messageCounter: 0,
}
},
computed: {
messagesPresent() {
return !isEmpty(this.messages)
},
},
methods: {
importData(data) {
this.messages = []
this.processing = true
let input, userId, posts
if (this.dataImport === '') {
this.$toast.error(this.$t('settings.upload.isEmpty'))
this.processing = false
return
}
try {
input = JSON.parse(this.dataImport)
} catch (err) {
this.$toast.error(err.message)
this.processing = false
return
}
this.messages.push({ text: 'input parsed successfully', key: this.messageCounter++ })
try {
userId = input.user.id
} catch (err) {
this.$toast.error(err.message)
this.processing = false
return
}
this.messages.push({ text: 'user ID is ' + userId, key: this.messageCounter++ })
try {
posts = input.posts.filter((post) => post.author.id === userId)
} catch (err) {
this.$toast.error(err.message)
this.processing = false
return
}
this.messages.push({
text: String(posts.length) + ' posts found to import',
key: this.messageCounter++,
})
posts.forEach(async (post) => {
const { title, content, categoryIds, language } = post
this.$apollo
.mutate({
mutation: PostMutations().CreatePost,
variables: {
title,
content,
categoryIds,
id: null,
language,
},
})
.then(({ data }) => {
this.messages.push({ text: 'IMPORTING: ' + title, key: this.messageCounter++ })
})
.catch((err) => {
this.$toast.error(err.message)
})
})
this.processing = false
},
},
}
</script>