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

[LOC] MWPW-164734 - single rollout for loc #3428

Merged
Show file tree
Hide file tree
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
65 changes: 38 additions & 27 deletions libs/blocks/locui-create/input-locale/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
setLocale,
updateDraftProject,
} from '../store.js';
import { ENG_LANG_CODE, PROJECT_TYPES } from '../utils/constant.js';
import { ENG_LANG_CODE, PROJECT_ACTION, PROJECT_TYPES } from '../utils/constant.js';

function initialLanguageList() {
if (
Expand All @@ -22,8 +22,8 @@

function initialRegions() {
if (project.value.type === PROJECT_TYPES.translation) {
const englishLocale = locales.value.filter((locItem) => locItem.languagecode === ENG_LANG_CODE);
const { livecopies = '' } = englishLocale[0] || {};
const englishLocale = locales.value.find((locItem) => locItem.languagecode === ENG_LANG_CODE);
const { livecopies = '' } = englishLocale || {};
return localeRegion.value.reduce((acc, curr) => {
const { key, value } = curr;
const valueList = value.split(',');
Expand All @@ -45,6 +45,41 @@
return localeRegion.value;
}

function prefillActionAndWorkflow(languages) {
const storedLanguages = project.value?.languages ?? [];
if (storedLanguages.length < 1) {
return languages.map((lang) => ({
...lang,
action: project.value.type === PROJECT_TYPES.translation
? PROJECT_ACTION.translate : PROJECT_ACTION.rollout,
workflow: '',
}));
}

const languageByCode = storedLanguages.reduce((acc, curr) => {
const { langCode } = curr;
acc[langCode] = curr;
return acc;
}, {});

const prefilledLanguages = languages.map((lang) => {
const { langCode } = lang;
const { action, workflow = '' } = languageByCode[langCode] || {};
const prefillLanguage = {
...lang,
action,
workflow,
};
if (!action) {
prefillLanguage.action = project.value.type === PROJECT_TYPES.translation
? PROJECT_ACTION.translate : PROJECT_ACTION.rollout;
}
return prefillLanguage;
});

return prefilledLanguages;
}

export default function useInputLocale() {
const [selectedRegion, setSelectedRegion] = useState(
locSelected.value?.selectedRegion || {},
Expand Down Expand Up @@ -175,34 +210,10 @@
...prevState,
...updateRegionStates(selectedLocale),
}));
}, [selectedLocale]);

Check warning on line 213 in libs/blocks/locui-create/input-locale/index.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 React Hook useEffect has a missing dependency: 'updateRegionStates'. Either include it or remove the dependency array. Raw Output: {"ruleId":"react-hooks/exhaustive-deps","severity":1,"message":"React Hook useEffect has a missing dependency: 'updateRegionStates'. Either include it or remove the dependency array.","line":213,"column":6,"nodeType":"ArrayExpression","endLine":213,"endColumn":22,"suggestions":[{"desc":"Update the dependencies array to be: [selectedLocale, updateRegionStates]","fix":{"range":[6334,6350],"text":"[selectedLocale, updateRegionStates]"}}]}

const errorPresent = () => Object.keys(activeLocales).length > 0;

const prefillActionAndWorkflow = (languages) => {
const storedLanguages = project.value?.languages ?? [];
if (storedLanguages.length < 1) {
return languages.map((lang) => ({ ...lang, action: project.value.type === PROJECT_TYPES.translation ? 'Translate' : 'Rollout', workflow: '' }));
}
let iteratorIndex = 0;
const prefilledLanguages = [];

while (iteratorIndex < languages.length) {
const { action, workflow } = storedLanguages[iteratorIndex] ?? {};
const prefillLanguage = {
...languages[iteratorIndex],
action,
workflow: workflow || '',
};
if (!action) {
prefillLanguage.action = project.value.type === PROJECT_TYPES.translation ? 'Translate' : 'Rollout';
}
prefilledLanguages.push(prefillLanguage);
iteratorIndex += 1;
}
return prefilledLanguages;
};

const handleNext = async () => {
if (!errorPresent()) return;
const sortedLanguages = transformActiveLocales()
Expand Down
38 changes: 24 additions & 14 deletions libs/blocks/locui-create/input-locale/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import useInputLocale from './index.js';
import StepControls from '../components/stepControls.js';
import { PROJECT_TYPES } from '../utils/constant.js';
import Toast from '../components/toast.js';
import { initByParams } from '../store.js';

export default function InputLocales() {
const {
Expand All @@ -24,23 +25,25 @@ export default function InputLocales() {
setApiError,
} = useInputLocale();

const RenderRegion = () => html`
const RenderRegion = () => {
if (!initByParams.value?.languages) {
return (html`
<h5 class="section-header">Quick Select for Language/Locale</h5>
<div class="region-grid">
<div class="region-buttons">
${localeRegionList.map(
(region) => html`
(region) => html`
<button
key=${region.key}
class="region-button ${selectedRegion[region.key]
? 'active'
: ''}"
? 'active'
: ''}"
onClick=${() => toggleRegion(region)}
>
${region.key}
</button>
`,
)}
)}
</div>
<div class="additional-cta">
<button class="reset-button" onClick=${selectAll}>
Expand All @@ -51,31 +54,38 @@ export default function InputLocales() {
</button>
</div>
</div>
`;
`);
} return null;
};

const RenderLanguage = () => html`
const RenderLanguage = () => {
if (!initByParams.value?.languages) {
return (html`
<div class="language-grid">
<h5 class="section-header">Select the Language(s)</h5>
<div class="language-buttons">
${languagesList.map(
(language) => language.livecopies.length > 0
(language) => language.livecopies.length > 0
&& html`
<button
key=${language.languagecode}
class="language-button ${language.livecopies
.split(',')
.some((locale) => selectedLocale.includes(locale))
? 'active'
: ''}"
.split(',')
.some((locale) => selectedLocale.includes(locale))
? 'active'
: ''}"
onClick=${() => selectLanguage(language)}
>
${language.language}
</button>
`,
)}
)}
</div>
</div>
`;
`);
}
return null;
};

const RenderLocales = () => {
const groupedLocales = selectedLocale.reduce((acc, locale) => {
Expand Down
6 changes: 4 additions & 2 deletions libs/blocks/locui-create/input-urls/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import FragmentsSection from '../fragments/view.js';
import {
authenticated,
createDraftProject,
initByParams,
nextStep,
project,
projectCreated,
Expand Down Expand Up @@ -171,7 +172,7 @@ export default function InputUrls() {
setEditBehavior(project.value?.editBehavior || '');
setUrlsStr(project.value?.urls?.join('\n'));
setFragmentsEnabled(project.value?.fragments?.length > 0);
setFragments(project.value?.fragments);
setFragments(project.value?.fragments ?? []);
if (
project.value?.fragments?.length > 0
&& project.value?.urls.length > 0
Expand Down Expand Up @@ -209,7 +210,7 @@ export default function InputUrls() {
<span>- ${PROJECT_TYPE_LABELS[type]}</span>
</div>
<div class="locui-form-body">
${!projectCreated.value && html`
${(!projectCreated.value && !initByParams.value?.type) && html`
<div class="segment-ctrl pb-12">
${[PROJECT_TYPES.translation, PROJECT_TYPES.rollout].map((pType) => html`
<div
Expand Down Expand Up @@ -287,6 +288,7 @@ export default function InputUrls() {
onInput=${handleUrlsChange}
onBlur=${handleUrlsBlur}
placeholder=${`Enter the full URL. E.g, ${origin}/drafts/localization/projects/raga/image-test-one`}
disabled=${initByParams.value?.urls}
/>
${errors.urlsStr
&& html`<div class="form-field-error">${errors.urlsStr}</div>`}
Expand Down
7 changes: 7 additions & 0 deletions libs/blocks/locui-create/locui-create.css
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ body {
border-color: #d73220;
}

.form-field-textarea:disabled {
background-color: #fff;
border-color: #dadada;
color: #c6c6c6;
cursor: not-allowed;
}

.form-field-switch {
display: none;
}
Expand Down
16 changes: 12 additions & 4 deletions libs/blocks/locui-create/locui-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
getUserToken,
loading,
project,
setInitByParams,
setProject,
} from './store.js';
import StepTracker from './components/stepTracker.js';
import InputActions from './input-actions/view.js';
import Header from '../milostudio-header/milostudio-header.js';
import Sidenav from '../milostudio-sidenav/sidenav.js';
import { getConfig, loadStyle } from '../../utils/utils.js';
import { getEnvQueryParam, setSelectedLocalesAndRegions } from './utils/utils.js';
import { getEnvQueryParam, getProjectByParams, setSelectedLocalesAndRegions } from './utils/utils.js';
import Toast from './components/toast.js';

function Create() {
Expand All @@ -24,13 +26,14 @@ function Create() {
useEffect(() => {
const setup = async () => {
try {
await fetchLocaleDetails();
await getUserToken();
await fetchLocaleDetails();
env.value = getEnvQueryParam();

/* Fetch draft project if project key is found in url params */
const searchParams = new URLSearchParams(window.location.search);
const projectKey = searchParams.get('projectKey');
const projectInitByUrl = getProjectByParams(searchParams);

/* Fetch draft project if project key is found in url params */
if (projectKey) {
const error = await fetchDraftProject(projectKey);
if (error) {
Expand All @@ -43,6 +46,11 @@ function Create() {
setSelectedLocalesAndRegions();
}
}
if (projectInitByUrl && !projectKey) {
setProject(projectInitByUrl);
setInitByParams(projectInitByUrl);
setSelectedLocalesAndRegions();
}
} catch (error) {
// console.error('Error fetching locale details:', error);
}
Expand Down
10 changes: 10 additions & 0 deletions libs/blocks/locui-create/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
export const localeRegion = signal([]);
export const locSelected = signal(null);
export const projectType = signal('rollout');
export const initByParams = signal(null);
export const env = signal('dev');

export function nextStep() {
Expand All @@ -38,6 +39,13 @@
};
}

export function setInitByParams(params) {
initByParams.value = {
...initByParams.value,
...params,
};
}

export function setLocale(_locale) {
locSelected.value = {
...locSelected.value,
Expand All @@ -62,7 +70,7 @@
userToken = accessToken.value;
authenticated.value = true;
} catch {
console.error('Sharepoint login failed. Unable to get User-Token!');

Check warning on line 73 in libs/blocks/locui-create/store.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 Unexpected console statement. Raw Output: {"ruleId":"no-console","severity":1,"message":"Unexpected console statement.","line":73,"column":5,"nodeType":"MemberExpression","messageId":"unexpected","endLine":73,"endColumn":18}
authenticated.value = false;
}
loading.value = false;
Expand All @@ -71,6 +79,7 @@

export async function fetchLocaleDetails() {
try {
loading.value = true;
const tenantName = getTenantName();
if (!tenantName) {
// console.warn('Tenant name is missing, skipping fetchLocaleDetails.');
Expand All @@ -96,9 +105,10 @@
locales.value = processedLocales.filter((locItem) => locItem.workflow !== TRANSCREATION_WORKFLOW && locItem.livecopies !== '');
localeRegion.value = processedLocaleRegion;
} catch (error) {
console.error('Error during fetchLocaleDetails:', error.message);

Check warning on line 108 in libs/blocks/locui-create/store.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 Unexpected console statement. Raw Output: {"ruleId":"no-console","severity":1,"message":"Unexpected console statement.","line":108,"column":5,"nodeType":"MemberExpression","messageId":"unexpected","endLine":108,"endColumn":18}
throw error;
}
loading.value = false;
}

export async function createDraftProject() {
Expand Down
6 changes: 6 additions & 0 deletions libs/blocks/locui-create/utils/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export const PROJECT_TYPE_LABELS = {
rollout: 'Rollout',
};

export const PROJECT_ACTION = {
translate: 'Translate',
rollout: 'Rollout',
english_copy: 'English Copy',
};

export const TRANSLATE_ONLY_LANGS = ['en-GB'];
export const ENG_LANG_CODE = 'en';
export const TRANSCREATION_WORKFLOW = 'Transcreation';
33 changes: 33 additions & 0 deletions libs/blocks/locui-create/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import getServiceConfig from '../../../utils/service-config.js';
import { origin } from '../../locui/utils/franklin.js';
import { getInitialName } from '../input-urls/index.js';
import { env, locSelected, locales as stLocales, project as stProject } from '../store.js';

export function getTenantName() {
Expand Down Expand Up @@ -104,3 +105,35 @@ export function setSelectedLocalesAndRegions() {
selectedLocale.sort((a, b) => a.localeCompare(b));
locSelected.value = { selectedLocale, activeLocales };
}

export function getLanguageDetails(lang) {
const langDetails = stLocales.value?.find(({ languagecode }) => languagecode === lang) ?? {};
return [{
action: 'Rollout',
langCode: langDetails.languagecode,
language: langDetails.language,
locales: langDetails.livecopies.split(','),
workflow: '',
}];
}

export function getProjectByParams(searchParams) {
const encodedUrls = searchParams.get('encodedUrls');
const type = searchParams.get('type');
const decodedUrls = encodedUrls?.split(',')?.map((url) => decodeURI(url));
const language = searchParams.get('language');

const projectInfo = {};
if (type) {
projectInfo.type = type;
projectInfo.name = getInitialName(type);
}
if (decodedUrls?.length > 0) {
projectInfo.urls = decodedUrls;
}
if (language) {
projectInfo.languages = getLanguageDetails(language);
}

return Object.keys(projectInfo).length > 0 ? projectInfo : null;
}
Loading