diff --git a/coinmanager/coinc/static/coinc/main.js b/coinmanager/coinc/static/coinc/main.js index a26f98a..594e453 100644 --- a/coinmanager/coinc/static/coinc/main.js +++ b/coinmanager/coinc/static/coinc/main.js @@ -23,16 +23,19 @@ import { Cookie } from './modules/cookie.mjs'; $(document).ready(function() { - function render_login(name, color) { + function render_login(input_name, input_color) { // manage user login + let color = decodeURIComponent(input_color); + let name = decodeURIComponent(input_name); new Cookie('coinc-name', name) - $('label#for_select_usernames').html('Wilkommen zurück'); + render_login_color(color); let option = $('div.username option[value=' + name + ']'); - - if (!option) { - $('div.username select').append($('', { value: name, html: name })); + if (option.length == 0) { + $('div.username select').append($('', { + value: name, label: color, html: name + })); } $('div.username option[value=' + name + ']').attr('selected', 'selected'); @@ -42,8 +45,13 @@ $(document).ready(function() { function render_login_color(color) { // manage login colors - $('div.username').css('background-color', color); new Cookie('coinc-color', color); + $('div.username').css('background-color', color); + } + + + function hide_modal() { + $('div#modal_add_user').fadeOut('fast'); } @@ -55,38 +63,53 @@ $(document).ready(function() { $('button#show_add_user').click(function() { + $('div#modal_add_user').fadeIn('fast'); - $('div#modal_add_user').show('fast'); - $('button#close_modal_add_user').click(function() { - - $('div#modal_add_user').hide('fast'); }); }); - - - $('button#add_user').click(function() { - - let name = $('input#text_user').val(); - let color = $('input#text_color').val(); - - $.ajax({ - type: 'GET', - dataType: 'json', - url: 'add/user/' + name + '/color/' + color, - success: function(response) { - - if (response.error) { - $('span#response').html(response.message); - $('span#response').css('color', 'red'); - - } else { - $('span#response').css('color', 'black'); - $('div#modal_add_user').hide('fast'); - $('span#user_message').hide(); - render_login(name, color); + $(document).on('click', function(event) { + if ($('div#modal_add_user').is(':visible')) { + let target = $(event.target); + if (target.attr('id') === 'modal_add_user') { + hide_modal(); } } }); - }); + $(document).keypress(function(event) { + if (event.keyCode == 27) { // ESC is pressed + hide_modal(); + $(document).off('keypress'); + } + }); + + $('button#close_modal_add_user').click(function() { + hide_modal(); + }); + + $('button#add_user').click(function() { + + let name = encodeURIComponent($('input#text_user').val()); + let color = encodeURIComponent($('input#text_color').val()); + + $.ajax({ + type: 'GET', + dataType: 'json', + url: 'add/user/' + name + '/color/' + color, + success: function(response) { + + if (response.status === 0) { + $('span#response').fadeOut('fast'); + hide_modal(); + render_login(name, color); + + } else { + $('span#response').html(response.message); + $('span#response').css('color', 'red'); + } + } + }); + }); + + }); let cookie = new Cookie(); let name = cookie.get('coinc-name'); diff --git a/coinmanager/coinc/static/coinc/styles.css b/coinmanager/coinc/static/coinc/styles.css index fe6007d..00aee06 100644 --- a/coinmanager/coinc/static/coinc/styles.css +++ b/coinmanager/coinc/static/coinc/styles.css @@ -173,7 +173,6 @@ div.username { bottom: 0; width: 100%; height: 70px; - background-color:red; line-height: 70px; text-align: center; z-index: 1; diff --git a/coinmanager/coinc/templates/coinc/username.html b/coinmanager/coinc/templates/coinc/username.html index f526113..9493a24 100644 --- a/coinmanager/coinc/templates/coinc/username.html +++ b/coinmanager/coinc/templates/coinc/username.html @@ -1,18 +1,18 @@ - Dein Name + Name {% for user in users %} {{ user.name }} {% endfor %} - Nicht dabei? Namen hinzufügen + + Namen hinzufügen - Nutzer hinzufügen + Name hinzufügen × diff --git a/coinmanager/coinc/views.py b/coinmanager/coinc/views.py index 19c6c82..5272f2b 100644 --- a/coinmanager/coinc/views.py +++ b/coinmanager/coinc/views.py @@ -18,6 +18,7 @@ +from json import dumps from datetime import datetime from django.http import HttpResponse @@ -107,7 +108,7 @@ def show_country(country, single_country=False): return { 'country': c, 'single_country': single_country, - 'users': User.objects.order_by('name').values('name','color') + 'users': User.objects.order_by('id').values('name','color') } @@ -172,24 +173,23 @@ def show_coin(country, year, stamp, value): def add_user(request, username, color): ''' add a user ''' - error = False + status = 0 message = '' if username and color: try: existing_user = User.objects.get(name=username) - error = True + status = 1 message = f"'{username}' ist schon vergeben." except User.DoesNotExist: -# u = User(name=username, color=color) -# u.save() - message = f"Gespeichert" + User.objects.create(name=username, color=color) + message = 'success' else: - error = True + status = 1 message = 'Du musst einen Namen und Farbe angeben.' - import json - return HttpResponse(json.dumps({ 'error': error, 'message': message })) + + return HttpResponse(dumps({ 'status': status, 'message': message })) # TODO