Files
Homarr/src/components/Dashboard/Modals/ChangePosition/ChangePositionModal.tsx

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-12-10 15:00:16 +01:00
import { Button, Flex, Grid, NumberInput } from '@mantine/core';
import { useForm } from '@mantine/form';
2022-12-10 15:00:16 +01:00
import { closeModal, ContextModalProps } from '@mantine/modals';
import { useConfigContext } from '../../../../config/provider';
import { useConfigStore } from '../../../../config/store';
import { ServiceType } from '../../../../types/service';
import { TileBaseType } from '../../../../types/tile';
export const ChangePositionModal = ({
context,
id,
innerProps,
}: ContextModalProps<{ type: 'service' | 'type'; tile: TileBaseType }>) => {
2022-12-10 15:00:16 +01:00
const updateConfig = useConfigStore((x) => x.updateConfig);
const { name: configName } = useConfigContext();
const form = useForm({
initialValues: {
2022-12-10 15:00:16 +01:00
tile: innerProps.tile,
},
2022-12-10 15:00:16 +01:00
validateInputOnChange: true,
validateInputOnBlur: true,
});
2022-12-10 15:00:16 +01:00
const onSubmit = () => {
if (!configName) {
return;
}
const tileAsService = form.values.tile as ServiceType;
updateConfig(configName, (previous) => ({
...previous,
services: [...previous.services.filter((x) => x.id === tileAsService.id), tileAsService],
}));
};
return (
2022-12-10 15:00:16 +01:00
<form onSubmit={form.onSubmit(onSubmit)}>
<Grid>
<Grid.Col xs={12} md={6}>
2022-12-10 15:00:16 +01:00
<NumberInput
max={99}
min={0}
label="X Position"
description="0 or higher"
{...form.getInputProps('tile.shape.location.x')}
/>
</Grid.Col>
<Grid.Col xs={12} md={6}>
2022-12-10 15:00:16 +01:00
<NumberInput
max={99}
min={0}
label="Y Position"
description="0 or higher"
{...form.getInputProps('tile.shape.location.y')}
/>
</Grid.Col>
</Grid>
2022-12-10 15:00:16 +01:00
<Grid>
<Grid.Col xs={12} md={6}>
<NumberInput
max={24}
min={1}
label="Width"
description="Between 1 and 24"
{...form.getInputProps('tile.shape.size.width')}
/>
</Grid.Col>
<Grid.Col xs={12} md={6}>
<NumberInput
max={24}
min={1}
label="Height"
description="Between 1 and 24"
{...form.getInputProps('tile.shape.size.height')}
/>
</Grid.Col>
</Grid>
<Flex justify="end" gap="sm" mt="md">
<Button onClick={() => closeModal(id)} variant="light" color="gray">
Cancel
</Button>
<Button type="submit">Change Position</Button>
</Flex>
</form>
);
};