Skip to content
Draft
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
3 changes: 2 additions & 1 deletion src/apps/content-editor/src/app/components/Editor/Editor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { memo, useCallback, useEffect, useMemo, useState } from "react";
import { useDispatch } from "react-redux";
import { useDispatch, useSelector } from "react-redux";
Copy link
Contributor

Choose a reason for hiding this comment

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

unused import

import { AppLink } from "shell/components/AppLink";
import { unescape } from "lodash";
import { Field } from "./Field";
Expand Down Expand Up @@ -423,6 +423,7 @@ export default memo(function Editor({
field.settings?.maxCharLimit ?? MaxLengths[field.datatype]
}
minLength={field.settings?.minCharLimit ?? 0}
itemZUID={item?.meta?.ZUID}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type FieldProps = {
errors: Error;
maxLength: number;
minLength: number;
itemZUID?: string;
};

export const Field = memo(
Expand All @@ -110,6 +111,7 @@ export const Field = memo(
maxLength,
minLength,
version,
itemZUID,
}: FieldProps) => {
const dispatch = useDispatch();
const { data: fields } = useGetContentModelFieldsQuery({
Expand Down Expand Up @@ -655,6 +657,9 @@ export const Field = memo(
relatedFieldZUID={relatedFieldZUID}
onChange={onChange}
fieldLabel={fieldData?.label}
langID={langID}
contentItemZUID={itemZUID}
contentVersion={version}
/>
</FieldShell>
);
Expand All @@ -671,6 +676,9 @@ export const Field = memo(
relatedFieldZUID={relatedFieldZUID}
onChange={onChange}
fieldLabel={fieldData?.label}
langID={langID}
contentItemZUID={itemZUID}
contentVersion={version}
/>
</FieldShell>
);
Expand Down
62 changes: 59 additions & 3 deletions src/shell/components/RelationalFieldBase/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ import {
} from "@mui/icons-material";
import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
import { useDispatch } from "react-redux";
import { useDispatch, useSelector } from "react-redux";

import { ActiveItem } from "./ActiveItem";
import { FieldSelectorDialog } from "./FieldSelectorDialog";
import {
useGetContentModelQuery,
useGetContentModelFieldsQuery,
} from "../../services/instance";
import { fetchItems } from "../../store/content";
import { fetchItem, fetchItems } from "../../store/content";
import { ActiveItemLoading } from "./ActiveItem/ActiveItemLoading";
import { CreateNewItemDialog } from "./CreateNewItemDialog";
import { useParams } from "../../hooks/useParams";
import { CreateContentItemDialogContext } from "../../contexts/CreateContentItemDialogProvider";
import { Language } from "shell/services/types";
import { AppState } from "shell/store/types";

type RelationalFieldBaseProps = {
name: string;
Expand All @@ -31,6 +33,9 @@ type RelationalFieldBaseProps = {
relatedFieldZUID: string;
onChange: (value: string, name: string) => void;
multiselect?: boolean;
langID?: number | null;
contentItemZUID?: string;
contentVersion?: number;
};
export const RelationalFieldBase = ({
name,
Expand All @@ -41,13 +46,19 @@ export const RelationalFieldBase = ({
relatedFieldZUID,
onChange,
multiselect,
langID = null,
contentItemZUID,
contentVersion,
}: RelationalFieldBaseProps) => {
const dispatch = useDispatch();
const [params] = useParams();
const [itemZUIDs, setItemZUIDs] = useState<string[]>(value?.split(",") || []);
const [showAll, setShowAll] = useState(false);
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement>(null);
const [replace, setReplace] = useState<string | null>(null);
const [isUpdatingLocaleValue, setIsUpdatingLocaleValue] =
useState<boolean>(false);
const languages = useSelector((state: AppState) => state.languages);
const [
initiatorZUID,
setInitiatorZUID,
Expand All @@ -67,6 +78,50 @@ export const RelationalFieldBase = ({
}
);

const updateItemLocale = useCallback(async () => {
//update locale if version <= 1
//item's default language is inherited
if (contentVersion <= 1 || !contentVersion) {
const valueArray = value?.split(",") || [];
const langCode =
languages?.find((lang: Language) => lang?.ID === langID)?.code ||
"en-US";

setIsUpdatingLocaleValue(true);
const langItems = await Promise.all(
valueArray
?.map((itemVal) =>
dispatch(fetchItem(relatedModelZUID, itemVal))
//@ts-ignore
.then((resItem) => {
const itemData = resItem?.data;
return itemData?.siblings?.[langCode] || itemVal;
})
)
?.filter(Boolean)
);
//trigger onchange event to update state
onChange(langItems?.join(","), name);
dispatch({
type: "UNMARK_ITEMS_DIRTY",
items: [contentItemZUID],
});
setIsUpdatingLocaleValue(false);
}
}, [
contentVersion,
value,
contentItemZUID,
languages,
langID,
relatedModelZUID,
name,
]);

useEffect(() => {
updateItemLocale();
Copy link
Contributor

@agalin920 agalin920 Oct 29, 2025

Choose a reason for hiding this comment

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

This seems like a very hacky solution to this. If im reading this correctly you are changing the content's state and then manually unmarking it as dirty on mount? Are we also changing values? If so why? We should not be doing any kind of content change that is not explicit by the user

A more holistic solution is when switching locale the value of the initial state should reflect the locale.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The content of all locale items will be identical, as they inherit the same content upon creation.

I’m not sure if we have control over that behavior on the frontend.

}, []);

useEffect(() => {
// Ensures that the values always synced
setItemZUIDs(value?.split(",") || []);
Expand Down Expand Up @@ -111,7 +166,8 @@ export const RelationalFieldBase = ({
};

const isRenderedAsDialog = params.get("isDialog") === "true";
const isLoading = isLoadingModelData || isLoadingModelFields;
const isLoading =
isLoadingModelData || isLoadingModelFields || isUpdatingLocaleValue;

return (
<Box component="section">
Expand Down
Loading