Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<script>
import { get } from 'svelte/store';
import { openPopup } from './popup';
import { BASE_URL } from './constants';
import { queueList, updated, activeTab, clearQueueForTab } from './stores';
import Loading from './Loading.svelte';
import LoadingEllipsis from './Project/LoadingEllipsis.svelte';
let loading = false;
const { Drupal } = window;
const currentQueueList = get(queueList)[$activeTab] || [];
const queueLength = Object.keys(currentQueueList).length;
const projectsToActivate = [];
const projectsToDownloadAndActivate = [];
const handleError = async (errorResponse) => {
// If an error occurred, set loading to false so the UI no longer reports
// the download/install as in progress.
loading = false;
// The error can take on many shapes, so it should be normalized.
let err = '';
if (typeof errorResponse === 'string') {
err = errorResponse;
} else {
err = await errorResponse.text();
}
try {
// See if the error string can be parsed as JSON. If not, the block
// is exited before the `err` string is overwritten.
const parsed = JSON.parse(err);
err = parsed;
} catch (error) {
// The catch behavior is established before the try block.
}
const errorMessage = err.message || err;
// The popup function expects an element, so a div containing the error
// message is created here for it to display in a modal.
const div = document.createElement('div');

utkarsh_33
committed
const currentUrl =
window.location.pathname + window.location.search + window.location.hash;
if (err.unlock_url) {

utkarsh_33
committed
try {
const unlockUrl = new URL(err.unlock_url, BASE_URL);
unlockUrl.searchParams.set('destination', currentUrl);

utkarsh_33
committed
const updatedMessage = errorMessage.replace(
'[+ unlock link]',
`<a href="${
unlockUrl.pathname + unlockUrl.search
}" id="unlock-link">${Drupal.t('unlock link')}</a>`,
);
div.innerHTML += `<p>${updatedMessage}</p>`;

utkarsh_33
committed
} catch (urlError) {
div.innerHTML += `<p>${errorMessage}</p>`;
}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
} else {
div.innerHTML += `<p>${errorMessage}</p>`;
}
openPopup(div, {
title: 'Error while installing package(s)',
});
};
/**
* Actives already-downloaded projects.
*
* @param {string[]} projectIds
* An array of project IDs to activate.
*
* @return {Promise<void>}
* A promise that resolves when the project is activated.
*/
async function activateProject(projectIds) {
const url = `${BASE_URL}admin/modules/project_browser/activate`;
const installResponse = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(projectIds),
});
if (!installResponse.ok) {
await handleError(installResponse);
loading = false;
return;
}
let responseContent = await installResponse.text();
try {
const parsedJson = JSON.parse(responseContent);
responseContent = parsedJson;
} catch (err) {
await handleError(installResponse);
}
}
/**
* Performs the requests necessary to download and activate project via Package Manager.
*
* @param {string[]} projectIds
* An array of project IDs to download and activate.
*
* @return {Promise<void>}
* Returns a promise that resolves once the download and activation process is complete.
*/
async function doRequests(projectIds) {
const beginInstallUrl = `${BASE_URL}admin/modules/project_browser/install-begin`;
const beginInstallResponse = await fetch(beginInstallUrl);
if (!beginInstallResponse.ok) {
await handleError(beginInstallResponse);
} else {
const beginInstallData = await beginInstallResponse.json();
const stageId = beginInstallData.stage_id;
// The process of adding a module is separated into four stages, each
// with their own endpoint. When one stage completes, the next one is
// requested.
const installSteps = [
{
url: `${BASE_URL}admin/modules/project_browser/install-require/${stageId}`,
method: 'POST',
},
{

Narendra Singh Rathore
committed
url: `${BASE_URL}admin/modules/project_browser/install-apply/${stageId}`,
method: 'GET',
},
{

Narendra Singh Rathore
committed
url: `${BASE_URL}admin/modules/project_browser/install-post_apply/${stageId}`,
method: 'GET',
},
{

Narendra Singh Rathore
committed
url: `${BASE_URL}admin/modules/project_browser/install-destroy/${stageId}`,
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
method: 'GET',
},
];
// eslint-disable-next-line no-restricted-syntax,guard-for-in
for (const step of installSteps) {
const options = {
method: step.method,
};
// Additional options need to be added when the request method is POST.
// This is specifically required for the `install-require` step.
if (step.method === 'POST') {
options.headers = {
'Content-Type': 'application/json',
};
// Set the request body to include the project(s) id as an array.
options.body = JSON.stringify(projectsToDownloadAndActivate);
}
// eslint-disable-next-line no-await-in-loop
const stepResponse = await fetch(step.url, options);
if (!stepResponse.ok) {
// eslint-disable-next-line no-await-in-loop
const errorMessage = await stepResponse.text();
// eslint-disable-next-line no-console
console.warn(
`failed request to ${step.url}: ${errorMessage}`,
stepResponse,
);
// eslint-disable-next-line no-await-in-loop
await handleError(errorMessage);
return;
}
}
await activateProject(projectIds);
}
}
async function processQueue() {
// eslint-disable-next-line no-restricted-syntax,guard-for-in
for (const proj of currentQueueList) {
if (proj.status === 'absent') {
projectsToDownloadAndActivate.push(proj.id);
} else if (proj.status === 'present') {
projectsToActivate.push(proj.id);
}
}
loading = true;
document.body.style.pointerEvents = 'none';
if (projectsToActivate.length > 0) {
await activateProject(projectsToActivate);
}
if (projectsToDownloadAndActivate.length > 0) {
await doRequests(projectsToDownloadAndActivate);
}
loading = false;
document.body.style.pointerEvents = 'auto';
clearQueueForTab($activeTab);
// eslint-disable-next-line no-restricted-syntax,guard-for-in
for (const project of currentQueueList) {
project.status = 'active';
}
$updated = new Date().getTime();
}
</script>
<button
class="project__action_button project__action_button--fixed"
on:click={processQueue}
>
{#if loading}
<Loading />
<LoadingEllipsis
message={Drupal.formatPlural(
queueLength,
'Installing 1 project',
'Installing @count projects',
)}
/>
{:else}
{Drupal.t('Install selected projects')}
{/if}
</button>