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 all commits
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
56 changes: 41 additions & 15 deletions libs/blocks/locui/utils/miloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} 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 @@ let pollingInterval = null;
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,24 @@ function handleProjectStatusDetail(detail) {
languages.value = [...languages.value.map((lang) => ({ ...lang, ...detail[lang.code] }))];
}

const cancelPolling = () => {
if (pollingInterval) {
clearInterval(pollingInterval);
pollingInterval = null;
polling.value = false;
}
};

function showAuthError(operation) {
cancelPolling();
setStatus(
'service',
'error',
`You do not have access to ${operation}.`,
'Please refresh page, login to sidekick and retry.',
);
}

export async function getProjectStatus() {
try {
const url = await getMilocUrl();
Expand Down Expand Up @@ -109,20 +129,25 @@ export async function startSync() {
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 } };
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 +157,7 @@ export async function cancelProject() {
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 } };
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 +166,11 @@ export async function cancelProject() {
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,16 +184,20 @@ export async function rolloutLang(
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 } };
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 opts = { method: 'POST', headers: { 'User-Token': accessToken.value }, body };
const resp = await fetch(`${url}create-project`, opts);
if (resp.status === 201) {
setExcelStatus('Project Created', '');
Expand All @@ -179,17 +212,10 @@ export async function createProject() {
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;
}

const cancelPolling = () => {
if (pollingInterval) {
clearInterval(pollingInterval);
pollingInterval = null;
polling.value = false;
}
};

export async function getServiceUpdates() {
const url = await getMilocUrl();
let count = 1;
Expand Down
Loading