From 0c4c5e54d91076b317ba8e0aa141cb378faa599f Mon Sep 17 00:00:00 2001 From: usmannasir Date: Tue, 8 Apr 2025 22:48:36 +0500 Subject: [PATCH] add loader --- .../websiteFunctions/websiteFunctions.js | 261 ++++++++++-------- 1 file changed, 153 insertions(+), 108 deletions(-) diff --git a/websiteFunctions/static/websiteFunctions/websiteFunctions.js b/websiteFunctions/static/websiteFunctions/websiteFunctions.js index 5715baa26..45689afac 100755 --- a/websiteFunctions/static/websiteFunctions/websiteFunctions.js +++ b/websiteFunctions/static/websiteFunctions/websiteFunctions.js @@ -2635,43 +2635,58 @@ app.controller('listWebsites', function ($scope, $http, $window) { return; } - var url = '/websites/fetchWPDetails'; - var data = { - domain: domain - }; - - console.log('Making request to:', url, 'with data:', data); - - $http({ - method: 'POST', - url: url, - data: $.param(data), - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'X-CSRFToken': getCookie('csrftoken') - } - }).then(function(response) { - console.log('Response received:', response); - if (response.data.status === 1 && response.data.fetchStatus === 1) { - // Find the website in the list and update its properties - $scope.WebSitesList.forEach(function(website) { - if (website.domain === domain) { - website.wp_sites = response.data.sites; - website.showWPSites = true; - console.log('Updated website:', website); - } - }); - $("#listFail").hide(); - } else { - $("#listFail").fadeIn(); - $scope.errorMessage = response.data.error_message || 'Failed to fetch WordPress sites'; - console.error('Error in response:', response.data.error_message); - } - }).catch(function(error) { - $("#listFail").fadeIn(); - $scope.errorMessage = error.message || 'An error occurred while fetching WordPress sites'; - console.error('Request failed:', error); + // Find the website in the list and set loading state + var site = $scope.WebSitesList.find(function(website) { + return website.domain === domain; }); + + if (!site) { + console.error('Website not found:', domain); + return; + } + + // Toggle visibility and handle loading state + site.showWPSites = !site.showWPSites; + + // Only fetch if we're showing and don't have data yet + if (site.showWPSites && (!site.wp_sites || !site.wp_sites.length)) { + // Set loading state + site.loadingWPSites = true; + + var url = '/websites/fetchWPDetails'; + var data = { + domain: domain + }; + + console.log('Making request to:', url, 'with data:', data); + + $http({ + method: 'POST', + url: url, + data: $.param(data), + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'X-CSRFToken': getCookie('csrftoken') + } + }).then(function(response) { + console.log('Response received:', response); + if (response.data.status === 1 && response.data.fetchStatus === 1) { + site.wp_sites = response.data.sites; + $("#listFail").hide(); + } else { + $("#listFail").fadeIn(); + $scope.errorMessage = response.data.error_message || 'Failed to fetch WordPress sites'; + console.error('Error in response:', response.data.error_message); + } + }).catch(function(error) { + $("#listFail").fadeIn(); + $scope.errorMessage = error.message || 'An error occurred while fetching WordPress sites'; + console.error('Request failed:', error); + }).finally(function() { + // Clear loading state when done + site.loadingWPSites = false; + }); + } }; $scope.visitSite = function(wp) { @@ -5746,43 +5761,58 @@ app.controller('listWebsites', function ($scope, $http, $window) { return; } - var url = '/websites/fetchWPDetails'; - var data = { - domain: domain - }; - - console.log('Making request to:', url, 'with data:', data); - - $http({ - method: 'POST', - url: url, - data: $.param(data), - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'X-CSRFToken': getCookie('csrftoken') - } - }).then(function(response) { - console.log('Response received:', response); - if (response.data.status === 1 && response.data.fetchStatus === 1) { - // Find the website in the list and update its properties - $scope.WebSitesList.forEach(function(website) { - if (website.domain === domain) { - website.wp_sites = response.data.sites; - website.showWPSites = true; - console.log('Updated website:', website); - } - }); - $("#listFail").hide(); - } else { - $("#listFail").fadeIn(); - $scope.errorMessage = response.data.error_message || 'Failed to fetch WordPress sites'; - console.error('Error in response:', response.data.error_message); - } - }).catch(function(error) { - $("#listFail").fadeIn(); - $scope.errorMessage = error.message || 'An error occurred while fetching WordPress sites'; - console.error('Request failed:', error); + // Find the website in the list and set loading state + var site = $scope.WebSitesList.find(function(website) { + return website.domain === domain; }); + + if (!site) { + console.error('Website not found:', domain); + return; + } + + // Toggle visibility and handle loading state + site.showWPSites = !site.showWPSites; + + // Only fetch if we're showing and don't have data yet + if (site.showWPSites && (!site.wp_sites || !site.wp_sites.length)) { + // Set loading state + site.loadingWPSites = true; + + var url = '/websites/fetchWPDetails'; + var data = { + domain: domain + }; + + console.log('Making request to:', url, 'with data:', data); + + $http({ + method: 'POST', + url: url, + data: $.param(data), + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'X-CSRFToken': getCookie('csrftoken') + } + }).then(function(response) { + console.log('Response received:', response); + if (response.data.status === 1 && response.data.fetchStatus === 1) { + site.wp_sites = response.data.sites; + $("#listFail").hide(); + } else { + $("#listFail").fadeIn(); + $scope.errorMessage = response.data.error_message || 'Failed to fetch WordPress sites'; + console.error('Error in response:', response.data.error_message); + } + }).catch(function(error) { + $("#listFail").fadeIn(); + $scope.errorMessage = error.message || 'An error occurred while fetching WordPress sites'; + console.error('Request failed:', error); + }).finally(function() { + // Clear loading state when done + site.loadingWPSites = false; + }); + } }; $scope.visitSite = function(wp) { @@ -9431,43 +9461,58 @@ app.controller('listWebsites', function ($scope, $http, $window) { return; } - var url = '/websites/fetchWPDetails'; - var data = { - domain: domain - }; - - console.log('Making request to:', url, 'with data:', data); - - $http({ - method: 'POST', - url: url, - data: $.param(data), - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'X-CSRFToken': getCookie('csrftoken') - } - }).then(function(response) { - console.log('Response received:', response); - if (response.data.status === 1 && response.data.fetchStatus === 1) { - // Find the website in the list and update its properties - $scope.WebSitesList.forEach(function(website) { - if (website.domain === domain) { - website.wp_sites = response.data.sites; - website.showWPSites = true; - console.log('Updated website:', website); - } - }); - $("#listFail").hide(); - } else { - $("#listFail").fadeIn(); - $scope.errorMessage = response.data.error_message || 'Failed to fetch WordPress sites'; - console.error('Error in response:', response.data.error_message); - } - }).catch(function(error) { - $("#listFail").fadeIn(); - $scope.errorMessage = error.message || 'An error occurred while fetching WordPress sites'; - console.error('Request failed:', error); + // Find the website in the list and set loading state + var site = $scope.WebSitesList.find(function(website) { + return website.domain === domain; }); + + if (!site) { + console.error('Website not found:', domain); + return; + } + + // Toggle visibility and handle loading state + site.showWPSites = !site.showWPSites; + + // Only fetch if we're showing and don't have data yet + if (site.showWPSites && (!site.wp_sites || !site.wp_sites.length)) { + // Set loading state + site.loadingWPSites = true; + + var url = '/websites/fetchWPDetails'; + var data = { + domain: domain + }; + + console.log('Making request to:', url, 'with data:', data); + + $http({ + method: 'POST', + url: url, + data: $.param(data), + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'X-CSRFToken': getCookie('csrftoken') + } + }).then(function(response) { + console.log('Response received:', response); + if (response.data.status === 1 && response.data.fetchStatus === 1) { + site.wp_sites = response.data.sites; + $("#listFail").hide(); + } else { + $("#listFail").fadeIn(); + $scope.errorMessage = response.data.error_message || 'Failed to fetch WordPress sites'; + console.error('Error in response:', response.data.error_message); + } + }).catch(function(error) { + $("#listFail").fadeIn(); + $scope.errorMessage = error.message || 'An error occurred while fetching WordPress sites'; + console.error('Request failed:', error); + }).finally(function() { + // Clear loading state when done + site.loadingWPSites = false; + }); + } }; $scope.visitSite = function(wp) {