2023-01-04 18:37:25 -08:00
|
|
|
import { Group, Stack } from '@mantine/core';
|
|
|
|
|
import { useForm } from '@mantine/form';
|
2023-04-30 22:01:52 -07:00
|
|
|
import { CreatePlaylistBody, CreatePlaylistResponse, ServerType } from '/@/renderer/api/types';
|
2023-01-04 18:37:25 -08:00
|
|
|
import { Button, Switch, TextInput, toast } from '/@/renderer/components';
|
|
|
|
|
import { useCreatePlaylist } from '/@/renderer/features/playlists/mutations/create-playlist-mutation';
|
|
|
|
|
import { useCurrentServer } from '/@/renderer/store';
|
|
|
|
|
|
|
|
|
|
interface SaveAsPlaylistFormProps {
|
|
|
|
|
body: Partial<CreatePlaylistBody>;
|
|
|
|
|
onCancel: () => void;
|
2023-04-30 22:01:52 -07:00
|
|
|
onSuccess: (data: CreatePlaylistResponse) => void;
|
2023-05-21 18:20:46 -07:00
|
|
|
serverId: string | undefined;
|
2023-01-04 18:37:25 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-21 18:20:46 -07:00
|
|
|
export const SaveAsPlaylistForm = ({
|
|
|
|
|
body,
|
|
|
|
|
serverId,
|
|
|
|
|
onSuccess,
|
|
|
|
|
onCancel,
|
|
|
|
|
}: SaveAsPlaylistFormProps) => {
|
2023-04-30 22:01:52 -07:00
|
|
|
const mutation = useCreatePlaylist({});
|
2023-01-04 18:37:25 -08:00
|
|
|
const server = useCurrentServer();
|
|
|
|
|
|
|
|
|
|
const form = useForm<CreatePlaylistBody>({
|
|
|
|
|
initialValues: {
|
2023-04-30 22:01:52 -07:00
|
|
|
_custom: {
|
|
|
|
|
navidrome: {
|
|
|
|
|
public: false,
|
|
|
|
|
rules: undefined,
|
|
|
|
|
...body?._custom?.navidrome,
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-01-04 18:37:25 -08:00
|
|
|
comment: body.comment || '',
|
|
|
|
|
name: body.name || '',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const handleSubmit = form.onSubmit((values) => {
|
|
|
|
|
mutation.mutate(
|
2023-05-21 18:20:46 -07:00
|
|
|
{ body: values, serverId },
|
2023-01-04 18:37:25 -08:00
|
|
|
{
|
|
|
|
|
onError: (err) => {
|
|
|
|
|
toast.error({ message: err.message, title: 'Error creating playlist' });
|
|
|
|
|
},
|
|
|
|
|
onSuccess: (data) => {
|
2023-02-07 22:47:23 -08:00
|
|
|
toast.success({ message: `Playlist has been created` });
|
2023-01-04 18:37:25 -08:00
|
|
|
onSuccess(data);
|
|
|
|
|
onCancel();
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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')}
|
|
|
|
|
/>
|
|
|
|
|
{isPublicDisplayed && (
|
|
|
|
|
<Switch
|
|
|
|
|
label="Is Public?"
|
2023-05-30 19:34:07 -07:00
|
|
|
{...form.getInputProps('_custom.navidrome.public', { type: 'checkbox' })}
|
2023-01-04 18:37:25 -08:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<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>
|
|
|
|
|
);
|
|
|
|
|
};
|