|
1
|
import { keepPreviousData } from "@tanstack/react-query" |
|
2
|
import { useEffect } from "react" |
|
3
|
import { useNavigate, useSearchParams } from "react-router-dom" |
|
4
|
import { RouteFocusModal } from "../../../components/modals" |
|
5
|
import { |
|
6
|
useReferenceTranslations, |
|
7
|
useStore, |
|
8
|
useTranslationSettings, |
|
9
|
} from "../../../hooks/api" |
|
10
|
import { useFeatureFlag } from "../../../providers/feature-flag-provider" |
|
11
|
import { TranslationsEditForm } from "./components/translations-edit-form" |
|
12
|
|
|
13
|
export const TranslationsEdit = () => { |
|
14
|
const isTranslationsEnabled = useFeatureFlag("translation") |
|
15
|
const navigate = useNavigate() |
|
16
|
const [searchParams] = useSearchParams() |
|
17
|
const reference = searchParams.get("reference") |
|
18
|
const referenceIdParam = searchParams.getAll("reference_id") |
|
19
|
|
|
20
|
useEffect(() => { |
|
21
|
if (!reference || !isTranslationsEnabled) { |
|
22
|
navigate(-1) |
|
23
|
return |
|
24
|
} |
|
25
|
}, [reference, navigate, isTranslationsEnabled]) |
|
26
|
|
|
27
|
const { |
|
28
|
translation_settings, |
|
29
|
isPending: isTranslationSettingsPending, |
|
30
|
isError: isTranslationSettingsError, |
|
31
|
error: translationSettingsError, |
|
32
|
} = useTranslationSettings({ entity_type: reference! }) |
|
33
|
|
|
34
|
const { |
|
35
|
translations, |
|
36
|
references, |
|
37
|
fetchNextPage, |
|
38
|
count, |
|
39
|
isFetchingNextPage, |
|
40
|
hasNextPage, |
|
41
|
isPending, |
|
42
|
isError, |
|
43
|
error, |
|
44
|
} = useReferenceTranslations(reference!, referenceIdParam, { |
|
45
|
enabled: !!reference, |
|
46
|
placeholderData: keepPreviousData, |
|
47
|
}) |
|
48
|
const { |
|
49
|
store, |
|
50
|
isPending: isStorePending, |
|
51
|
isError: isStoreError, |
|
52
|
error: storeError, |
|
53
|
} = useStore() |
|
54
|
|
|
55
|
const ready = |
|
56
|
!isPending && |
|
57
|
!!translations && |
|
58
|
!!translation_settings && |
|
59
|
!isTranslationSettingsPending && |
|
60
|
!!references && |
|
61
|
!isStorePending && |
|
62
|
!!store |
|
63
|
|
|
64
|
if (isError || isStoreError || isTranslationSettingsError) { |
|
65
|
throw error || storeError || translationSettingsError |
|
66
|
} |
|
67
|
|
|
68
|
return ( |
|
69
|
<RouteFocusModal prev={referenceIdParam.length ? -1 : ".."}> |
|
70
|
{ready && ( |
|
71
|
<TranslationsEditForm |
|
72
|
translations={translations} |
|
73
|
references={references as { [key: string]: string; id: string }[]} |
|
74
|
entityType={reference!} |
|
75
|
availableLocales={store?.supported_locales ?? []} |
|
76
|
translatableFields={translation_settings[reference!]?.fields ?? []} |
|
77
|
fetchNextPage={fetchNextPage} |
|
78
|
hasNextPage={hasNextPage} |
|
79
|
isFetchingNextPage={isFetchingNextPage} |
|
80
|
referenceCount={count} |
|
81
|
/> |
|
82
|
)} |
|
83
|
</RouteFocusModal> |
|
84
|
) |
|
85
|
} |