From a2709787289d634a2dc33a18c6948089d626a53a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20MARQUET?= Date: Thu, 27 Jun 2024 14:22:15 +0200 Subject: [PATCH] Add remove repos button and change background color --- index.html | 2 +- nginx.conf | 7 +++++++ ntfy.py | 3 ++- ntfy_api.py | 30 +++++++++++++++++++++++++++++- script.js | 38 +++++++++++++++++++++++++++++++++++++- 5 files changed, 76 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 43de228..2156d6f 100644 --- a/index.html +++ b/index.html @@ -7,7 +7,7 @@ - +

Github-Ntfy

Add a repo

diff --git a/nginx.conf b/nginx.conf index 60b9b1b..57a8ca7 100644 --- a/nginx.conf +++ b/nginx.conf @@ -28,5 +28,12 @@ http { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } + location /delete_repo { + proxy_pass http://127.0.0.1:5000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } } } diff --git a/ntfy.py b/ntfy.py index f470a59..b012139 100644 --- a/ntfy.py +++ b/ntfy.py @@ -133,6 +133,7 @@ if __name__ == "__main__": else: logger.error("Usage: python ntfy.py") logger.error( - "auth: can be generataed by the folowing command: echo -n 'username:password' | base64 and need to be stored in a file named auth.txt") + "auth: can be generataed by the folowing command: echo -n 'username:password' | base64 and need to be " + "stored in a file named auth.txt") logger.error("NTFY_URL: the url of the ntfy server need to be stored in an environment variable named NTFY_URL") diff --git a/ntfy_api.py b/ntfy_api.py index 74da01b..dc47cca 100644 --- a/ntfy_api.py +++ b/ntfy_api.py @@ -6,7 +6,6 @@ app = Flask(__name__) CORS(app) app.logger.setLevel("WARNING") - def get_db_connection(): conn = sqlite3.connect('/github-ntfy/watched_repos.db') conn.row_factory = sqlite3.Row @@ -57,5 +56,34 @@ def get_watched_repos(): return jsonify(watched_repos) +@app.route('/delete_repo', methods=['POST']) +def delete_repo(): + data = request.json + repo = data.get('repo') + + # Vérifier si le champ 'repo' est présent dans les données JSON + if not repo: + return jsonify({"error": "The repo field is required."}), 400 + + # Établir une connexion à la base de données + conn = get_db_connection() + cursor = conn.cursor() + + try: + # Vérifier si le dépôt existe dans la base de données + cursor.execute("SELECT * FROM watched_repos WHERE repo=?", (repo,)) + existing_repo = cursor.fetchone() + if not existing_repo: + return jsonify({"error": f"The repo {repo} is not in the database."}), 404 + + # Supprimer le dépôt de la base de données + cursor.execute("DELETE FROM watched_repos WHERE repo=?", (repo,)) + conn.commit() + return jsonify({"message": f"The repo {repo} as been deleted from the watched repos."}) + finally: + # Fermer la connexion à la base de données + close_db_connection(conn) + + if __name__ == "__main__": app.run(debug=False) diff --git a/script.js b/script.js index f106fc1..b038ca8 100644 --- a/script.js +++ b/script.js @@ -32,7 +32,24 @@ function refreshWatchedRepos() { // Ajouter chaque dépôt surveillé à la liste data.forEach(repo => { const listItem = document.createElement('li'); - listItem.textContent = repo; + const repoName = document.createElement('span'); + repoName.textContent = repo; + repoName.className = 'repo-name'; + listItem.appendChild(repoName); + + const deleteButton = document.createElement('button'); + deleteButton.textContent = ' X'; + deleteButton.className = 'delete-btn text-red-500 ml-2'; + deleteButton.addEventListener('click', () => { + // Remove the repo from the watched repos + // This is a placeholder. Replace it with your actual code to remove the repo from the watched repos. + removeRepoFromWatchedRepos(repo); + + // Remove the repo from the DOM + listItem.remove(); + }); + listItem.appendChild(deleteButton); + watchedReposList.appendChild(listItem); }); }) @@ -41,5 +58,24 @@ function refreshWatchedRepos() { }); } +function removeRepoFromWatchedRepos(repo) { + fetch('/delete_repo', { + method: 'POST', + headers: { + 'Access-Control-Allow-Origin': '*', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({repo: repo}) + }) + .then(response => { + if (!response.ok) { + throw new Error('Erreur lors de la suppression du dépôt'); + } + }) + .catch(error => { + console.error('Error:', error); + }); +} + // Appeler la fonction pour charger les dépôts surveillés au chargement de la page refreshWatchedRepos(); \ No newline at end of file