135 lines
4.8 KiB
JavaScript
135 lines
4.8 KiB
JavaScript
|
|
document.addEventListener('DOMContentLoaded', () => {
|
||
|
|
const actionForm = document.getElementById('action-form');
|
||
|
|
const actionIsinInput = document.querySelector('input[name="action_isin"]');
|
||
|
|
const isinInput = document.getElementById('action-isin');
|
||
|
|
const tickerInput = document.getElementById('action-ticker');
|
||
|
|
const yahooInput = document.getElementById('action-yahoo_code');
|
||
|
|
const companyInput = document.getElementById('action-company_name');
|
||
|
|
const feedbackMessage = document.getElementById('action-form-feedback');
|
||
|
|
const createButton = document.querySelector('.create-button');
|
||
|
|
const updateButton = document.querySelector('.update-button');
|
||
|
|
const cancelEditButton = document.querySelector('.cancel-edit-button');
|
||
|
|
const actionsPanel = document.querySelector('.actions-panel');
|
||
|
|
const editActionButtons = document.querySelectorAll('.edit-action-button');
|
||
|
|
|
||
|
|
const getExistingActions = () => Array.from(document.querySelectorAll('.action-row')).map((row) => ({
|
||
|
|
isin: row.dataset.isin || '',
|
||
|
|
ticker: row.dataset.ticker || '',
|
||
|
|
yahoo_code: row.dataset.yahoo_code || '',
|
||
|
|
company_name: row.dataset.company_name || '',
|
||
|
|
}));
|
||
|
|
|
||
|
|
const setFeedback = (message) => {
|
||
|
|
if (!feedbackMessage) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
feedbackMessage.textContent = message;
|
||
|
|
feedbackMessage.style.display = message ? 'block' : 'none';
|
||
|
|
};
|
||
|
|
|
||
|
|
const validateDuplicateFields = () => {
|
||
|
|
const existingActions = getExistingActions();
|
||
|
|
const currentActionIsin = actionIsinInput.value.trim();
|
||
|
|
const currentIsin = isinInput.value.trim();
|
||
|
|
const currentTicker = tickerInput.value.trim();
|
||
|
|
const currentYahoo = yahooInput.value.trim();
|
||
|
|
|
||
|
|
const duplicateFields = [];
|
||
|
|
|
||
|
|
const isDuplicate = (fieldName, value) => {
|
||
|
|
if (!value) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return existingActions.some((entry) => {
|
||
|
|
if (!entry[fieldName]) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (currentActionIsin && entry.isin === currentActionIsin) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return entry[fieldName].localeCompare(value, undefined, { sensitivity: 'base' }) === 0;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
if (isDuplicate('isin', currentIsin)) {
|
||
|
|
duplicateFields.push('ISIN');
|
||
|
|
}
|
||
|
|
if (isDuplicate('ticker', currentTicker)) {
|
||
|
|
duplicateFields.push('symbole');
|
||
|
|
}
|
||
|
|
if (isDuplicate('yahoo_code', currentYahoo)) {
|
||
|
|
duplicateFields.push('code Yahoo');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (duplicateFields.length > 0) {
|
||
|
|
const joined = duplicateFields.join(', ');
|
||
|
|
setFeedback(`Doublon détecté pour le(s) champ(s) : ${joined}. Veuillez modifier la saisie.`);
|
||
|
|
createButton.disabled = true;
|
||
|
|
updateButton.disabled = true;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
setFeedback('');
|
||
|
|
createButton.disabled = false;
|
||
|
|
updateButton.disabled = false;
|
||
|
|
return true;
|
||
|
|
};
|
||
|
|
|
||
|
|
const resetFormMode = () => {
|
||
|
|
actionForm.reset();
|
||
|
|
actionIsinInput.value = '';
|
||
|
|
createButton.removeAttribute('disabled');
|
||
|
|
createButton.hidden = false;
|
||
|
|
updateButton.hidden = true;
|
||
|
|
cancelEditButton.hidden = true;
|
||
|
|
isinInput.removeAttribute('readonly');
|
||
|
|
isinInput.removeAttribute('disabled');
|
||
|
|
actionsPanel.classList.remove('edit-mode');
|
||
|
|
setFeedback('');
|
||
|
|
};
|
||
|
|
|
||
|
|
const setEditMode = (row) => {
|
||
|
|
const isin = row.dataset.isin || '';
|
||
|
|
const ticker = row.dataset.ticker || '';
|
||
|
|
const yahooCode = row.dataset.yahoo_code || '';
|
||
|
|
const companyName = row.dataset.company_name || '';
|
||
|
|
|
||
|
|
actionIsinInput.value = isin;
|
||
|
|
isinInput.value = isin;
|
||
|
|
tickerInput.value = ticker;
|
||
|
|
yahooInput.value = yahooCode;
|
||
|
|
companyInput.value = companyName;
|
||
|
|
|
||
|
|
createButton.setAttribute('disabled', 'disabled');
|
||
|
|
createButton.hidden = true;
|
||
|
|
updateButton.hidden = false;
|
||
|
|
cancelEditButton.hidden = false;
|
||
|
|
isinInput.setAttribute('readonly', 'readonly');
|
||
|
|
actionsPanel.classList.add('edit-mode');
|
||
|
|
validateDuplicateFields();
|
||
|
|
};
|
||
|
|
|
||
|
|
const handleInputChange = () => {
|
||
|
|
validateDuplicateFields();
|
||
|
|
};
|
||
|
|
|
||
|
|
editActionButtons.forEach((button) => {
|
||
|
|
button.addEventListener('click', () => {
|
||
|
|
const row = button.closest('tr');
|
||
|
|
if (!row) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
setEditMode(row);
|
||
|
|
isinInput.focus();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
[isinInput, tickerInput, yahooInput].forEach((input) => {
|
||
|
|
input.addEventListener('input', handleInputChange);
|
||
|
|
});
|
||
|
|
|
||
|
|
cancelEditButton.addEventListener('click', resetFormMode);
|
||
|
|
|
||
|
|
resetFormMode();
|
||
|
|
});
|