Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/browse/origin-save.js
/** | /** | ||||
* Copyright (C) 2018 The Software Heritage developers | * Copyright (C) 2018 The Software Heritage developers | ||||
* See the AUTHORS file at the top-level directory of this distribution | * See the AUTHORS file at the top-level directory of this distribution | ||||
* License: GNU Affero General Public License version 3, or any later version | * License: GNU Affero General Public License version 3, or any later version | ||||
* See top-level LICENSE file for more information | * See top-level LICENSE file for more information | ||||
*/ | */ | ||||
import {handleFetchError, csrfPost, isGitRepoUrl, removeUrlFragment} from 'utils/functions'; | import {handleFetchError, csrfPost, isGitRepoUrl, removeUrlFragment} from 'utils/functions'; | ||||
import {validate} from 'validate.js'; | import {validate} from 'validate.js'; | ||||
let saveRequestsTable; | let saveRequestsTable; | ||||
function originSaveRequest(originType, originUrl, | function originSaveRequest(originType, originUrl, | ||||
acceptedCallback, pendingCallback, errorCallback) { | acceptedCallback, pendingCallback, errorCallback) { | ||||
let addSaveOriginRequestUrl = Urls.browse_origin_save_request(originType, originUrl); | let addSaveOriginRequestUrl = Urls.browse_origin_save_request(originType, originUrl); | ||||
let grecaptchaData = {'g-recaptcha-response': grecaptcha.getResponse()}; | let grecaptchaData = {}; | ||||
if (swh.webapp.isReCaptchaActivated()) { | |||||
grecaptchaData['g-recaptcha-response'] = grecaptcha.getResponse(); | |||||
} | |||||
let headers = { | let headers = { | ||||
'Accept': 'application/json', | 'Accept': 'application/json', | ||||
'Content-Type': 'application/json' | 'Content-Type': 'application/json' | ||||
}; | }; | ||||
let body = JSON.stringify(grecaptchaData); | let body = JSON.stringify(grecaptchaData); | ||||
csrfPost(addSaveOriginRequestUrl, headers, body) | csrfPost(addSaveOriginRequestUrl, headers, body) | ||||
.then(handleFetchError) | .then(handleFetchError) | ||||
.then(response => response.json()) | .then(response => response.json()) | ||||
.then(data => { | .then(data => { | ||||
if (data.save_request_status === 'accepted') { | if (data.save_request_status === 'accepted') { | ||||
acceptedCallback(); | acceptedCallback(); | ||||
} else { | } else { | ||||
pendingCallback(); | pendingCallback(); | ||||
} | } | ||||
if (swh.webapp.isReCaptchaActivated()) { | |||||
grecaptcha.reset(); | grecaptcha.reset(); | ||||
} | |||||
}) | }) | ||||
.catch(response => { | .catch(response => { | ||||
if (response.status === 403) { | if (response.status === 403) { | ||||
errorCallback(); | errorCallback(); | ||||
} | } | ||||
if (swh.webapp.isReCaptchaActivated()) { | |||||
grecaptcha.reset(); | grecaptcha.reset(); | ||||
} | |||||
}); | }); | ||||
} | } | ||||
export function initOriginSave() { | export function initOriginSave() { | ||||
$(document).ready(() => { | $(document).ready(() => { | ||||
$.fn.dataTable.ext.errMode = 'throw'; | $.fn.dataTable.ext.errMode = 'throw'; | ||||
▲ Show 20 Lines • Show All 207 Lines • Show Last 20 Lines |