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; let listAction = listActionElement.dataset.listAction;
switch (listAction) { switch (listAction) {
case 'toggle-is-public': { case 'toggle-is-public': {
Utils.spaCyNLPPipelineModelToggleIsPublicRequest(this.userId, itemId); let newIsPublicValue = listActionElement.checked;
Utils.updateSpaCyNLPPipelineModelIsPublicRequest(itemId, newIsPublicValue)
.catch((response) => {
listActionElement.checked = !newIsPublicValue;
});
break; break;
} }
default: { default: {

View File

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

View File

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