Answer the question
In order to leave comments, you need to log in
State machine isolation of mutations or how to properly manage the state of complex modules with deep objects?
We have state = storage
getter = only return values
action = execute and contain all the logic
mutation = only mutates the
state
state
export interface IAbstractFormWithLocalizedFields extends IAbstractForm,
IFormWithLocalizedFieldsGetters,
IFormWithLocalizedFieldsActions {
_fields?: {
_localizedFields?: Array<ILocalizedFields>,
}
_violations?: {
_localizedFields?: Array<ILocalizedFields>,
}
}
let locales = [
{
isDefault: true,
isSelected: true,
locale: {
language: {
code: {
alpha2: 'en',
alpha3: 'eng',
},
nativeName: 'English',
commonName: 'English',
},
country: {
code: {
alpha2: 'ua',
alpha3: 'ukr',
},
nativeName: 'Украина',
commonName: 'Ukraine',
},
},
fields: [
{
fieldName: 'title',
values: [
{localizedValue: 'Some titles 1', isDefault: true},
{localizedValue: 'Some titles 2', isDefault: false},
{localizedValue: 'Some titles 3', isDefault: false},
],
},
{
fieldName: 'description',
values: [
{localizedValue: 'Some description 1', isDefault: true},
],
},
],
},
{
isDefault: false,
isSelected: false,
locale: {
language: {
code: {
alpha2: 'de',
alpha3: 'deu',
},
nativeName: 'Deutch',
commonName: 'Deutch',
},
country: {
code: {
alpha2: 'ge',
alpha3: 'ger',
},
nativeName: 'Germany',
commonName: 'Germany',
},
},
fields: [
{
fieldName: 'title',
values: [
{localizedValue: 'Some titles 11', isDefault: true},
{localizedValue: 'Some titles 22', isDefault: false},
{localizedValue: 'Some titles 33', isDefault: false},
],
},
{
fieldName: 'description',
values: [
{localizedValue: 'Some description 11', isDefault: true},
],
},
],
},
]
;
export interface ILocalizedFields {
locale: ILocale,
isDefault: boolean,
isSelected: boolean,
fields: Array<ILocalizedField>,
}
export interface ILocalizedField {
fieldName: string,
values: Array<ILocalizedValue>,
}
export interface ILocalizedValue {
localizedValue: string,
isDefault: boolean,
}
const local = {
/** @override **/
_fields: {
_localizedFields: locales,
},
};
export default (): IStrAny => merge({}, common(), local) as IStrAny;
[SET_ACTIVE_LOCALE_ISO](state: any, activateLocaleWithISO: ISOAlpha2Type): void {
let fields: Array<ILocalizedFields> = state._fields._localizedFields;
// can moved to getter function
const disableByIndex: number = findIndex(
state._fields._localizedFields,
(field: ILocalizedFields): boolean => field.isSelected,
);
if (-1 < disableByIndex) {
let disabledItem: ILocalizedFields = fields[disableByIndex];
disabledItem.isSelected = false;
fields.splice(disableByIndex, 1, disabledItem);
}
// can moved to getter function
const enableByIndex: number = findIndex(
state._fields._localizedFields,
(field: ILocalizedFields): boolean =>
formatLocaleToISOCode(field.locale).toLowerCase() === activateLocaleWithISO,
);
if (-1 < enableByIndex) {
let enabledItem: ILocalizedFields = fields[enableByIndex];
enabledItem.isSelected = true;
fields.splice(enableByIndex, 1, enabledItem);
}
},
setActiveLocaleISO({state, commit, getters}, isoCode: ISOAlpha2Type): void {
if (isoCode !== getters.activeLocalizedFieldsLocaleISO2) {
commit(SET_ACTIVE_LOCALE_ISO, isoCode.toLowerCase());
}
},
localeLocalizedFieldsIndexByLocaleISO2: (state: any, getters: any) => (localeISO_2: ISOAlpha2Type): ILocalizedFields | undefined => {
return findIndex(
getters.localizedFields as Array<ILocalizedFields>,
(field: ILocalizedFields): boolean => localeISO_2 === formatLocaleToISOCode(field.locale).toLowerCase(),
);
},
locali
licalizedFieldsSelectedPath (state : any, paramsForDeepObjSearch: {localeIso2 : string, fieldName: string} ) : string {
return '_fields._localizedFields.2.isSelected' // get path we want to update in deep object
}
activateLocalizedFieldsTab (getters: any, dispatch: any) : void {
dispatch('mutationName', {mutationPath: getters.licalizedFieldsSelectedPath, value: true}); // pass path and value
}
setActiveLocalizedFields (state : any, update: {mutationPath: string, value: any}) : void {
state[update.mutationPath] = update.value; // state mutated
}
return stateDeepMapStructure // {level_1_path_1 : '_fields', level_2_path_1: '_localizedFields'}
{
{
localeCountryISO2Code : 'ru',
localeLanguageISO2Code : 'ru',
fieldName: 'title',
fieldValue: 'Крутой тайтл для крутого поста',
isSelected: true,
isDefault: true,
},
{
localeCountryISO2Code : 'en',
localeLanguageISO2Code : 'en',
fieldName: 'title',
fieldValue: 'This is cool title for this cool post',
isSelected: false,
isDefault: false,
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question