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

82 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Group, Stack } from '@mantine/core';
import { useForm } from '@mantine/form';
2023-01-01 14:02:03 -08:00
import { CreatePlaylistQuery, ServerType } from '/@/renderer/api/types';
import { Button, Switch, TextInput, toast } from '/@/renderer/components';
import { useCreatePlaylist } from '/@/renderer/features/playlists/mutations/create-playlist-mutation';
2023-01-01 14:02:03 -08:00
import { useCurrentServer } from '/@/renderer/store';
interface CreatePlaylistFormProps {
onCancel: () => void;
}
export const CreatePlaylistForm = ({ onCancel }: CreatePlaylistFormProps) => {
const mutation = useCreatePlaylist();
2023-01-01 14:02:03 -08:00
const server = useCurrentServer();
const form = useForm<CreatePlaylistQuery>({
initialValues: {
comment: '',
name: '',
public: false,
rules: undefined,
},
});
const handleSubmit = form.onSubmit((values) => {
mutation.mutate(
{ query: values },
{
onError: (err) => {
toast.error({ message: err.message, title: 'Error creating playlist' });
},
onSuccess: () => {
toast.success({ message: 'Playlist created successfully' });
onCancel();
},
},
);
});
2023-01-01 14:02:03 -08:00
const isPublicDisplayed = server?.type === ServerType.NAVIDROME;
const isSubmitDisabled = !form.values.name || mutation.isLoading;
return (
<form onSubmit={handleSubmit}>
<Stack>
<TextInput
data-autofocus
required
label="Name"
{...form.getInputProps('name')}
/>
<TextInput
label="Description"
{...form.getInputProps('comment')}
/>
2023-01-01 14:02:03 -08:00
{isPublicDisplayed && (
<Switch
label="Is Public?"
{...form.getInputProps('public')}
/>
)}
<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>
);
};