Add the ability to edit the service.

Fixes #2
This commit is contained in:
Aj - Thomas
2022-05-06 22:29:47 +02:00
parent db01831916
commit c9dd7db9ef
3 changed files with 155 additions and 119 deletions

View File

@@ -22,15 +22,6 @@ export default function AddItemShelfItem(props: any) {
const { addService } = useConfig(); const { addService } = useConfig();
const [opened, setOpened] = useState(false); const [opened, setOpened] = useState(false);
const theme = useMantineTheme(); const theme = useMantineTheme();
const form = useForm({
initialValues: {
type: 'Other',
name: '',
icon: '',
url: '',
apiKey: undefined as unknown as string,
},
});
return ( return (
<> <>
<Modal <Modal
@@ -40,18 +31,72 @@ export default function AddItemShelfItem(props: any) {
onClose={() => setOpened(false)} onClose={() => setOpened(false)}
title="Add a service" title="Add a service"
> >
<AddAppShelfItemForm setOpened={setOpened} />
</Modal>
<AspectRatio
style={{
minHeight: 120,
minWidth: 120,
}}
ratio={4 / 3}
>
<Card
style={{
backgroundColor:
theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.colors.gray[1],
width: 200,
height: 180,
}}
radius="md"
>
<Group direction="column" position="center">
<motion.div whileHover={{ scale: 1.2 }}>
<Apps style={{ cursor: 'pointer' }} onClick={() => setOpened(true)} size={60} />
</motion.div>
<Text>Add Service</Text>
</Group>
</Card>
</AspectRatio>
</>
);
}
export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & any) {
const { setOpened } = props;
const { addService, config, setConfig } = useConfig();
const form = useForm({
initialValues: {
type: props.type ?? 'Other',
name: props.name ?? '',
icon: props.icon ?? '',
url: props.url ?? '',
apiKey: props.apiKey ?? (undefined as unknown as string),
},
});
return (
<>
<Center> <Center>
<Image <Image height={120} width={120} src={form.values.icon} alt="Placeholder" withPlaceholder />
height={120}
width={120}
src={form.values.icon}
alt="Placeholder"
withPlaceholder
/>
</Center> </Center>
<form <form
onSubmit={form.onSubmit(() => { onSubmit={form.onSubmit(() => {
// If service already exists, update it.
if (config.services && config.services.find((s) => s.name === form.values.name)) {
setConfig({
...config,
services: config.services.map((s) => {
if (s.name === form.values.name) {
return {
...form.values,
};
}
return s;
}),
});
} else {
addService(form.values); addService(form.values);
}
setOpened(false); setOpened(false);
form.reset(); form.reset();
})} })}
@@ -107,34 +152,9 @@ export default function AddItemShelfItem(props: any) {
</Group> </Group>
<Group grow position="center" mt="xl"> <Group grow position="center" mt="xl">
<Button type="submit">Add service</Button> <Button type="submit">{props.message ?? 'Add service'}</Button>
</Group> </Group>
</form> </form>
</Modal>
<AspectRatio
style={{
minHeight: 120,
minWidth: 120,
}}
ratio={4 / 3}
>
<Card
style={{
backgroundColor:
theme.colorScheme === 'dark' ? theme.colors.dark[5] : theme.colors.gray[1],
width: 200,
height: 180,
}}
radius="md"
>
<Group direction="column" position="center">
<motion.div whileHover={{ scale: 1.2 }}>
<Apps style={{ cursor: 'pointer' }} onClick={() => setOpened(true)} size={60} />
</motion.div>
<Text>Add Service</Text>
</Group>
</Card>
</AspectRatio>
</> </>
); );
} }

View File

@@ -103,7 +103,7 @@ export function AppShelfItem(props: any) {
opacity: hovering ? 1 : 0, opacity: hovering ? 1 : 0,
}} }}
> >
<AppShelfMenu name={service.name} removeitem={removeService} /> <AppShelfMenu service={service} removeitem={removeService} />
</motion.div> </motion.div>
</Group> </Group>
</Card.Section> </Card.Section>

View File

@@ -1,36 +1,51 @@
import { Menu, Text } from '@mantine/core'; import { Menu, Modal, Text } from '@mantine/core';
import { showNotification } from '@mantine/notifications'; import { showNotification } from '@mantine/notifications';
import { useState } from 'react';
import { Check, Edit, Trash } from 'tabler-icons-react'; import { Check, Edit, Trash } from 'tabler-icons-react';
import { AddAppShelfItemForm } from './AddAppShelfItem';
export default function AppShelfMenu(props: any) { export default function AppShelfMenu(props: any) {
const { name, removeitem: removeItem } = props; const { service, removeitem: removeItem } = props;
const [opened, setOpened] = useState(false);
return ( return (
<>
<Modal
size="xl"
radius="lg"
opened={props.opened || opened}
onClose={() => setOpened(false)}
title="Modify a service"
>
<AddAppShelfItemForm
setOpened={setOpened}
name={service.name}
type={service.type}
url={service.url}
icon={service.icon}
apiKey={service.apiKey}
message="Save service"
/>
</Modal>
<Menu position="right"> <Menu position="right">
<Menu.Label>Settings</Menu.Label> <Menu.Label>Settings</Menu.Label>
<Menu.Item <Menu.Item
color="primary" color="primary"
icon={<Edit size={14} />} icon={<Edit size={14} />}
// TODO: #2 Add the ability to edit the service. // TODO: #2 Add the ability to edit the service.
onClick={() => { onClick={() => setOpened(true)}
showNotification({
color: 'red',
title: <Text>Feature not yet implemented</Text>,
message: `${name} could not be edited`,
});
}}
> >
Rename Edit
</Menu.Item> </Menu.Item>
<Menu.Label>Danger zone</Menu.Label> <Menu.Label>Danger zone</Menu.Label>
<Menu.Item <Menu.Item
color="red" color="red"
onClick={(e: any) => { onClick={(e: any) => {
removeItem(name); removeItem(service.name);
showNotification({ showNotification({
autoClose: 5000, autoClose: 5000,
title: ( title: (
<Text> <Text>
Service <b>{name}</b> removed successfully Service <b>{service.name}</b> removed successfully
</Text> </Text>
), ),
color: 'green', color: 'green',
@@ -43,5 +58,6 @@ export default function AppShelfMenu(props: any) {
Delete Delete
</Menu.Item> </Menu.Item>
</Menu> </Menu>
</>
); );
} }