Changeset View
Changeset View
Standalone View
Standalone View
assets/src/bundles/save/index.js
/** | /** | ||||
* Copyright (C) 2018-2021 The Software Heritage developers | * Copyright (C) 2018-2021 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 {csrfPost, handleFetchError, isGitRepoUrl, htmlAlert, removeUrlFragment} from 'utils/functions'; | import {csrfPost, handleFetchError, isGitRepoUrl, htmlAlert, removeUrlFragment} from 'utils/functions'; | ||||
import {swhSpinnerSrc} from 'utils/constants'; | import {swhSpinnerSrc} from 'utils/constants'; | ||||
import './save.css'; | |||||
let saveRequestsTable; | let saveRequestsTable; | ||||
function originSaveRequest(originType, originUrl, | function originSaveRequest( | ||||
acceptedCallback, pendingCallback, errorCallback) { | originType, originUrl, extraData, | ||||
acceptedCallback, pendingCallback, errorCallback | |||||
) { | |||||
// Actually trigger the origin save request | |||||
let addSaveOriginRequestUrl = Urls.api_1_save_origin(originType, originUrl); | let addSaveOriginRequestUrl = Urls.api_1_save_origin(originType, originUrl); | ||||
$('.swh-processing-save-request').css('display', 'block'); | $('.swh-processing-save-request').css('display', 'block'); | ||||
csrfPost(addSaveOriginRequestUrl) | let headers = {}; | ||||
let body = null; | |||||
if (extraData !== {}) { | |||||
body = JSON.stringify(extraData); | |||||
headers = { | |||||
'Content-Type': 'application/json' | |||||
}; | |||||
}; | |||||
csrfPost(addSaveOriginRequestUrl, headers, body) | |||||
.then(handleFetchError) | .then(handleFetchError) | ||||
.then(response => response.json()) | .then(response => response.json()) | ||||
.then(data => { | .then(data => { | ||||
$('.swh-processing-save-request').css('display', 'none'); | $('.swh-processing-save-request').css('display', 'none'); | ||||
if (data.save_request_status === 'accepted') { | if (data.save_request_status === 'accepted') { | ||||
acceptedCallback(); | acceptedCallback(); | ||||
} else { | } else { | ||||
pendingCallback(); | pendingCallback(); | ||||
} | } | ||||
}) | }) | ||||
.catch(response => { | .catch(response => { | ||||
$('.swh-processing-save-request').css('display', 'none'); | $('.swh-processing-save-request').css('display', 'none'); | ||||
response.json().then(errorData => { | response.json().then(errorData => { | ||||
errorCallback(response.status, errorData); | errorCallback(response.status, errorData); | ||||
}); | }); | ||||
}); | }); | ||||
} | } | ||||
export function maybeDisplayExtraInputs() { | |||||
// Read the actual selected value and depending on the origin type, display some extra | |||||
// inputs or hide them. | |||||
const originType = $('#swh-input-visit-type').val(); | |||||
anlambert: you need to use `flex` instead of `block` here to match bootstrap layout and correctly align… | |||||
const display = originType === 'bundle' ? 'block' : 'none'; | |||||
console.log(`originType: ${originType}, display: ${display}`); | |||||
$('#optional-origin-forms').css('display', display); | |||||
} | |||||
export function initOriginSave() { | export function initOriginSave() { | ||||
$(document).ready(() => { | $(document).ready(() => { | ||||
$.fn.dataTable.ext.errMode = 'none'; | $.fn.dataTable.ext.errMode = 'none'; | ||||
fetch(Urls.origin_save_types_list()) | fetch(Urls.origin_save_types_list()) | ||||
.then(response => response.json()) | .then(response => response.json()) | ||||
.then(data => { | .then(data => { | ||||
for (let originType of data) { | for (let originType of data) { | ||||
$('#swh-input-visit-type').append(`<option value="${originType}">${originType}</option>`); | $('#swh-input-visit-type').append(`<option value="${originType}">${originType}</option>`); | ||||
} | } | ||||
// set git as the default value as before | |||||
$('#swh-input-visit-type').val('git'); | |||||
// hard-coding to ease manuel testing, to remove | |||||
$('#swh-input-visit-type').val('bundle'); | |||||
$('#swh-input-origin-url').val('https://ftp.gnu.org/pub/pub/gnu/3dldf'); | |||||
$('#swh-input-artifact-url').val('https://ftp.gnu.org/pub/pub/gnu/3dldf/3DLDF-1.1.4.tar.gz'); | |||||
$('#swh-input-artifact-filename').val('3DLDF-1.1.4.tar.gz'); | |||||
$('#swh-input-artifact-version').val('1.1.4'); | |||||
maybeDisplayExtraInputs(); | |||||
}); | }); | ||||
saveRequestsTable = $('#swh-origin-save-requests') | saveRequestsTable = $('#swh-origin-save-requests') | ||||
.on('error.dt', (e, settings, techNote, message) => { | .on('error.dt', (e, settings, techNote, message) => { | ||||
$('#swh-origin-save-request-list-error').text('An error occurred while retrieving the save requests list'); | $('#swh-origin-save-request-list-error').text('An error occurred while retrieving the save requests list'); | ||||
console.log(message); | console.log(message); | ||||
}) | }) | ||||
.DataTable({ | .DataTable({ | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | $('#swh-save-origin-form').submit(event => { | ||||
event.preventDefault(); | event.preventDefault(); | ||||
event.stopPropagation(); | event.stopPropagation(); | ||||
$('.alert').alert('close'); | $('.alert').alert('close'); | ||||
if (event.target.checkValidity()) { | if (event.target.checkValidity()) { | ||||
$(event.target).removeClass('was-validated'); | $(event.target).removeClass('was-validated'); | ||||
let originType = $('#swh-input-visit-type').val(); | let originType = $('#swh-input-visit-type').val(); | ||||
let originUrl = $('#swh-input-origin-url').val(); | let originUrl = $('#swh-input-origin-url').val(); | ||||
originSaveRequest(originType, originUrl, | // read the extra inputs for the bundle type | ||||
let extraData = originType !== 'bundle' ? {} : { | |||||
'artifact_url': $('#swh-input-artifact-url').val(), | |||||
'artifact_filename': $('#swh-input-artifact-filename').val(), | |||||
'artifact_version': $('#swh-input-artifact-version').val() | |||||
}; | |||||
originSaveRequest(originType, originUrl, extraData, | |||||
() => $('#swh-origin-save-request-status').html(saveRequestAcceptedAlert), | () => $('#swh-origin-save-request-status').html(saveRequestAcceptedAlert), | ||||
() => $('#swh-origin-save-request-status').html(saveRequestPendingAlert), | () => $('#swh-origin-save-request-status').html(saveRequestPendingAlert), | ||||
(statusCode, errorData) => { | (statusCode, errorData) => { | ||||
$('#swh-origin-save-request-status').css('color', 'red'); | $('#swh-origin-save-request-status').css('color', 'red'); | ||||
if (statusCode === 403) { | if (statusCode === 403) { | ||||
const errorAlert = htmlAlert('danger', `Error: ${errorData['reason']}`); | const errorAlert = htmlAlert('danger', `Error: ${errorData['reason']}`); | ||||
$('#swh-origin-save-request-status').html(errorAlert); | $('#swh-origin-save-request-status').html(errorAlert); | ||||
} else if (statusCode === 429) { | } else if (statusCode === 429) { | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | export function initTakeNewSnapshot() { | ||||
); | ); | ||||
$(document).ready(() => { | $(document).ready(() => { | ||||
$('#swh-take-new-snapshot-form').submit(event => { | $('#swh-take-new-snapshot-form').submit(event => { | ||||
event.preventDefault(); | event.preventDefault(); | ||||
event.stopPropagation(); | event.stopPropagation(); | ||||
let originType = $('#swh-input-visit-type').val(); | let originType = $('#swh-input-visit-type').val(); | ||||
let originUrl = $('#swh-input-origin-url').val(); | let originUrl = $('#swh-input-origin-url').val(); | ||||
Done Inline ActionsWe'll probably need to not display the "save again" button for the origins with type 'bundle' (as we do not store the extra arguments in the save code now backend). ardumont: We'll probably need to not display the "save again" button for the origins with type 'bundle'… | |||||
let extraData = {}; | |||||
originSaveRequest(originType, originUrl, | originSaveRequest(originType, originUrl, extraData, | ||||
() => $('#swh-take-new-snapshot-request-status').html(newSnapshotRequestAcceptedAlert), | () => $('#swh-take-new-snapshot-request-status').html(newSnapshotRequestAcceptedAlert), | ||||
() => $('#swh-take-new-snapshot-request-status').html(newSnapshotRequestPendingAlert), | () => $('#swh-take-new-snapshot-request-status').html(newSnapshotRequestPendingAlert), | ||||
(statusCode, errorData) => { | (statusCode, errorData) => { | ||||
$('#swh-take-new-snapshot-request-status').css('color', 'red'); | $('#swh-take-new-snapshot-request-status').css('color', 'red'); | ||||
if (statusCode === 403) { | if (statusCode === 403) { | ||||
const errorAlert = htmlAlert('danger', `Error: ${errorData['detail']}`, true); | const errorAlert = htmlAlert('danger', `Error: ${errorData['detail']}`, true); | ||||
$('#swh-take-new-snapshot-request-status').html(errorAlert); | $('#swh-take-new-snapshot-request-status').html(errorAlert); | ||||
} else if (statusCode === 429) { | } else if (statusCode === 429) { | ||||
▲ Show 20 Lines • Show All 122 Lines • Show Last 20 Lines |
you need to use flex instead of block here to match bootstrap layout and correctly align the inputs..