Restructure toggle ispublic requests

This commit is contained in:
Patrick Jentsch 2023-03-06 15:04:06 +01:00
parent cfa4fa68f2
commit 7770d4d478
3 changed files with 61 additions and 29 deletions

View File

@ -119,7 +119,11 @@ class SpaCyNLPPipelineModelList extends ResourceList {
let listAction = listActionElement.dataset.listAction;
switch (listAction) {
case 'toggle-is-public': {
Utils.spaCyNLPPipelineModelToggleIsPublicRequest(this.userId, itemId);
let newIsPublicValue = listActionElement.checked;
Utils.updateSpaCyNLPPipelineModelIsPublicRequest(itemId, newIsPublicValue)
.catch((response) => {
listActionElement.checked = !newIsPublicValue;
});
break;
}
default: {

View File

@ -128,7 +128,11 @@ class TesseractOCRPipelineModelList extends ResourceList {
let listAction = listActionElement.dataset.listAction;
switch (listAction) {
case 'toggle-is-public': {
Utils.tesseractOCRPipelineModelToggleIsPublicRequest(this.userId, itemId);
let newIsPublicValue = listActionElement.checked;
Utils.updateTesseractOCRPipelineModelIsPublicRequest(itemId, newIsPublicValue)
.catch((response) => {
listActionElement.checked = !newIsPublicValue;
});
break;
}
default: {

View File

@ -101,13 +101,21 @@ class Utils {
static updateCorpusFollowerRole(corpusId, followerId, roleName) {
return new Promise((resolve, reject) => {
fetch(`/corpora/${corpusId}/followers/${followerId}/role`, {method: 'POST', headers: {Accept: 'application/json', 'Content-Type': 'application/json'}, body: JSON.stringify({role: roleName})})
let fetchRessource = `/corpora/${corpusId}/followers/${followerId}/role`;
let fetchOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({role: roleName})
};
fetch(fetchRessource, fetchOptions)
.then(
(response) => {
if (response.ok) {
app.flash('Role updated', 'corpus');
resolve(response);
return;
} else {
app.flash(`${response.statusText}`, 'error');
reject(response);
@ -179,7 +187,15 @@ class Utils {
static unfollowCorpusRequest(corpusId, followerId) {
return new Promise((resolve, reject) => {
fetch(`/corpora/${corpusId}/followers/${followerId}/unfollow`, {method: 'POST', headers: {Accept: 'application/json'}})
let fetchRessource = `/corpora/${corpusId}/followers/${followerId}/unfollow`;
let fetchOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
fetch(fetchRessource, fetchOptions)
.then(
(response) => {
if (response.ok) {
@ -683,23 +699,27 @@ class Utils {
});
}
static tesseractOCRPipelineModelToggleIsPublicRequest(userId, tesseractOCRPipelineModelId, is_public) {
static updateTesseractOCRPipelineModelIsPublicRequest(tesseractOCRPipelineModelId, newIsPublicValue) {
return new Promise((resolve, reject) => {
let tesseractOCRPipelineModel;
try {
tesseractOCRPipelineModel = app.data.users[userId].tesseract_ocr_pipeline_models[tesseractOCRPipelineModelId];
} catch (error) {
tesseractOCRPipelineModel = {};
}
fetch(`/contributions/tesseract-ocr-pipeline-models/${tesseractOCRPipelineModelId}/toggle-public-status`, {method: 'POST', headers: {Accept: 'application/json'}})
let fetchRessource = `/contributions/tesseract-ocr-pipeline-models/${tesseractOCRPipelineModelId}/is_public`;
let fetchOptions = {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(newIsPublicValue)
};
fetch(fetchRessource, fetchOptions)
.then(
(response) => {
if (response.status === 403) {
app.flash('Forbidden', 'error');
if (response.ok) {
response.json().then((data) => {app.flash(data);});
resolve(response);
} else {
app.flash(`${response.statusText}`, 'error');
reject(response);
}
resolve(response);
},
(response) => {
app.flash('Something went wrong', 'error');
@ -709,23 +729,27 @@ class Utils {
});
}
static spaCyNLPPipelineModelToggleIsPublicRequest(userId, spaCyNLPPipelineModelId) {
static updateSpaCyNLPPipelineModelIsPublicRequest(SpaCyNLPPipelineModelId, newIsPublicValue) {
return new Promise((resolve, reject) => {
let spaCyNLPPipelineModel;
try {
spaCyNLPPipelineModel = app.data.users[userId].spacy_nlp_pipeline_models[spaCyNLPPipelineModelId];
} catch (error) {
spaCyNLPPipelineModel = {};
}
fetch(`/contributions/spacy-nlp-pipeline-models/${spaCyNLPPipelineModelId}/toggle-public-status`, {method: 'POST', headers: {Accept: 'application/json'}})
let fetchRessource = `/contributions/spacy-nlp-pipeline-models/${SpaCyNLPPipelineModelId}/is_public`;
let fetchOptions = {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(newIsPublicValue)
};
fetch(fetchRessource, fetchOptions)
.then(
(response) => {
if (response.status === 403) {
app.flash('Forbidden', 'error');
if (response.ok) {
response.json().then((data) => {app.flash(data);});
resolve(response);
} else {
app.flash(`${response.statusText}`, 'error');
reject(response);
}
resolve(response);
},
(response) => {
app.flash('Something went wrong', 'error');