From 14901477a3eba13529c58c5aa76ef15554e54c15 Mon Sep 17 00:00:00 2001 From: archon0ne Date: Wed, 3 Jul 2024 17:43:00 +0200 Subject: [PATCH] add search function to edit page --- app.py | 26 +++++++++++++++++++ templates/edit.html | 63 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/app.py b/app.py index 24007fb..6dc77b7 100644 --- a/app.py +++ b/app.py @@ -232,5 +232,31 @@ def item_image(tpl): details = get_item_details_cached(tpl) return jsonify({'image512pxLink': details.get('image512pxLink', ''), 'name': details.get('name', '')}) +@app.route('/search_items', methods=['GET']) +def search_items(): + query = request.args.get('query', '') + if not query: + return jsonify([]) + + search_query = f''' + {{ + items(name: "{query}") {{ + id + name + gridImageLink + }} + }} + ''' + + try: + response = requests.post(TARKOV_API_URL, json={'query': search_query}) + data = response.json() + items = data['data'].get('items', []) + return jsonify(items) + except Exception as e: + logging.error(f"Error searching for items with query '{query}': {e}") + logging.error(traceback.format_exc()) + return jsonify([]), 500 + if __name__ == '__main__': app.run(debug=True) diff --git a/templates/edit.html b/templates/edit.html index 6dbbf1f..5318a51 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -55,6 +55,18 @@ width: 30px; margin-right: 5px; } + .search-result-item { + display: flex; + align-items: center; + margin-bottom: 10px; + } + .search-result-item img { + width: 30px; + margin-right: 10px; + } + .search-result-item button { + margin-left: auto; + } @@ -130,6 +142,13 @@ {% endif %}

{% endif %} + + +

Search Items

+
+ +
+
@@ -173,6 +192,50 @@ $(`#barter_img_${index}`).attr('src', data.image512pxLink); }); }); + + $('#item-search').on('input', function() { + const query = $(this).val(); + if (query.length > 2) { + $.get('/search_items', { query: query }, function(data) { + let resultsHtml = ''; + data.forEach(item => { + resultsHtml += ` +
+ ${item.name} + ${item.name} + +
`; + }); + $('#search-results').html(resultsHtml); + }); + } else { + $('#search-results').empty(); + } + }); + + $(document).on('click', '.add-search-item', function() { + const itemId = $(this).closest('.search-result-item').data('id'); + const itemName = $(this).closest('.search-result-item').data('name'); + const itemImg = $(this).closest('.search-result-item').data('img'); + + const newItem = ` +
+
+
+ + ${itemName} + +
+ + +
+ +
+
+
`; + $('#barter-scheme').append(newItem); + barterIndex++; + }); });