Open hemvall opened 1 year ago
ADD :
createNewQuestion() {
console.log('newQuestion')
if (!this.newLocQuestion.Title || !this.newLocQuestion.Answer || !this.newQuestion.categoryId) {
alert("L'un des champs n'a pas été rempli. Requête annulée.");
return false;
}
// let data = new FormData();
// data.append('file', this.newQuestion.imageFile);
let langId = 0;
fetch(`${this.$globalVar}/${localStorage.getItem('username')}/categories/${this.newQuestion.categoryId}/questions/add`, {
method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
isHighlighted: this.newQuestion.isHighlighted,
isParent: this.newQuestion.isParent,
isDisplayed: this.newQuestion.isDisplayed,
parentId: this.newQuestion.parentId,
imagePath: this.newQuestion.imageFile
})
}
)
.then(response => response.json())
.then(response => {
// post a new image
// fetch(`/question/${response}/image`, {
// method: 'post', headers: { 'Content-Type': 'multipart/form-data' },
// body: this.newQuestion.imageFile
// })
console.log('Created the main Question' + response)
// createdId = response;
for (let i = 0; i < 3; i++) {
let langCode = "en";
langId += 1;
if (i == 1) {
langCode = "fr"
}
else if (i == 2) {
langCode = "es"
}
// fetch(`${this.$globalVar}/${langCode}/tags/add`, { /* /" + this.createCategory.Language + " */
// method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
// Label: this.newLocQuestion.Label[i],
// QuestionId: response,
// LanguageId: langId
// })
// })
// console.log("tag crée avec succès")
fetch(`${this.$globalVar}/${localStorage.getItem('username')}/${langCode}/questions/localized/add`, { /* /" + this.createCategory.Language + " */
method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
Title: this.newLocQuestion.Title[i],
Answer: this.newLocQuestion.Answer[i],
Keywords: this.newLocQuestion.Keywords[i],
Status: this.newLocQuestion.Status[i],
QuestionId: response,
LanguageId: langId,
isParent: this.newQuestion.isParent,
isDisplayed: this.newQuestion.isDisplayed,
parentId: this.newQuestion.parentId,
nbVotes: 0
})
}
)
.then(response => {
if (response.ok) {
alert('La question a bien été créee !')
}
else {
alert("Erreur. La question n'a pas été crée")
}
})
.then(response => response.json())
.then(response => console.log('Created the Localized Category from' + langCode + response + langId))
.then(
console.log("in successform variable"),
this.successLayer = true,
setTimeout(() => {
this.successLayer = false;
}, 2500)
)
.catch((err) => {
alert("Erreur")
console.log(err)
return false
});
}
}
)
},
EDIT :
EditQuestion(questionId, isDisplayedItem, isHighlightedItem) {
// if (!this.FormQuestion.title || !this.FormQuestion.answer) {
// alert("L'un des champs n'a pas été rempli. Requête annulée.");
// return false;
// }
if (!this.FormQuestion.categoryId) {
this.FormQuestion.categoryId = 1;
}
fetch(`${this.$globalVar}/${localStorage.getItem('username')}/fr/questions/${this.$route.params.idQuestion}/update`, {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
categoryId: this.FormQuestion.categoryId,
title: this.FormQuestion.title,
answer: this.FormQuestion.answer,
status: this.FormQuestion.status,
keywords: this.FormQuestion.keywords,
isDisplayed: isDisplayedItem,
isHighlighted: isHighlightedItem,
imagePath: this.newImage
})
}
)
.then(response => response.json())
},
[ ] Create a detail page
[ ] Create an edit page