From dc6bd1fd1b2ad477f1769664bade46868551ebf8 Mon Sep 17 00:00:00 2001 From: LASER-Yi Date: Mon, 24 Jan 2022 10:48:33 +0800 Subject: [PATCH] Fix a issue that the edit modal will clear the languages profiles if without changing anything --- .../src/components/modals/ItemEditorModal.tsx | 29 ++++++++++--------- frontend/src/components/views/ItemView.tsx | 4 +-- frontend/src/pages/Episodes/index.tsx | 4 +-- frontend/src/pages/Movies/Details/index.tsx | 4 +-- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/frontend/src/components/modals/ItemEditorModal.tsx b/frontend/src/components/modals/ItemEditorModal.tsx index d123250f3..77bbf66f1 100644 --- a/frontend/src/components/modals/ItemEditorModal.tsx +++ b/frontend/src/components/modals/ItemEditorModal.tsx @@ -1,18 +1,18 @@ import { useIsAnyActionRunning, useLanguageProfiles } from "apis/hooks"; -import React, { FunctionComponent, useMemo, useState } from "react"; +import React, { FunctionComponent, useEffect, useMemo, useState } from "react"; import { Container, Form } from "react-bootstrap"; +import { UseMutationResult } from "react-query"; import { GetItemId } from "utilities"; import { AsyncButton, Selector } from "../"; import BaseModal, { BaseModalProps } from "./BaseModal"; import { useModalInformation } from "./hooks"; interface Props { - submit: (form: FormType.ModifyItem) => Promise; - onSuccess?: (item: Item.Base) => void; + mutation: UseMutationResult; } const Editor: FunctionComponent = (props) => { - const { onSuccess, submit, ...modal } = props; + const { mutation, ...modal } = props; const { data: profiles } = useLanguageProfiles(); @@ -20,6 +20,8 @@ const Editor: FunctionComponent = (props) => { modal.modalKey ); + const { mutateAsync, isLoading } = mutation; + const hasTask = useIsAnyActionRunning(); const profileOptions = useMemo[]>( @@ -29,14 +31,16 @@ const Editor: FunctionComponent = (props) => { }) ?? [], [profiles] ); - const [id, setId] = useState>(null); - const [updating, setUpdating] = useState(false); + const [id, setId] = useState>(payload?.profileId ?? null); + + useEffect(() => { + setId(payload?.profileId ?? null); + }, [payload]); const footer = ( { if (payload) { @@ -45,7 +49,7 @@ const Editor: FunctionComponent = (props) => { return null; } - return submit({ + return mutateAsync({ id: [itemId], profileid: [id], }); @@ -53,10 +57,7 @@ const Editor: FunctionComponent = (props) => { return null; } }} - onSuccess={() => { - closeModal(); - onSuccess && payload && onSuccess(payload); - }} + onSuccess={() => closeModal()} > Save @@ -64,7 +65,7 @@ const Editor: FunctionComponent = (props) => { return ( = (props) => { clearable disabled={hasTask} options={profileOptions} - defaultValue={payload?.profileId} + value={id} onChange={(v) => setId(v === undefined ? null : v)} > diff --git a/frontend/src/components/views/ItemView.tsx b/frontend/src/components/views/ItemView.tsx index 22cd56ea8..c1c0a2052 100644 --- a/frontend/src/components/views/ItemView.tsx +++ b/frontend/src/components/views/ItemView.tsx @@ -36,8 +36,6 @@ function ItemView({ }: Props) { const [editMode, setEditMode] = useState(false); - const { mutateAsync } = mutation; - const showModal = useShowModal(); const updateRow = useCallback>( @@ -77,7 +75,7 @@ function ItemView({ query={query} data={[]} > - + ); diff --git a/frontend/src/pages/Episodes/index.tsx b/frontend/src/pages/Episodes/index.tsx index cf90ac26f..dacc4a47c 100644 --- a/frontend/src/pages/Episodes/index.tsx +++ b/frontend/src/pages/Episodes/index.tsx @@ -44,7 +44,7 @@ const SeriesEpisodesView: FunctionComponent = (props) => { const { data: series, isFetched } = useSeriesById(id); const { data: episodes } = useEpisodesBySeriesId(id); - const { mutateAsync } = useSeriesModification(); + const mutation = useSeriesModification(); const { mutateAsync: action } = useSeriesAction(); const available = episodes?.length !== 0; @@ -168,7 +168,7 @@ const SeriesEpisodesView: FunctionComponent = (props) => { > )} - + = ({ match }) => { const showModal = useShowModal(); - const { mutateAsync } = useMovieModification(); + const mutation = useMovieModification(); const { mutateAsync: action } = useMovieAction(); const { mutateAsync: downloadAsync } = useDownloadMovieSubtitles(); @@ -177,7 +177,7 @@ const MovieDetailView: FunctionComponent = ({ match }) => {
- +