Azeez98 commited on
Commit
3fe95c4
·
verified ·
1 Parent(s): 86136f0

Update static/js/index.js

Browse files
Files changed (1) hide show
  1. static/js/index.js +72 -4
static/js/index.js CHANGED
@@ -68,22 +68,90 @@ async function handleDockerFormSubmit(event) {
68
  }
69
  }
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  async function handlePipFormSubmit(event) {
72
  event.preventDefault(); // Prevent the form from submitting the traditional way
73
  const submitButton = document.getElementById('pip-submit-button');
74
  const messageDiv = document.getElementById('pip-message');
 
 
75
  submitButton.disabled = true; // Disable the submit button
76
  messageDiv.textContent = "Downloading dependencies..."; // Show downloading message
77
  messageDiv.style.display = 'block';
 
 
78
 
79
  const formData = new FormData(event.target);
 
80
  try {
81
  const response = await fetch('/download-dependencies', {
82
  method: 'POST',
83
  body: formData
84
  });
85
  if (response.ok) {
86
- const blob = await response.blob();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  const url = window.URL.createObjectURL(blob);
88
  const a = document.createElement('a');
89
  a.style.display = 'none';
@@ -92,13 +160,13 @@ async function handlePipFormSubmit(event) {
92
  document.body.appendChild(a);
93
  a.click();
94
  window.URL.revokeObjectURL(url);
95
- messageDiv.textContent = "Download complete!"; // Show download complete message
96
  } else {
97
- throw new Error('Download failed');
 
98
  }
99
  } catch (error) {
100
  console.error('Error:', error);
101
- messageDiv.innerHTML = "Error downloading dependencies.<br>Please try again."; // Show error message in two lines
102
  } finally {
103
  submitButton.disabled = false; // Re-enable the submit button after download
104
  }
 
68
  }
69
  }
70
 
71
+ // async function handlePipFormSubmit(event) {
72
+ // event.preventDefault(); // Prevent the form from submitting the traditional way
73
+ // const submitButton = document.getElementById('pip-submit-button');
74
+ // const messageDiv = document.getElementById('pip-message');
75
+ // submitButton.disabled = true; // Disable the submit button
76
+ // messageDiv.textContent = "Downloading dependencies..."; // Show downloading message
77
+ // messageDiv.style.display = 'block';
78
+
79
+ // const formData = new FormData(event.target);
80
+ // try {
81
+ // const response = await fetch('/download-dependencies', {
82
+ // method: 'POST',
83
+ // body: formData
84
+ // });
85
+ // if (response.ok) {
86
+ // const blob = await response.blob();
87
+ // const url = window.URL.createObjectURL(blob);
88
+ // const a = document.createElement('a');
89
+ // a.style.display = 'none';
90
+ // a.href = url;
91
+ // a.download = 'dependencies.tar.gz';
92
+ // document.body.appendChild(a);
93
+ // a.click();
94
+ // window.URL.revokeObjectURL(url);
95
+ // messageDiv.textContent = "Download complete!"; // Show download complete message
96
+ // } else {
97
+ // throw new Error('Download failed');
98
+ // }
99
+ // } catch (error) {
100
+ // console.error('Error:', error);
101
+ // messageDiv.innerHTML = "Error downloading dependencies.<br>Please try again."; // Show error message in two lines
102
+ // } finally {
103
+ // submitButton.disabled = false; // Re-enable the submit button after download
104
+ // }
105
+ // }
106
+
107
  async function handlePipFormSubmit(event) {
108
  event.preventDefault(); // Prevent the form from submitting the traditional way
109
  const submitButton = document.getElementById('pip-submit-button');
110
  const messageDiv = document.getElementById('pip-message');
111
+ const progressBar = document.getElementById('progress-bar');
112
+ const progressContainer = document.getElementById('progress-container');
113
  submitButton.disabled = true; // Disable the submit button
114
  messageDiv.textContent = "Downloading dependencies..."; // Show downloading message
115
  messageDiv.style.display = 'block';
116
+ progressContainer.style.display = 'block';
117
+ progressBar.style.width = '0%'; // Reset progress bar
118
 
119
  const formData = new FormData(event.target);
120
+
121
  try {
122
  const response = await fetch('/download-dependencies', {
123
  method: 'POST',
124
  body: formData
125
  });
126
  if (response.ok) {
127
+ const reader = response.body.getReader();
128
+ const contentLength = response.headers.get('content-length');
129
+ const total = parseInt(contentLength, 10);
130
+ let loaded = 0;
131
+
132
+ const stream = new ReadableStream({
133
+ start(controller) {
134
+ function push() {
135
+ reader.read().then(({ done, value }) => {
136
+ if (done) {
137
+ controller.close();
138
+ updateProgress(progressBar, messageDiv, total, total);
139
+ return;
140
+ }
141
+ loaded += value.length;
142
+ updateProgress(progressBar, messageDiv, loaded, total);
143
+ controller.enqueue(value);
144
+ push();
145
+ }).catch(error => {
146
+ console.error('Error:', error);
147
+ messageDiv.textContent = "Error downloading dependencies.";
148
+ });
149
+ }
150
+ push();
151
+ }
152
+ });
153
+
154
+ const blob = await new Response(stream).blob();
155
  const url = window.URL.createObjectURL(blob);
156
  const a = document.createElement('a');
157
  a.style.display = 'none';
 
160
  document.body.appendChild(a);
161
  a.click();
162
  window.URL.revokeObjectURL(url);
 
163
  } else {
164
+ const errorMessage = await response.text();
165
+ throw new Error(errorMessage || 'Download failed');
166
  }
167
  } catch (error) {
168
  console.error('Error:', error);
169
+ messageDiv.innerHTML = "Error downloading dependencies.<br>Please try again.";
170
  } finally {
171
  submitButton.disabled = false; // Re-enable the submit button after download
172
  }