Skip to content

Commit

Permalink
Fixed a bug where editing a record would add a new
Browse files Browse the repository at this point in the history
record and leave the original unchanged
Active row now works more intuitively after delete
Fixed Create/Edit record wording in menu and modal title
  • Loading branch information
Mattk70 committed Nov 9, 2023
1 parent 1c1be7b commit db520da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ <h5 class="modal-title" id="diagnosticsModalLabel">Diagnostic Information</h5>
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header justify-content-center">
<h5 class="modal-title" id="record-entry-modal-label">Archive Record</h5>
<h5 class="modal-title" id="record-entry-modal-label">Edit Record</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
</button>
</div>
Expand Down
10 changes: 6 additions & 4 deletions js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ function generateBirdOptionList({ store, rows, selected }) {



const getActiveRowID = () => activeRow?.id;
const getActiveRowID = () => activeRow?.rowIndex - 1;

const isSpeciesViewFiltered = (sendSpecies) => {
const filtered = document.querySelector('#speciesFilter tr.text-warning');
Expand Down Expand Up @@ -2658,7 +2658,7 @@ async function onPredictionDone({
const resultTable = document.getElementById('resultTableBody');
if (active) {
// Refresh node and scroll to active row:
activeRow = document.getElementById(active);
activeRow = resultTable.rows[active];
if (activeRow === null) { // because: after an edit the active row may not exist
const rows = resultTable.querySelectorAll('tr.daytime, tr.nighttime')
if (rows.length) {
Expand Down Expand Up @@ -3730,7 +3730,7 @@ async function createContextMenu(e) {
}
}
if (region === undefined && ! inSummary) return;
//const createOrEdit = (['archive', 'explore'].includes(STATE.mode)) && (region?.attributes.label || target.closest('#summary')) ? 'Edit' : 'Create';
const createOrEdit = (['archive', 'explore'].includes(STATE.mode)) && (region?.attributes.label || target.closest('#summary')) ? 'Edit' : 'Create';

menu.html(`
<a class="dropdown-item play ${hideInSummary}"><span class='material-symbols-outlined'>play_circle</span> Play</a>
Expand All @@ -3739,7 +3739,7 @@ async function createContextMenu(e) {
</a>
<div class="dropdown-divider ${hideInSummary}"></div>
<a class="dropdown-item" id="create-manual-record" href="#">
<span class="material-symbols-outlined">edit_document</span> Edit Record${plural}
<span class="material-symbols-outlined">edit_document</span> ${createOrEdit} Record${plural}
</a>
<a class="dropdown-item" id="context-create-clip" href="#">
<span class="material-symbols-outlined">music_note</span> Export Audio Clip${plural}
Expand All @@ -3752,6 +3752,8 @@ async function createContextMenu(e) {
<span class='delete material-symbols-outlined'>delete_forever</span> Delete Record${plural}
</a>
`);
const modalTitle = document.getElementById('record-entry-modal-label');
modalTitle.innerText = `${createOrEdit} Record`;
if (!hideInSelection) {
const contextAnalyseSelectionLink = document.getElementById('context-analyse-selection');
contextAnalyseSelectionLink.addEventListener('click', getSelectionResults);
Expand Down
46 changes: 19 additions & 27 deletions js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1683,45 +1683,37 @@ const onInsertManualRecord = async ({ cname, start, end, comment, count, file, l
const { speciesID } = await db.getAsync(`SELECT id as speciesID FROM species
WHERE cname = ?`, cname);
let res = await db.getAsync(`SELECT id,filestart FROM files WHERE name = ?`, file);
// Manual records can be added off the bat, so there may be no record of the file in either db


if (!res) { // No file: check the memory db
if (toDisk) res = await memoryDB.getAsync('SELECT id, filestart FROM files WHERE name = ?', file);
if (!res) { // Still no file, add it.
fileStart = metadata[file].fileStart;
res = await db.runAsync('INSERT OR IGNORE INTO files VALUES ( ?,?,?,?,? )',
fileID, file, metadata[file].duration, fileStart, null);
fileID = res.lastID;
changes = 1;
} else { // memory db has file
fileID = res.id;
fileStart = res.filestart;
await diskDB.runAsync('INSERT OR IGNORE INTO files VALUES ( ?,?,?,?,? )',
fileID, file, metadata[file].duration, fileStart, null);
}
if (!res) {
// Manual records can be added off the bat, so there may be no record of the file in either db
fileStart = metadata[file].fileStart;
res = await db.runAsync('INSERT OR IGNORE INTO files VALUES ( ?,?,?,?,? )',
fileID, file, metadata[file].duration, fileStart, null);
fileID = res.lastID;
changes = 1;
let durationSQL = Object.entries(metadata[file].dateDuration)
.map(entry => `(${entry.toString()},${fileID})`).join(',');
await db.runAsync(`INSERT OR IGNORE INTO duration VALUES ${durationSQL}`);
} else {
fileID = res.id;
fileStart = res.filestart;
}
let durationSQL;
if (metadata[file]) { // If we don't have file metadata, the file must be saved already
durationSQL = Object.entries(metadata[file].dateDuration)
.map(entry => `(${entry.toString()},${fileID})`).join(',');
await db.runAsync(`INSERT OR IGNORE INTO duration VALUES ${durationSQL}`);
}

let response;
const dateTime = fileStart + startMilliseconds;
const isDaylight = isDuringDaylight(dateTime, STATE.lat, STATE.lon);
confidence = confidence || 2000;
response = await db.runAsync('INSERT OR REPLACE INTO records VALUES ( ?,?,?,?,?,?,?,?,?,?)',
// Delete an existing record if it exists
const result = await db.getAsync(`SELECT id as originalSpeciesID FROM species WHERE cname = ?`, originalCname);
if (result?.originalSpeciesID) await db.runAsync('DELETE FROM records WHERE datetime = ? AND speciesID = ? AND fileID = ?', dateTime, result.originalSpeciesID, fileID)
const response = await db.runAsync('INSERT OR REPLACE INTO records VALUES ( ?,?,?,?,?,?,?,?,?,?)',
dateTime, start, fileID, speciesID, confidence, label, comment, end, parseInt(count), isDaylight);

if (response.changes && STATE.db === diskDB) {
UI.postMessage({ event: 'diskDB-has-records' });
} else {
UI.postMessage({event: 'unsaved-records'});
if (response.changes){
STATE.db === diskDB ? UI.postMessage({ event: 'diskDB-has-records' }) : UI.postMessage({event: 'unsaved-records'});
}
let test = await db.allAsync('SELECT * from records where datetime = ?', dateTime)
console.log('After insert: ',JSON.stringify(test));
return response.changes
}

Expand Down

0 comments on commit db520da

Please sign in to comment.