2023-01-04 18:37:25 -08:00
|
|
|
import { Group, Stack } from '@mantine/core';
|
|
|
|
|
import { useForm } from '@mantine/form';
|
2025-05-18 14:03:18 -07:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
|
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';
|
2025-05-20 19:23:36 -07:00
|
|
|
import { hasFeature } from '/@/shared/api/utils';
|
|
|
|
|
import {
|
|
|
|
|
CreatePlaylistBody,
|
|
|
|
|
CreatePlaylistResponse,
|
|
|
|
|
ServerType,
|
|
|
|
|
} from '/@/shared/types/domain-types';
|
|
|
|
|
import { ServerFeature } from '/@/shared/types/features-types';
|
2023-01-04 18:37:25 -08:00
|
|
|
|
|
|
|
|
interface SaveAsPlaylistFormProps {
|
2023-07-01 19:10:05 -07:00
|
|
|
body: Partial<CreatePlaylistBody>;
|
|
|
|
|
onCancel: () => void;
|
|
|
|
|
onSuccess: (data: CreatePlaylistResponse) => void;
|
|
|
|
|
serverId: string | undefined;
|
2023-01-04 18:37:25 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-21 18:20:46 -07:00
|
|
|
export const SaveAsPlaylistForm = ({
|
2023-07-01 19:10:05 -07:00
|
|
|
body,
|
|
|
|
|
onCancel,
|
2025-05-18 14:03:18 -07:00
|
|
|
onSuccess,
|
|
|
|
|
serverId,
|
2023-05-21 18:20:46 -07:00
|
|
|
}: SaveAsPlaylistFormProps) => {
|
2023-10-30 19:22:45 -07:00
|
|
|
const { t } = useTranslation();
|
2023-07-01 19:10:05 -07:00
|
|
|
const mutation = useCreatePlaylist({});
|
|
|
|
|
const server = useCurrentServer();
|
2023-01-04 18:37:25 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const form = useForm<CreatePlaylistBody>({
|
|
|
|
|
initialValues: {
|
|
|
|
|
_custom: {
|
|
|
|
|
navidrome: {
|
|
|
|
|
rules: undefined,
|
|
|
|
|
...body?._custom?.navidrome,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
comment: body.comment || '',
|
|
|
|
|
name: body.name || '',
|
2024-09-01 09:37:37 -07:00
|
|
|
public: body.public,
|
2023-04-30 22:01:52 -07:00
|
|
|
},
|
2023-07-01 19:10:05 -07:00
|
|
|
});
|
2023-01-04 18:37:25 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
const handleSubmit = form.onSubmit((values) => {
|
|
|
|
|
mutation.mutate(
|
|
|
|
|
{ body: values, serverId },
|
|
|
|
|
{
|
|
|
|
|
onError: (err) => {
|
2023-10-30 19:22:45 -07:00
|
|
|
toast.error({
|
|
|
|
|
message: err.message,
|
|
|
|
|
title: t('error.genericError', { postProcess: 'sentenceCase' }),
|
|
|
|
|
});
|
2023-07-01 19:10:05 -07:00
|
|
|
},
|
|
|
|
|
onSuccess: (data) => {
|
2023-10-30 19:22:45 -07:00
|
|
|
toast.success({
|
|
|
|
|
message: t('form.createPlaylist.success', { postProcess: 'sentenceCase' }),
|
|
|
|
|
});
|
2023-07-01 19:10:05 -07:00
|
|
|
onSuccess(data);
|
|
|
|
|
onCancel();
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
2023-01-04 18:37:25 -08:00
|
|
|
|
2024-09-01 09:37:37 -07:00
|
|
|
const isPublicDisplayed = hasFeature(server, ServerFeature.PUBLIC_PLAYLIST);
|
2023-07-01 19:10:05 -07:00
|
|
|
const isSubmitDisabled = !form.values.name || mutation.isLoading;
|
2023-01-04 18:37:25 -08:00
|
|
|
|
2023-07-01 19:10:05 -07:00
|
|
|
return (
|
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
|
<Stack>
|
|
|
|
|
<TextInput
|
|
|
|
|
data-autofocus
|
2023-10-30 19:22:45 -07:00
|
|
|
label={t('form.createPlaylist.input', {
|
|
|
|
|
context: 'name',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
2025-05-18 14:03:18 -07:00
|
|
|
required
|
2023-07-01 19:10:05 -07:00
|
|
|
{...form.getInputProps('name')}
|
|
|
|
|
/>
|
2024-09-01 09:37:37 -07:00
|
|
|
{server?.type === ServerType.NAVIDROME && (
|
|
|
|
|
<TextInput
|
|
|
|
|
label={t('form.createPlaylist.input', {
|
|
|
|
|
context: 'description',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
|
|
|
|
{...form.getInputProps('comment')}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2023-07-01 19:10:05 -07:00
|
|
|
{isPublicDisplayed && (
|
|
|
|
|
<Switch
|
2023-10-30 19:22:45 -07:00
|
|
|
label={t('form.createPlaylist.input', {
|
|
|
|
|
context: 'public',
|
|
|
|
|
postProcess: 'titleCase',
|
|
|
|
|
})}
|
2024-09-01 09:37:37 -07:00
|
|
|
{...form.getInputProps('public', { type: 'checkbox' })}
|
2023-07-01 19:10:05 -07:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<Group position="right">
|
|
|
|
|
<Button
|
|
|
|
|
onClick={onCancel}
|
2025-05-18 14:03:18 -07:00
|
|
|
variant="subtle"
|
2023-07-01 19:10:05 -07:00
|
|
|
>
|
2023-10-30 19:22:45 -07:00
|
|
|
{t('common.cancel', { postProcess: 'titleCase' })}
|
2023-07-01 19:10:05 -07:00
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
disabled={isSubmitDisabled}
|
|
|
|
|
loading={mutation.isLoading}
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="filled"
|
|
|
|
|
>
|
2023-10-30 19:22:45 -07:00
|
|
|
{t('common.save', { postProcess: 'titleCase' })}
|
2023-07-01 19:10:05 -07:00
|
|
|
</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</Stack>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
2023-01-04 18:37:25 -08:00
|
|
|
};
|