Skip to content

Commit

Permalink
fix: fix form state resetting in media source edit modals
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbenincasa committed Dec 6, 2024
1 parent 3e48d6f commit 236d819
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export function JellyfinServerEditDialog({ open, onClose, server }: Props) {
const title = server ? `Editing "${server.name}"` : 'New Media Source';

const handleClose = () => {
reset(emptyDefaults);
setShowAccessToken(false);
onClose();
};
Expand All @@ -99,6 +98,12 @@ export function JellyfinServerEditDialog({ open, onClose, server }: Props) {
defaultValues: server ?? emptyDefaults,
});

useEffect(() => {
if (open) {
reset();
}
}, [reset, open]);

// These are updated in a watch callback, so we debounce them
// along with the details we use to check server status. Otherwise
// setting the error will cause us to check server status on every
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { MediaSourceSettings } from '@tunarr/types';
import { capitalize, isNull, isUndefined } from 'lodash-es';
import { useState } from 'react';
import { RotatingLoopIcon } from '../../base/LoadingIcon.tsx';
import { JellyfinServerEditDialog } from './JelllyfinServerEditDialog.tsx';
import { MediaSourceDeleteDialog } from './MediaSourceDeleteDialog.tsx';
import { PlexServerEditDialog } from './PlexServerEditDialog.tsx';
import { JellyfinServerEditDialog } from './JelllyfinServerEditDialog.tsx';

export function MediaSourceTableRow({ server }: MediaSourceTableRowProps) {
const apiClient = useTunarrApi();
Expand All @@ -19,7 +19,7 @@ export function MediaSourceTableRow({ server }: MediaSourceTableRowProps) {
isLoading: backendStatusLoading,
error: backendStatusError,
} = useQuery({
queryKey: ['plex-servers', server.id, 'status'],
queryKey: ['settings', 'media-sources', server.id, 'status'],
queryFn: () =>
apiClient.getMediaSourceStatus({ params: { id: server.id } }),
staleTime: 1000 * 60 * 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export function PlexServerEditDialog({ open, onClose, server }: Props) {
const title = server ? `Editing "${server.name}"` : 'New Plex Server';

const handleClose = () => {
reset(emptyDefaults);
setShowAccessToken(false);
onClose();
};
Expand All @@ -81,6 +80,12 @@ export function PlexServerEditDialog({ open, onClose, server }: Props) {
defaultValues: server ?? emptyDefaults,
});

useEffect(() => {
if (open) {
reset();
}
}, [open, reset]);

const updatePlexServerMutation = useMutation({
mutationFn: async (newOrUpdatedServer: PlexServerSettingsForm) => {
if (isNonEmptyString(newOrUpdatedServer.id)) {
Expand All @@ -98,6 +103,7 @@ export function PlexServerEditDialog({ open, onClose, server }: Props) {
onSuccess: async () => {
await queryClient.invalidateQueries({
queryKey: ['settings', 'media-sources'],
exact: false,
});
handleClose();
},
Expand Down Expand Up @@ -160,7 +166,6 @@ export function PlexServerEditDialog({ open, onClose, server }: Props) {
fullWidth
component="form"
onSubmit={onSubmit}
keepMounted={false}
onClose={() => onClose()}
>
<DialogTitle>{title}</DialogTitle>
Expand Down
12 changes: 0 additions & 12 deletions web/src/pages/settings/MediaSourceSettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import UnsavedNavigationAlert from '@/components/settings/UnsavedNavigationAlert.tsx';
import { AddMediaSourceButton } from '@/components/settings/media_source/AddMediaSourceButton.tsx';
import { JellyfinServerEditDialog } from '@/components/settings/media_source/JelllyfinServerEditDialog.tsx';
import { MediaSourceTableRow } from '@/components/settings/media_source/MediaSourceTableRow';
import { PlexServerEditDialog } from '@/components/settings/media_source/PlexServerEditDialog';
import {
CheckboxFormController,
TypedController,
Expand Down Expand Up @@ -51,8 +49,6 @@ const supportedPaths = [
export default function MediaSourceSettingsPage() {
const apiClient = useTunarrApi();
const [restoreTunarrDefaults, setRestoreTunarrDefaults] = useState(false);
const [plexEditDialogOpen, setPlexEditDialogOpen] = useState(false);
const [jellyfinEditDialogOpen, setJellyfinEditDialogOpen] = useState(false);

const {
data: servers,
Expand Down Expand Up @@ -375,14 +371,6 @@ export default function MediaSourceSettingsPage() {
</Stack>
</Stack>
</Box>
<PlexServerEditDialog
open={plexEditDialogOpen}
onClose={() => setPlexEditDialogOpen(false)}
/>
<JellyfinServerEditDialog
open={jellyfinEditDialogOpen}
onClose={() => setJellyfinEditDialogOpen(false)}
/>
</Box>
);
}

0 comments on commit 236d819

Please sign in to comment.