-
Notifications
You must be signed in to change notification settings - Fork 5
[#3833]: Content - Item with relational field not reflecting correct locale when switch to other language version #3871
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
base: dev
Are you sure you want to change the base?
Changes from all commits
dd6ee81
c46694a
f4b94bd
c1a3553
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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, | ||
|
|
@@ -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, | ||
|
|
@@ -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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(",") || []); | ||
|
|
@@ -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"> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused import