2022-12-31 12:40:11 -08:00
|
|
|
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';
|
2022-12-31 12:40:11 -08:00
|
|
|
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';
|
2022-12-31 12:40:11 -08:00
|
|
|
|
|
|
|
|
interface CreatePlaylistFormProps {
|
|
|
|
|
onCancel: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const CreatePlaylistForm = ({ onCancel }: CreatePlaylistFormProps) => {
|
|
|
|
|
const mutation = useCreatePlaylist();
|
2023-01-01 14:02:03 -08:00
|
|
|
const server = useCurrentServer();
|
2022-12-31 12:40:11 -08:00
|
|
|
|
|
|
|
|
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;
|
2022-12-31 12:40:11 -08:00
|
|
|
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>
|
2022-12-31 12:40:11 -08:00
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
};
|