real-estate / static /js /utils.js
brestok's picture
init
d0e0a14
async function deleteRecord(clickedElement, type) {
const recordElement = clickedElement.closest('.record');
makeLoading()
if (recordElement) {
const recordID = recordElement.getAttribute('record-id');
const response = await fetch('/delete-record', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({'record_id': parseInt(recordID), 'type': type})
});
if (response.ok) {
stopLoading()
recordElement.remove();
}
}
}
async function getRecordTranscription(recordId, type) {
try {
makeLoading()
const url = `/record/${recordId}/${type}/transcription`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
const result = await response.json();
transcriptionReportBlock.innerHTML = marked.parse(result)
stopLoading()
} catch (error) {
console.error('Failed to fetch transcription:', error);
stopLoading()
transcriptionReportBlock.innerText = '';
}
}
async function playRecord(recordId) {
try {
const url = `/record/${recordId}/audio/play`;
makeLoading()
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
const audioBase64 = await response.json();
const audioSrc = `data:audio/mp3;base64,${audioBase64}`;
const audio = new Audio(audioSrc);
stopLoading()
await audio.play();
} catch (error) {
stopLoading()
console.error('Failed to fetch transcription:', error);
}
}
async function createAIReport() {
try {
transcriptionReportBlock.innerText = ''
const records = document.getElementsByClassName('record');
const imageRecordIDs = [];
const audioRecordIDs = [];
for (let i = 0; i < records.length; i++) {
const recordID = records[i].getAttribute('record-id');
const objectType = records[i].getAttribute('object-type');
if (objectType === 'image') {
imageRecordIDs.push(recordID);
} else if (objectType === 'audio') {
audioRecordIDs.push(recordID);
}
}
if (imageRecordIDs.length === 0 && audioRecordIDs.length === 0) {
alert('Record at least one audio or image.');
}
const requestBody = JSON.stringify({
audio_ids: audioRecordIDs,
image_ids: imageRecordIDs,
language: selectLanguage.value
});
makeLoading()
const response = await fetch('/report', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: requestBody
});
if (!response.ok) {
const errorData = await response.json()
throw new Error(errorData.detail);
}
const result = await response.json();
transcriptionReportBlock.innerHTML = marked.parse(result)
stopLoading()
} catch (error) {
console.log(error)
transcriptionReportBlock.innerHTML = `<h4 style="padding-top: 0.7em">${error}</h4>`;
stopLoading()
}
}
function generateUUID() {
const arr = new Uint8Array(16);
window.crypto.getRandomValues(arr);
arr[6] = (arr[6] & 0x0f) | 0x40;
arr[8] = (arr[8] & 0x3f) | 0x80;
return ([...arr].map((b, i) =>
(i === 4 || i === 6 || i === 8 || i === 10 ? "-" : "") + b.toString(16).padStart(2, "0")
).join(""));
}
function signOut() {
const confirmed = confirm('Are you sure you want to sign out?');
if (!confirmed) {
return false;
}
fetch('/auth/jwt/logout', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
.then(function (response) {
if (response.ok) {
window.location.href = 'http://' + window.location.host + '/user/login';
} else {
console.error('Error logging out user');
}
})
}