Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete tag #193

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/actions/Tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ export const createTag = async (name) => {
});
};

export const deleteTag = async (_id) => {
return fetch(urls.api.tag.delete, {
method: "POST",
mode: "same-origin",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ _id }),
})
.then((response) => response.json())
.then((json) => {
if (json == null) {
throw new Error("Could not connect to API!");
} else if (!json.success) {
throw new Error(json.message);
}
return json.payload;
});
};

export const getTagsObject = async () => {
return fetch(urls.api.tag.getObject, {
method: "GET",
Expand Down
87 changes: 77 additions & 10 deletions src/components/Tag/TagSelect.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,86 @@
import { CloseIcon } from "@chakra-ui/icons";
import { AbsoluteCenter, Button, useDisclosure } from "@chakra-ui/react";
import { useState } from "react";
import { useSWRConfig } from "swr";
import { deleteTag } from "../../actions/Tag";
import urls from "../../lib/utils/urls";
import CheckboxArrayControl from "../FormComponents/CheckboxArrayControl";
import ConfirmActionModal from "../Modals/ConfirmActionModal";

const TagSelect = ({ tag }) => {
const [hover, setHover] = useState(false);

const { mutate } = useSWRConfig();

const removeTagFromDatabase = async () => {
await deleteTag(tag.id);
mutate(urls.api.tag.getObject);
};

const updateTags = async () => {
await removeTagFromDatabase();
onCloseDeleteModal();
};

const {
isOpen: isOpenDeleteModal,
onOpen: onOpenDeleteModal,
onClose: onCloseDeleteModal,
} = useDisclosure();

return (
<CheckboxArrayControl
key={tag._id}
name="tagArray"
value={tag.name}
style={{
w: "100%",
textTransform: "capitalize",
fontSize: { base: "0.8em", "2xl": "1em" },
<div
onMouseEnter={() => {
setHover(true);
}}
onMouseLeave={() => {
setHover(false);
}}
>
{tag.name}
</CheckboxArrayControl>
<CheckboxArrayControl
key={tag._id}
name="tagArray"
value={tag.name}
style={{
w: "100%",
textTransform: "capitalize",
fontSize: { base: "0.8em", "2xl": "1em" },
}}
>
{tag.name}
{hover && (
<AbsoluteCenter
right="-1.35rem"
bg="transparent"
p="1"
axis="vertical"
>
<Button
minWidth="15px"
minHeight="15px"
height="15px"
width="15px"
backgroundColor="transparent"
color="none"
rounded="full"
_hover={{ bg: "#E2E3E5" }}
onClick={onOpenDeleteModal}
padding="0"
>
<CloseIcon h={1.5} w={1.5} color="#515254" />
</Button>
</AbsoluteCenter>
)}
<ConfirmActionModal
isOpen={isOpenDeleteModal}
onClose={onCloseDeleteModal}
mainText="Are you sure you want to delete this tag?"
confirmButtonText="Yes, delete tag"
cancelButtonText="No, return to add standard"
handleAction={updateTags}
/>
</CheckboxArrayControl>
</div>
);
};

Expand Down
Loading