Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-156265 - [LocUI] Pass user information to loc service calls #2759

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions libs/blocks/locui/utils/miloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
} from './state.js';
import { getItemId } from '../../../tools/sharepoint/shared.js';
import updateExcelTable from '../../../tools/sharepoint/excel.js';
import { accessToken } from '../../../tools/sharepoint/state.js';
import { origin, preview } from './franklin.js';
import { setExcelStatus, setStatus } from './status.js';
import getServiceConfig from '../../../utils/service-config.js';
Expand All @@ -24,6 +25,7 @@
const INTERVAL = 3000;
const MAX_COUNT = 1200; // 3000 x 1200 = 3600000s = 1 hour
const ROLLOUT_ALL_AVAILABLE = ['completed', 'translated'];
const UNAUTHORIZED = 401;

let waiting = false;

Expand All @@ -40,6 +42,15 @@
languages.value = [...languages.value.map((lang) => ({ ...lang, ...detail[lang.code] }))];
}

function showAuthError(operation) {
cancelPolling();

Check failure on line 46 in libs/blocks/locui/utils/miloc.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 'cancelPolling' was used before it was defined. Raw Output: {"ruleId":"no-use-before-define","severity":2,"message":"'cancelPolling' was used before it was defined.","line":46,"column":3,"nodeType":"Identifier","messageId":"usedBeforeDefined","endLine":46,"endColumn":16}
setStatus(
'service',
'error',
`You donot have access to ${operation}.`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`You donot have access to ${operation}.`,
`You do not have access to ${operation}.`,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Sartxi updated as suggested

'Please refresh page, login to sidekick and retry.');

Check failure on line 51 in libs/blocks/locui/utils/miloc.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 Expected newline before ')'. Raw Output: {"ruleId":"function-paren-newline","severity":2,"message":"Expected newline before ')'.","line":51,"column":56,"nodeType":"Punctuator","messageId":"expectedBefore","endLine":51,"endColumn":57,"fix":{"range":[1475,1475],"text":"\n"}}
}

export async function getProjectStatus() {
try {
const url = await getMilocUrl();
Expand Down Expand Up @@ -109,20 +120,25 @@
setStatus('service', 'info', 'Syncing documents to Langstore.');
const url = await getMilocUrl();
setExcelStatus('Sync to langstore/en', '');
const opts = { method: 'POST' };
const opts = { method: 'POST', headers: { 'User-Token': accessToken.value } };
const resp = await fetch(`${url}start-sync?project=${heading.value.projectId}`, opts);
if (resp.status === UNAUTHORIZED) showAuthError('start project');
return resp.status;
}

export async function startProject({ skipSync }) {
let url = await getMilocUrl();
setStatus('service', 'info', 'Starting project');
const opts = { method: 'POST' };
const opts = { method: 'POST', headers: { 'User-Token': accessToken.value } };

Check failure on line 132 in libs/blocks/locui/utils/miloc.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 Multiple spaces found before '}'. Raw Output: {"ruleId":"no-multi-spaces","severity":2,"message":"Multiple spaces found before '}'.","line":132,"column":78,"nodeType":"Punctuator","messageId":"multipleSpaces","endLine":132,"endColumn":80,"fix":{"range":[4118,4120],"text":" "}}
url = `${url}start-project?project=${heading.value.projectId}`;
if (skipSync) url = `${url}&skipsync=true`;
const resp = await fetch(url, opts);
if (resp.status === 201) setExcelStatus('Sent to localization service', '');
setStatus('service');
if (resp.status === UNAUTHORIZED) {
showAuthError('start project');
} else {
setStatus('service');
}
return resp.status;
}

Expand All @@ -132,7 +148,7 @@
allowCancelProject.value = false;
let url = await getMilocUrl();
setStatus('service', 'info', 'Cancelling project');
const opts = { method: 'POST' };
const opts = { method: 'POST', headers: { 'User-Token': accessToken.value } };

Check failure on line 151 in libs/blocks/locui/utils/miloc.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 Multiple spaces found before '}'. Raw Output: {"ruleId":"no-multi-spaces","severity":2,"message":"Multiple spaces found before '}'.","line":151,"column":78,"nodeType":"Punctuator","messageId":"multipleSpaces","endLine":151,"endColumn":80,"fix":{"range":[4804,4806],"text":" "}}
url = `${url}cancel-project?project=${heading.value.projectId}`;
const resp = await fetch(url, opts);
if (resp.status === 200) setExcelStatus('Project cancelled', '');
Expand All @@ -141,7 +157,11 @@
setStatus('service', 'error', 'Cancelling project', json.error);
return resp.status;
}
setStatus('service', 'info', 'Successfully Cancelled Project', null, 5000);
if (resp.status === UNAUTHORIZED) {
showAuthError('cancel project');
} else {
setStatus('service', 'info', 'Successfully Cancelled Project', null, 5000);
}
return resp.status;
}

Expand All @@ -155,17 +175,21 @@
if (ep === 'start-rollout') { statNotes = `${statNotes} - Reroll: ${reroll ? 'yes' : 'no'}`; }
setExcelStatus(statAction, statNotes);
const url = await getMilocUrl();
const opts = { method: 'POST' };
const opts = { method: 'POST', headers: { 'User-Token': accessToken.value } };

Check failure on line 178 in libs/blocks/locui/utils/miloc.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 Multiple spaces found before '}'. Raw Output: {"ruleId":"no-multi-spaces","severity":2,"message":"Multiple spaces found before '}'.","line":178,"column":78,"nodeType":"Punctuator","messageId":"multipleSpaces","endLine":178,"endColumn":80,"fix":{"range":[5761,5763],"text":" "}}
const resp = await fetch(`${url}${ep}?project=${heading.value.projectId}&languageCode=${languageCode}&reroll=${reroll}`, opts);
if (resp.status === UNAUTHORIZED) {
showAuthError('rollout');
return {};
}
return resp.json();
}

export async function createProject() {
const url = await getMilocUrl();
setStatus('service', 'info', 'Creating new project.');
const body = `${origin}${heading.value.path}.json`;
const opts = { method: 'POST', body };
const resp = await fetch(`${url}create-project`, opts);
const opts = { method: 'POST', headers: { 'User-Token': accessToken.value }, body };
const resp = await fetch(`${url}create-project?locui=${encodeURIComponent(window.location.href)}`, opts);
if (resp.status === 201) {
setExcelStatus('Project Created', '');
canRefresh.value = false;
Expand All @@ -179,6 +203,7 @@
const json = await resp.json();
setStatus('service', 'error', 'Creating project', json.errors && json.errors?.length > 0 ? json.errors : json.error);
}
if (resp.status === UNAUTHORIZED) showAuthError('create project');
return resp.status;
}

Expand Down
Loading