mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-19 22:24:11 +00:00
refactor(translations): allow resetting to default values
This commit is contained in:
committed by
Carlos Valente
parent
1553573b94
commit
ce8a4d230b
+3
@@ -0,0 +1,3 @@
|
|||||||
|
.footer {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
+28
-14
@@ -1,5 +1,5 @@
|
|||||||
import { TranslationObject, langEn } from 'ontime-types';
|
import { TranslationObject, langEn } from 'ontime-types';
|
||||||
import { useMemo } from 'react';
|
import { useEffect, useMemo } from 'react';
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
|
|
||||||
import { maybeAxiosError } from '../../../../../common/api/utils';
|
import { maybeAxiosError } from '../../../../../common/api/utils';
|
||||||
@@ -10,6 +10,8 @@ import Modal from '../../../../../common/components/modal/Modal';
|
|||||||
import { useTranslation } from '../../../../../translation/TranslationProvider';
|
import { useTranslation } from '../../../../../translation/TranslationProvider';
|
||||||
import * as Panel from '../../../panel-utils/PanelUtils';
|
import * as Panel from '../../../panel-utils/PanelUtils';
|
||||||
|
|
||||||
|
import style from './CustomTranslationModal.module.scss';
|
||||||
|
|
||||||
interface CustomTranslationModalProps {
|
interface CustomTranslationModalProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
@@ -17,14 +19,7 @@ interface CustomTranslationModalProps {
|
|||||||
|
|
||||||
export default function CustomTranslationModal({ isOpen, onClose }: CustomTranslationModalProps) {
|
export default function CustomTranslationModal({ isOpen, onClose }: CustomTranslationModalProps) {
|
||||||
const { userTranslation, postUserTranslation } = useTranslation();
|
const { userTranslation, postUserTranslation } = useTranslation();
|
||||||
|
const customTranslationFormValues = useMemo(() => toFormValues(userTranslation), [userTranslation]);
|
||||||
const defaultValues = useMemo(() => {
|
|
||||||
const values: Record<string, string> = {};
|
|
||||||
Object.keys(langEn).forEach((key) => {
|
|
||||||
values[toFormKey(key)] = userTranslation[key as keyof TranslationObject] || '';
|
|
||||||
});
|
|
||||||
return values;
|
|
||||||
}, [userTranslation]);
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
@@ -33,13 +28,17 @@ export default function CustomTranslationModal({ isOpen, onClose }: CustomTransl
|
|||||||
formState: { isSubmitting, isDirty, errors, isValid },
|
formState: { isSubmitting, isDirty, errors, isValid },
|
||||||
setError,
|
setError,
|
||||||
} = useForm({
|
} = useForm({
|
||||||
defaultValues,
|
defaultValues: customTranslationFormValues,
|
||||||
resetOptions: {
|
|
||||||
keepDirtyValues: true,
|
|
||||||
},
|
|
||||||
mode: 'onChange',
|
mode: 'onChange',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// keep data up to date
|
||||||
|
useEffect(() => {
|
||||||
|
if (isOpen) {
|
||||||
|
reset(customTranslationFormValues);
|
||||||
|
}
|
||||||
|
}, [customTranslationFormValues, isOpen, reset]);
|
||||||
|
|
||||||
const onSubmit = async (formData: Record<string, string>) => {
|
const onSubmit = async (formData: Record<string, string>) => {
|
||||||
try {
|
try {
|
||||||
const translationData: Record<string, string> = {};
|
const translationData: Record<string, string> = {};
|
||||||
@@ -54,6 +53,10 @@ export default function CustomTranslationModal({ isOpen, onClose }: CustomTransl
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const resetToEnglish = () => {
|
||||||
|
reset(toFormValues(langEn), { keepDefaultValues: true });
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Modal
|
<Modal
|
||||||
title='Edit custom translations'
|
title='Edit custom translations'
|
||||||
@@ -84,9 +87,12 @@ export default function CustomTranslationModal({ isOpen, onClose }: CustomTransl
|
|||||||
</Panel.Section>
|
</Panel.Section>
|
||||||
}
|
}
|
||||||
footerElements={
|
footerElements={
|
||||||
<div>
|
<div className={style.footer}>
|
||||||
{errors?.root && <Panel.Error>{errors.root.message}</Panel.Error>}
|
{errors?.root && <Panel.Error>{errors.root.message}</Panel.Error>}
|
||||||
<Panel.InlineElements align='apart'>
|
<Panel.InlineElements align='apart'>
|
||||||
|
<Button variant='ghosted' size='large' onClick={resetToEnglish} disabled={isSubmitting}>
|
||||||
|
Reset to English
|
||||||
|
</Button>
|
||||||
<Panel.InlineElements>
|
<Panel.InlineElements>
|
||||||
<Button size='large' onClick={onClose}>
|
<Button size='large' onClick={onClose}>
|
||||||
Cancel
|
Cancel
|
||||||
@@ -109,6 +115,14 @@ export default function CustomTranslationModal({ isOpen, onClose }: CustomTransl
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toFormValues(translation: Partial<TranslationObject>) {
|
||||||
|
const values: Record<string, string> = {};
|
||||||
|
Object.keys(langEn).forEach((key) => {
|
||||||
|
values[toFormKey(key)] = translation[key as keyof TranslationObject] || '';
|
||||||
|
});
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
function toFormKey(key: string) {
|
function toFormKey(key: string) {
|
||||||
return key.replace('.', '_');
|
return key.replace('.', '_');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user