diff --git a/libs/blocks/locui-create/input-locale/index.js b/libs/blocks/locui-create/input-locale/index.js index 38abc2e664..5383486877 100644 --- a/libs/blocks/locui-create/input-locale/index.js +++ b/libs/blocks/locui-create/input-locale/index.js @@ -10,7 +10,7 @@ import { 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 ( @@ -22,8 +22,8 @@ function initialLanguageList() { 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(','); @@ -45,6 +45,41 @@ function initialRegions() { 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 || {}, @@ -179,30 +214,6 @@ export default function useInputLocale() { 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() diff --git a/libs/blocks/locui-create/input-locale/view.js b/libs/blocks/locui-create/input-locale/view.js index 8faca0b0a5..4a3e9839df 100644 --- a/libs/blocks/locui-create/input-locale/view.js +++ b/libs/blocks/locui-create/input-locale/view.js @@ -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 { @@ -24,23 +25,25 @@ export default function InputLocales() { setApiError, } = useInputLocale(); - const RenderRegion = () => html` + const RenderRegion = () => { + if (!initByParams.value?.languages) { + return (html`