feishin/src/renderer/features/playlists/components/update-playlist-form.tsx

166 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-01-03 00:28:09 -08:00
import { Group, Stack } from '@mantine/core';
import { useForm } from '@mantine/form';
2023-05-21 18:20:46 -07:00
import { openModal, closeAllModals } from '@mantine/modals';
import { api } from '/@/renderer/api';
import { queryKeys } from '/@/renderer/api/query-keys';
import {
2023-07-01 19:10:05 -07:00
PlaylistDetailResponse,
ServerListItem,
ServerType,
SortOrder,
UpdatePlaylistBody,
UpdatePlaylistQuery,
User,
UserListQuery,
UserListSort,
2023-05-21 18:20:46 -07:00
} from '/@/renderer/api/types';
import { Button, Select, Switch, TextInput, toast } from '/@/renderer/components';
2023-01-03 00:28:09 -08:00
import { useUpdatePlaylist } from '/@/renderer/features/playlists/mutations/update-playlist-mutation';
2023-05-21 18:20:46 -07:00
import { queryClient } from '/@/renderer/lib/react-query';
2023-01-03 00:28:09 -08:00
import { useCurrentServer } from '/@/renderer/store';
interface UpdatePlaylistFormProps {
2023-07-01 19:10:05 -07:00
body: Partial<UpdatePlaylistBody>;
onCancel: () => void;
query: UpdatePlaylistQuery;
users?: User[];
2023-01-03 00:28:09 -08:00
}
export const UpdatePlaylistForm = ({ users, query, body, onCancel }: UpdatePlaylistFormProps) => {
2023-07-01 19:10:05 -07:00
const mutation = useUpdatePlaylist({});
const server = useCurrentServer();
2023-01-03 00:28:09 -08:00
2023-07-01 19:10:05 -07:00
const userList = users?.map((user) => ({
label: user.name,
value: user.id,
}));
2023-07-01 19:10:05 -07:00
const form = useForm<UpdatePlaylistBody>({
initialValues: {
_custom: {
navidrome: {
owner: body?._custom?.navidrome?.owner || '',
ownerId: body?._custom?.navidrome?.ownerId || '',
public: body?._custom?.navidrome?.public || false,
rules: undefined,
sync: body?._custom?.navidrome?.sync || false,
},
},
comment: body?.comment || '',
name: body?.name || '',
},
2023-07-01 19:10:05 -07:00
});
2023-01-03 00:28:09 -08:00
2023-07-01 19:10:05 -07:00
const handleSubmit = form.onSubmit((values) => {
mutation.mutate(
{
body: values,
query,
serverId: server?.id,
},
{
onError: (err) => {
toast.error({ message: err.message, title: 'Error updating playlist' });
},
onSuccess: () => {
toast.success({ message: `Playlist has been saved` });
onCancel();
},
},
);
});
2023-01-03 00:28:09 -08:00
2023-07-01 19:10:05 -07:00
const isPublicDisplayed = server?.type === ServerType.NAVIDROME;
const isSubmitDisabled = !form.values.name || mutation.isLoading;
2023-01-03 00:28:09 -08:00
2023-07-01 19:10:05 -07:00
return (
<form onSubmit={handleSubmit}>
<Stack>
<TextInput
data-autofocus
required
label="Name"
{...form.getInputProps('name')}
/>
<TextInput
label="Description"
{...form.getInputProps('comment')}
/>
<Select
data={userList || []}
{...form.getInputProps('_custom.navidrome.ownerId')}
label="Owner"
/>
{isPublicDisplayed && (
<Switch
label="Is Public?"
{...form.getInputProps('_custom.navidrome.public', { type: 'checkbox' })}
/>
)}
<Group position="right">
<Button
variant="subtle"
onClick={onCancel}
>
Cancel
</Button>
<Button
disabled={isSubmitDisabled}
loading={mutation.isLoading}
type="submit"
variant="filled"
>
Save
</Button>
</Group>
</Stack>
</form>
);
2023-01-03 00:28:09 -08:00
};
2023-05-21 18:20:46 -07:00
export const openUpdatePlaylistModal = async (args: {
2023-07-01 19:10:05 -07:00
playlist: PlaylistDetailResponse;
server: ServerListItem;
2023-05-21 18:20:46 -07:00
}) => {
2023-07-01 19:10:05 -07:00
const { playlist, server } = args;
2023-05-21 18:20:46 -07:00
2023-07-01 19:10:05 -07:00
const query: UserListQuery = {
sortBy: UserListSort.NAME,
sortOrder: SortOrder.ASC,
startIndex: 0,
};
2023-05-21 18:20:46 -07:00
2023-07-01 19:10:05 -07:00
if (!server) return;
2023-05-21 18:20:46 -07:00
2023-07-01 19:10:05 -07:00
const users = await queryClient.fetchQuery({
queryFn: ({ signal }) =>
api.controller.getUserList({ apiClientProps: { server, signal }, query }),
queryKey: queryKeys.users.list(server?.id || '', query),
});
2023-05-21 18:20:46 -07:00
2023-07-01 19:10:05 -07:00
openModal({
children: (
<UpdatePlaylistForm
body={{
_custom: {
navidrome: {
owner: playlist?.owner || undefined,
ownerId: playlist?.ownerId || undefined,
public: playlist?.public || false,
rules: playlist?.rules || undefined,
sync: playlist?.sync || undefined,
},
},
comment: playlist?.description || undefined,
genres: playlist?.genres,
name: playlist?.name,
}}
query={{ id: playlist?.id }}
users={users?.items}
onCancel={closeAllModals}
/>
),
title: 'Edit playlist',
});
2023-05-21 18:20:46 -07:00
};