Fix display issue when deleting language in languages profiles setting

pull/1311/head^2
LASER-Yi 4 years ago
parent ec8bb2bc35
commit 2d0e3af693

@ -45,11 +45,13 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
const languages = useEnabledLanguages(); const languages = useEnabledLanguages();
const [current, setProfile] = useState(profile ?? createDefaultProfile()); const [current, setProfile] = useState(createDefaultProfile);
useEffect(() => { useEffect(() => {
if (profile) { if (profile) {
setProfile(profile); setProfile(profile);
} else {
setProfile(createDefaultProfile);
} }
}, [profile]); }, [profile]);
@ -71,9 +73,9 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
key: K, key: K,
value: Profile.Languages[K] value: Profile.Languages[K]
) => { ) => {
const object = { ...current }; const newProfile = { ...current };
object[key] = value; newProfile[key] = value;
setProfile(object); setProfile(newProfile);
}, },
[current] [current]
); );
@ -140,14 +142,15 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
Cell: ({ value, row, externalUpdate }) => { Cell: ({ value, row, externalUpdate }) => {
const code = value; const code = value;
const item = row.original; const item = row.original;
const lang = useMemo(() => languages.find((l) => l.code2 === code), [ const lang = useMemo(
code, () => languages.find((l) => l.code2 === code) ?? null,
]); [code]
);
return ( return (
<div style={{ width: "8rem" }}> <div style={{ width: "8rem" }}>
<LanguageSelector <LanguageSelector
options={languages} options={languages}
defaultValue={lang} value={lang}
onChange={(l) => { onChange={(l) => {
if (l) { if (l) {
item.language = l.code2; item.language = l.code2;
@ -168,7 +171,7 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
<Form.Check <Form.Check
custom custom
id={`${item.language}-forced`} id={`${item.language}-forced`}
defaultChecked={value === "True"} checked={value === "True"}
onChange={(v) => { onChange={(v) => {
item.forced = v.target.checked ? "True" : "False"; item.forced = v.target.checked ? "True" : "False";
externalUpdate && externalUpdate(row, item); externalUpdate && externalUpdate(row, item);
@ -186,7 +189,7 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
<Form.Check <Form.Check
custom custom
id={`${item.language}-hi`} id={`${item.language}-hi`}
defaultChecked={value === "True"} checked={value === "True"}
onChange={(v) => { onChange={(v) => {
item.hi = v.target.checked ? "True" : "False"; item.hi = v.target.checked ? "True" : "False";
externalUpdate && externalUpdate(row, item); externalUpdate && externalUpdate(row, item);
@ -204,7 +207,7 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
<Form.Check <Form.Check
custom custom
id={`${item.language}-audio`} id={`${item.language}-audio`}
defaultChecked={value === "True"} checked={value === "True"}
onChange={(v) => { onChange={(v) => {
item.audio_exclude = v.target.checked ? "True" : "False"; item.audio_exclude = v.target.checked ? "True" : "False";
externalUpdate && externalUpdate(row, item); externalUpdate && externalUpdate(row, item);
@ -235,7 +238,7 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
<Form.Control <Form.Control
type="text" type="text"
placeholder="Name" placeholder="Name"
value={current?.name} value={current.name}
onChange={(v) => { onChange={(v) => {
updateProfile("name", v.target.value); updateProfile("name", v.target.value);
}} }}
@ -245,7 +248,7 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
<SimpleTable <SimpleTable
responsive={false} responsive={false}
columns={columns} columns={columns}
data={current?.items ?? []} data={current.items}
externalUpdate={updateRow} externalUpdate={updateRow}
></SimpleTable> ></SimpleTable>
<Button block variant="light" onClick={addItem}> <Button block variant="light" onClick={addItem}>
@ -256,7 +259,7 @@ const LanguagesProfileModal: FunctionComponent<Props & BaseModalProps> = (
<Selector <Selector
clearable clearable
options={cutoff} options={cutoff}
value={current?.cutoff} value={current.cutoff}
onChange={(num) => updateProfile("cutoff", num)} onChange={(num) => updateProfile("cutoff", num)}
></Selector> ></Selector>
<Message>Ignore others if existing</Message> <Message>Ignore others if existing</Message>

@ -1,4 +1,5 @@
import { faTrash, faWrench } from "@fortawesome/free-solid-svg-icons"; import { faTrash, faWrench } from "@fortawesome/free-solid-svg-icons";
import { cloneDeep } from "lodash";
import React, { import React, {
FunctionComponent, FunctionComponent,
useCallback, useCallback,
@ -17,7 +18,7 @@ import { anyCutoff } from "./options";
const Table: FunctionComponent = () => { const Table: FunctionComponent = () => {
const originalProfiles = useProfiles(); const originalProfiles = useProfiles();
const [profiles, setProfiles] = useState([...originalProfiles]); const [profiles, setProfiles] = useState(() => cloneDeep(originalProfiles));
const nextProfileId = useMemo( const nextProfileId = useMemo(
() => () =>
@ -56,7 +57,7 @@ const Table: FunctionComponent = () => {
const updateRow = useCallback<TableUpdater<Profile.Languages>>( const updateRow = useCallback<TableUpdater<Profile.Languages>>(
(row, item?: Profile.Languages) => { (row, item?: Profile.Languages) => {
if (item) { if (item) {
showModal("profile", item); showModal("profile", cloneDeep(item));
} else { } else {
const list = [...profiles]; const list = [...profiles];
list.splice(row.index, 1); list.splice(row.index, 1);

Loading…
Cancel
Save