diff --git a/apps/family/static/family/js/achievements.js b/apps/family/static/family/js/achievements.js index 5794bfe5..dba07f0d 100644 --- a/apps/family/static/family/js/achievements.js +++ b/apps/family/static/family/js/achievements.js @@ -8,8 +8,7 @@ var LOCK = false * Refresh the history table on the consumptions page. */ function refreshHistory () { - $('#history').load('/note/consos/ #history') - $('#most_used').load('/note/consos/ #most_used') + $('#history').load('/family/manage/ #history') } $(document).ready(function () { @@ -38,18 +37,14 @@ notes_display = [] buttons = [] // When the user searches an alias, we update the auto-completion -autoCompleteNote('note', 'note_list', notes, notes_display, - 'alias', 'note', 'user_note', 'profile_pic', function () { - if (buttons.length > 0 && $('#single_conso').is(':checked')) { - consumeAll() - return false - } +autoCompleteFamily('note', 'note_list', notes, notes_display, + 'note', 'user_note', 'profile_pic', function () { return true }) /** * Add a transaction from a button. - * @param dest Where the money goes + * @param fam Where the money goes * @param amount The price of the item * @param type The type of the transaction (content type id for RecurrentTransaction) * @param category_id The category identifier @@ -57,35 +52,32 @@ autoCompleteNote('note', 'note_list', notes, notes_display, * @param template_id The identifier of the button * @param template_name The name of the button */ -function addConso (dest, amount, type, category_id, category_name, template_id, template_name) { - var button = null +function addChallenge (id, name, amount) { + var challenge = null + /** Ajout de 1 à chaque clic d'un bouton déjà choisi */ buttons.forEach(function (b) { - if (b.id === template_id) { + if (b.id === id) { b.quantity += 1 - button = b + challenge = b } }) - if (button == null) { - button = { - id: template_id, - name: template_name, - dest: dest, + if (challenge == null) { + challenge = { + id: id, + name: name, quantity: 1, amount: amount, - type: type, - category_id: category_id, - category_name: category_name } - buttons.push(button) + buttons.push(challenge) } - const dc_obj = $('#double_conso') - if (dc_obj.is(':checked') || notes_display.length === 0) { - const list = dc_obj.is(':checked') ? 'consos_list' : 'note_list' + const dc_obj = true + + const list = 'consos_list' let html = '' - buttons.forEach(function (button) { - html += li('conso_button_' + button.id, button.name + - '' + button.quantity + '') + buttons.forEach(function (challenge) { + html += li('conso_button_' + challenge.id, challenge.name + + '' + challenge.quantity + '') }) document.getElementById(list).innerHTML = html @@ -95,13 +87,14 @@ function addConso (dest, amount, type, category_id, category_name, template_id, removeNote(button, 'conso_button', buttons, list)() }) }) - } else { consumeAll() } + } /** * Reset the page as its initial state. */ function reset () { + console.log("reset lancée") notes_display.length = 0 notes.length = 0 buttons.length = 0 @@ -113,7 +106,6 @@ function reset () { document.getElementById('profile_pic').src = '/static/member/img/default_picture.png' document.getElementById('profile_pic_link').href = '#' refreshHistory() - refreshBalance() LOCK = false } @@ -121,6 +113,7 @@ function reset () { * Apply all transactions: all notes in `notes` buy each item in `buttons` */ function consumeAll () { + console.log("consumeAll lancée") if (LOCK) { return } LOCK = true @@ -129,12 +122,12 @@ function consumeAll () { if (notes_display.length === 0) { document.getElementById('note').classList.add('is-invalid') - $('#note_list').html(li('', 'Ajoutez des émetteurs.', 'text-danger')) + $('#note_list').html(li('', 'Ajoutez des familles.', 'text-danger')) error = true } if (buttons.length === 0) { - $('#consos_list').html(li('', 'Ajoutez des consommations.', 'text-danger')) + $('#consos_list').html(li('', 'Ajoutez des défis.', 'text-danger')) error = true } @@ -143,79 +136,39 @@ function consumeAll () { return } - notes_display.forEach(function (note_display) { - buttons.forEach(function (button) { - consume(note_display.note, note_display.name, button.dest, button.quantity * note_display.quantity, button.amount, - button.name + ' (' + button.category_name + ')', button.type, button.category_id, button.id) + notes_display.forEach(function (family) { + buttons.forEach(function (challenge) { + grantAchievement(family, challenge) }) }) } /** - * Create a new transaction from a button through the API. - * @param source The note that paid the item (type: note) - * @param source_alias The alias used for the source (type: str) - * @param dest The note that sold the item (type: int) - * @param quantity The quantity sold (type: int) - * @param amount The price of one item, in cents (type: int) - * @param reason The transaction details (type: str) - * @param type The type of the transaction (content type id for RecurrentTransaction) - * @param category The category id of the button (type: int) - * @param template The button id (type: int) + * Create a new achievement through the API. + * @param family The selected family + * @param challenge The selected challenge */ -function consume (source, source_alias, dest, quantity, amount, reason, type, category, template) { - $.post('/api/note/transaction/transaction/', +function grantAchievement (family, challenge) { + console.log("grant lancée",family,challenge) + $.post('/api/family/achievement/', { csrfmiddlewaretoken: CSRF_TOKEN, - quantity: quantity, - amount: amount, - reason: reason, - valid: true, - polymorphic_ctype: type, - resourcetype: 'RecurrentTransaction', - source: source.id, - source_alias: source_alias, - destination: dest, - template: template + family: family.id, + challenge: challenge.id, }) .done(function () { - if (!isNaN(source.balance)) { - const newBalance = source.balance - quantity * amount - if (newBalance <= -2000) { - addMsg(interpolate(gettext('Warning, the transaction from the note %s succeed, ' + - 'but the emitter note %s is very negative.'), [source_alias, source_alias]), 'danger', 30000) - } else if (newBalance < 0) { - addMsg(interpolate(gettext('Warning, the transaction from the note %s succeed, ' + - 'but the emitter note %s is negative.'), [source_alias, source_alias]), 'warning', 30000) - } - if (source.membership && source.membership.date_end < new Date().toISOString()) { - addMsg(interpolate(gettext('Warning, the emitter note %s is no more a BDE member.'), [source_alias]), - 'danger', 30000) - } - } reset() - }).fail(function (e) { - $.post('/api/note/transaction/transaction/', - { - csrfmiddlewaretoken: CSRF_TOKEN, - quantity: quantity, - amount: amount, - reason: reason, - valid: false, - invalidity_reason: 'Solde insuffisant', - polymorphic_ctype: type, - resourcetype: 'RecurrentTransaction', - source: source.id, - source_alias: source_alias, - destination: dest, - template: template - }).done(function () { - reset() - addMsg(gettext("The transaction couldn't be validated because of insufficient balance."), 'danger', 10000) - }).fail(function () { - reset() + addMsg("Défi validé pour la famille !", 'success', 5000) + }) + .fail(function (e) { + reset() + if (e.responseJSON) { errMsg(e.responseJSON) - }) + } else if (e.responseText) { + errMsg(e.responseText) + } else { + errMsg("Erreur inconnue lors de la création de l'achievement.") + } }) } @@ -261,3 +214,222 @@ function createshiny() { shiny_class.replace('btn-outline-dark', 'btn-outline-dark-shiny') } createshiny() + + + + + +/** + * Query the 20 first matched notes with a given pattern + * @param pattern The pattern that is queried + * @param fun For each found note with the matched alias `alias`, fun(note, alias) is called. + */ +function getMatchedFamilies (pattern, fun) { + $.getJSON('/api/family/family/?format=json&alias=' + pattern + '&search=family', fun) +} + +/** + * Generate a
+ {% trans "Families" %} +
++ {% trans "Challenges" %} +
+- {% trans "Families" %} -
-- {% trans "Challenges" %} -
-- {% trans "Recent achievements history" %} -
-+ {% trans "Recent achievements history" %} +
+