Use ArenServices for sondage

This commit is contained in:
Henintsoa 2021-12-12 11:09:43 +03:00
parent 8c26b56e91
commit 85c001950a
3 changed files with 166 additions and 110 deletions

View file

@ -317,6 +317,8 @@ User.prototype.oneToMany = {
comments: [Comment, "owner"],
createdDebates: [Debate, "owner"],
notifications: [Notification, "owner"],
sondage: [Sondage, "author"],
vote: [Vote, "authorId"],
};
User.prototype.manyToOne = {
institution: [Institution, "users"],
@ -336,6 +338,69 @@ User.prototype.is = function (authority) {
return Authority._value[this.authority] >= Authority._value[authority];
};
function Vote(obj = {}) {
Entity.call(this, obj);
}
Vote.prototype.attrs = {
id: Number,
opinion: String,
createdAt: Date,
};
Vote.prototype.manyToOne = {
authorId: [User, "vote"],
subThemeId: [Sondage, "sondage"],
vote: [Choice, "votes"]
};
function Choice(obj = {}) {
Entity.call(this, obj);
}
Choice.prototype.attrs = {
id: Number,
title: String,
description: String,
url: String,
img: String,
neutral: Number,
against: Number,
for: Number,
createdAt: Date,
};
Choice.prototype.manyToOne = {
themeId: [Sondage, "choices"],
};
Choice.prototype.oneToMany = {
votes: [Vote, "vote"],
};
function Agenda(obj = {}) {
Entity.call(this, obj);
}
Agenda.prototype.attrs = {
id: Number,
title: String,
description: String,
url: String,
date: Date,
};
function Sondage(obj = {}) {
Entity.call(this, obj);
}
Sondage.prototype.attrs = {
id: Number,
title: String,
description: String,
url: String,
createdAt: Date,
expiracyDate: Date,
};
Sondage.prototype.manyToOne = {
author: [User, "sondage"],
};
Sondage.prototype.oneToMany = {
sondage: [Vote, "subThemeId"],
choices: [Choice, "themeId"],
};
const ApiService = function (anUrl, locale) {
let self = this;
@ -349,6 +414,10 @@ const ApiService = function (anUrl, locale) {
this.Notification = [];
this.Team = [];
this.User = [];
this.Sondage = [];
this.Agenda = [];
this.Choice = [];
this.Vote = [];
},
detach(obj) {
this[obj.constructor.name].remove(obj);
@ -443,8 +512,8 @@ const ApiService = function (anUrl, locale) {
);
if (manyToOne[foreignKey][1]) {
let foreignCollection = that[foreignKey][manyToOne[foreignKey][1]];
if (!foreignCollection.includes(that)) {
foreignCollection.push(that);
if (!(foreignCollection?.includes(that))) {
foreignCollection?.push(that);
}
}
}
@ -789,6 +858,10 @@ const ApiService = function (anUrl, locale) {
this.Notifications = new EntityProcessor("notifications", Notification);
this.CommentListener = new Listener("comments", Comment);
this.NotificationListener = new Listener("notifications", Notification);
this.Agenda = new EntityProcessor("agenda/calendars", Agenda);
this.Sondage = new EntityProcessor("themes", Sondage);
this.Choice = new EntityProcessor("choices", Choice);
this.Vote = new EntityProcessor("votes", Vote);
this.Users.getLoged = function (params = {}) {
params.method = "GET";
@ -976,4 +1049,16 @@ const ApiService = function (anUrl, locale) {
params.method = "PUT";
this.call(params);
};
this.Sondage.removeCurrent = function (params = {}) {
params.method = "DELETE";
params.path = `/delete/${params.data.id}`;
let onSuccess = params.onSuccess;
params.onSuccess = function () {
self.Store.remove(params.data.sondage);
if(onSuccess){
onSuccess();
}
}
this.call(params);
}
};

View file

@ -114,7 +114,8 @@ module.exports = {
return {
categories: false,
agenda: [],
sondages: [],
// sondages: [],
sondages: false,
};
},
created() {
@ -141,13 +142,19 @@ module.exports = {
},
//Fetch sondages
async fetchSondages() {
try {
let agenda = await axios.get(`${baseUrl}/ws/themes`);
this.sondages.push(...agenda.data);
} catch (expetion) {
console.error(expetion);
}
// async fetchSondages() {
// try {
// let agenda = await axios.get(`${baseUrl}/ws/themes`);
// this.sondages.push(...agenda.data);
// } catch (expetion) {
// console.error(expetion);
// }
// },
fetchSondages() {
ArenService.Sondage.getAll({
onSuccess: (sondages)=>(this.sondages = sondages),
})
},
},
};

View file

@ -3,7 +3,7 @@
<base-layout id="voting">
<template v-slot:title>
<h1>Sondage</h1>
<div v-if="loggedUser.is('ADMIN') || loggedUser.is('SUPERADMIN')">
<div v-if="user.is('ADMIN') || user.is('SUPERADMIN')">
<button
title="Supprimer le sondage"
class="delete-btn space-top"
@ -238,10 +238,6 @@
</style>
<script>
const getUrl = window.location;
// let baseUrl = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split("/")[1];
// if (baseUrl.endsWith("/")) {
// baseUrl = baseUrl.slice(0, -1);
// }
let baseUrl = getUrl.protocol + "//" + getUrl.host;
module.exports = {
data() {
@ -249,117 +245,85 @@ module.exports = {
user: false,
theme: false,
choices: [],
votes: [],
loggedUser: false,
votes: false,
};
},
created() {
this.fetchTheme();
this.fetchUser();
this.fetchVotes();
if (!this.$root.user.is("USER")) {
this.$router.push("/404");
return;
}
this.getCurrentUser();
this.fetchTheme();
this.fetchVotes();
},
methods: {
getCurrentUser: function () {
getCurrentUser() {
ArenService.Users.getLoged({
onSuccess: (logedUser) => {
this.loggedUser = logedUser;
console.log(logedUser)
ArenService.NotificationListener.listen({
onMessage: (notif) => {
this.user.notifications.push(notif);
let message = this.$t(
"notification." + notif.content.message,
notif.content.details
);
this.$toast(message);
new BrowserNotification("AREN", {
body: message,
});
this.user = logedUser;
},
});
},
onError: (e) => {
this.logout();
fetchTheme() {
ArenService.Sondage.get({
id: this.$route.query.id,
onSuccess: (myTheme) => {
this.theme = myTheme;
this.choices = [...myTheme.choices];
},
});
},
fetchUser: async function () {
const _user = await axios.get(`${baseUrl}/ws/users/me`);
this.user = _user.data;
console.log(this.user);
},
fetchTheme: async function () {
try {
const myTheme = await axios.get(
`${baseUrl}/ws/themes/${this.$route.query.id}`
);
this.theme = myTheme.data;
this.choices = [...myTheme.data.choices];
} catch (error) {
console.error(error);
}
},
vote: async function (subtheme, opinion) {
try {
const makeVote = await axios({
method: "post",
url: `${baseUrl}/ws/votes`,
vote(subtheme, opinion) {
ArenService.Vote.create({
data: {
subThemeId: subtheme,
authorId: this.user,
opinion: opinion,
},
});
// console.log(makeVote);
onSuccess: () => {
location.reload();
this.fetchTheme();
} catch (error) {
console.error(error);
}
},
fetchVotes: async function () {
const _votes = await axios.get(`${baseUrl}/ws/votes`);
this.votes = [..._votes.data];
// console.log(this.votes.filter(el => el.authorId == this.user.id));
});
},
checkVoteAvalaibility: function (_subthemeId) {
checkVoteAvalaibility(_subthemeId) {
const userId = this.user.id;
const filteredVotes = this.votes.filter((el) => el.authorId == userId);
if (this.votes) {
const filteredVotes = this.votes?.filter(
(el) => el.authorId?.id == userId
);
const filteredBySubTheme = filteredVotes.map(
(el) => el.subThemeId.id || el.subThemeId
(el) => el.subThemeId?.id ?? el.subThemeId
);
const arr = filteredBySubTheme.filter((el) => el == _subthemeId);
return arr.length <= 0;
}
return false;
},
deleteCurrentSondage: function () {
try {
swal({
fetchVotes() {
ArenService.Vote.getAll({
onSuccess: (f_votes) => (this.votes = f_votes),
});
},
deleteCurrentSondage() {
this.$confirm({
title: "Êtes-vous sûr?",
text: "Le sondage sera supprimé avec toutes ses informations",
icon: "warning",
buttons: ["Annuler", true],
dangerMode: true,
}).then(async (willDelete) => {
if (willDelete) {
let _ = await axios.delete(
`${baseUrl}/ws/themes/delete/${this.$route.query.id}`
);
message: "Le sondage sera supprimé avec toutes ses informations",
callback: (returnValue) => {
if (returnValue) {
ArenService.Sondage.removeCurrent({
data: { id: this.$route.query.id, sondage: this.theme },
onSuccess: () =>
swal("Succès!", "Le sondage a été supprimé", "success").then(
(value) => {
location.href = baseUrl;
}
);
this.$router.push("/");
}
),
});
} catch (error) {
swal("Erreur!", `${error}`, "error");
console.log(error);
}
},
});
},
},
};
</script>