add group deletion

This commit is contained in:
Michael Fatemi 2021-06-23 17:24:28 -04:00
parent 3bd2435efb
commit 7f26931962
2 changed files with 67 additions and 5 deletions

View File

@ -1,7 +1,46 @@
import { useCallback, useState } from 'react'; import { useCallback, useState } from 'react';
import { IGroup } from './Group'; import { IGroup } from './Group';
import UILink from './UILink';
import UIPressable from './UIPressable';
import UISecondaryBox from './UISecondaryBox'; import UISecondaryBox from './UISecondaryBox';
function GroupSettings({ group }: { group: IGroup }) {
const [deletionSuccessful, setDeletionSuccessful] =
useState<boolean | null>(null);
const deleteGroup = useCallback(() => {
fetch('http://localhost:5000/api/groups/' + group.id, { method: 'delete' })
.then((res) => res.json())
.then(({ status }) => {
setDeletionSuccessful(status === 'success');
})
.catch(() => {
setDeletionSuccessful(false);
});
}, [group.id]);
return (
<UISecondaryBox>
<h1>Settings</h1>
{deletionSuccessful !== true && (
<UIPressable onClick={deleteGroup}>Delete Group</UIPressable>
)}
{deletionSuccessful !== null &&
(deletionSuccessful ? (
<span>
<b>{group.name}</b> was deleted.
<br />
<UILink href="/">Home</UILink>
</span>
) : (
<span>
For some reason, <b>{group.name}</b> was not successfully deleted.
</span>
))}
</UISecondaryBox>
);
}
export default function GroupSettingsLink({ group }: { group: IGroup }) { export default function GroupSettingsLink({ group }: { group: IGroup }) {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const toggle = useCallback(() => { const toggle = useCallback(() => {
@ -20,11 +59,7 @@ export default function GroupSettingsLink({ group }: { group: IGroup }) {
> >
Settings Settings
</div> </div>
{open && ( {open && <GroupSettings group={group} />}
<UISecondaryBox>
<h1>Settings</h1>
</UISecondaryBox>
)}
</div> </div>
); );
} }

View File

@ -0,0 +1,27 @@
import { CSSProperties, ReactNode, useMemo } from 'react';
const baseStyle: CSSProperties = {
textDecoration: 'none',
cursor: 'pointer',
userSelect: 'none',
};
export default function UIPressable({
onClick,
style,
children,
}: {
onClick: () => void;
style?: CSSProperties;
children: ReactNode;
}) {
const computedStyle = useMemo(() => {
return !style ? baseStyle : { ...baseStyle, ...style };
}, [style]);
return (
<div onClick={onClick} style={computedStyle}>
{children}
</div>
);
}