Skip to content

Instantly share code, notes, and snippets.

@iparrabb
Last active Aug 28, 2019
Embed
What would you like to do?
<template>
</template>
<script>
export const onDeleteNew = `subscription OnDeleteNew {
onDeleteNew {
id
title
content
author {
name
}
}
}
`;
const getNewsLatestFirst = `query GetNewsLatestFirst(
$createdAt: ModelStringKeyConditionInput
$filter: ModelNewFilterInput
$limit: Int
$nextToken: String
) {
getNewsLatestFirst(
createdAt: $createdAt
filter: $filter
limit: $limit
nextToken: $nextToken
) {
items {
id
title
content
author {
name
}
}
nextToken
}
}
`;
import { API, graphqlOperation } from "aws-amplify";
export default {
data() {
return {
news: [],
loading: false,
subscription: null
};
},
async mounted() {
try {
this.loading = true;
const { data } = await API.graphql(
graphqlOperation(getNewsLatestFirst, {
limit: 10
})
);
this.news = data.getNewsLatestFirst.items;
// nos suscribimos para estar pendientes cuando una noticia sea eliminada
this.subscription = API.graphql(graphqlOperation(onDeleteNew)).subscribe(
{
next: data => {
this.onDeletedNew(data);
}
}
);
} catch (e) {
} finally {
this.loading = false;
}
},
methods: {
/**
* Elimina una noticia
* @param thisNew
* @returns {Promise<void>}
*/
async deleteNew(thisNew) {
try {
// eliminamos la noticia
await API.graphql(
graphqlOperation(deleteNew, { input: { id: thisNew.id } })
);
} catch (e) {
}
},
/**
* Suscripción que será ejecutada al eliminar una noticia
* @param data
*/
onDeletedNew(data) {
const thisNew = data.value.data.onDeleteNew;
this.news = this.news.filter(n => n.id !== thisNew.id);
}
},
beforeDestroy() {
this.subscription = null;
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment