Fix post edit dropping pictures

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-12-16 09:53:43 +01:00
parent 9b27e70eb0
commit af98045d14
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
1 changed files with 17 additions and 13 deletions

View File

@ -109,10 +109,14 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { Component, Prop } from "vue-property-decorator"; import { Component, Prop, Watch } from "vue-property-decorator";
import { mixins } from "vue-class-component"; import { mixins } from "vue-class-component";
import { FETCH_GROUP } from "@/graphql/group"; import { FETCH_GROUP } from "@/graphql/group";
import { buildFileFromIMedia, readFileAsync } from "@/utils/image"; import {
buildFileFromIMedia,
buildFileVariable,
readFileAsync,
} from "@/utils/image";
import GroupMixin from "@/mixins/group"; import GroupMixin from "@/mixins/group";
import { PostVisibility } from "@/types/enums"; import { PostVisibility } from "@/types/enums";
import { TAGS } from "../../graphql/tags"; import { TAGS } from "../../graphql/tags";
@ -206,6 +210,13 @@ export default class EditPost extends mixins(GroupMixin) {
this.pictureFile = await buildFileFromIMedia(this.post.picture); this.pictureFile = await buildFileFromIMedia(this.post.picture);
} }
@Watch("post")
async updatePostPicture(oldPost: IPost, newPost: IPost): Promise<void> {
if (oldPost.picture !== newPost.picture) {
this.pictureFile = await buildFileFromIMedia(this.post.picture);
}
}
// eslint-disable-next-line consistent-return // eslint-disable-next-line consistent-return
async publish(draft: boolean): Promise<void> { async publish(draft: boolean): Promise<void> {
this.errors = {}; this.errors = {};
@ -292,19 +303,11 @@ export default class EditPost extends mixins(GroupMixin) {
async buildPicture(): Promise<Record<string, unknown>> { async buildPicture(): Promise<Record<string, unknown>> {
let obj: { picture?: any } = {}; let obj: { picture?: any } = {};
if (this.pictureFile) { if (this.pictureFile) {
const pictureObj = { const pictureObj = buildFileVariable(this.pictureFile, "picture");
picture: { obj = { ...obj, ...pictureObj };
picture: {
name: this.pictureFile.name,
alt: `${this.actualGroup.preferredUsername}'s avatar`,
file: this.pictureFile,
},
},
};
obj = { ...pictureObj };
} }
try { try {
if (this.post.picture) { if (this.post.picture && this.pictureFile) {
const oldPictureFile = (await buildFileFromIMedia( const oldPictureFile = (await buildFileFromIMedia(
this.post.picture this.post.picture
)) as File; )) as File;
@ -333,6 +336,7 @@ export default class EditPost extends mixins(GroupMixin) {
const roles = Array.isArray(givenRole) ? givenRole : [givenRole]; const roles = Array.isArray(givenRole) ? givenRole : [givenRole];
return ( return (
this.person && this.person &&
this.actualGroup &&
this.person.memberships.elements.some( this.person.memberships.elements.some(
({ parent: { id }, role }) => ({ parent: { id }, role }) =>
id === this.actualGroup.id && roles.includes(role) id === this.actualGroup.id && roles.includes(role)