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

116 lines
4 KiB
TypeScript
Raw Normal View History

2023-01-04 18:37:25 -08:00
import { Group, Stack } from '@mantine/core';
import { useForm } from '@mantine/form';
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';
import { useTranslation } from 'react-i18next';
2024-09-01 09:37:37 -07:00
import { ServerFeature } from '/@/renderer/api/features-types';
import { hasFeature } from '/@/renderer/api/utils';
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,
serverId,
onSuccess,
onCancel,
2023-05-21 18:20:46 -07:00
}: SaveAsPlaylistFormProps) => {
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-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) => {
toast.error({
message: err.message,
title: t('error.genericError', { postProcess: 'sentenceCase' }),
});
2023-07-01 19:10:05 -07:00
},
onSuccess: (data) => {
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
required
label={t('form.createPlaylist.input', {
context: 'name',
postProcess: 'titleCase',
})}
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
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
variant="subtle"
onClick={onCancel}
>
{t('common.cancel', { postProcess: 'titleCase' })}
2023-07-01 19:10:05 -07:00
</Button>
<Button
disabled={isSubmitDisabled}
loading={mutation.isLoading}
type="submit"
variant="filled"
>
{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
};