import Link from "next/link"; import { useRouter } from "next/router"; import React, { useEffect, useState } from "react"; import { useMutation, useQueryClient } from "react-query"; import { delTag, favExo, setTags } from "../../requests/requests.exos.js"; import { notificationService } from "../../services/notification.service.js"; import styles from "../../styles/exos/Card.module.scss"; import { isBrowser, isEmpty, parseClassName } from "../../utils/utils.js"; import Delete from "../icon_component/delete.jsx"; import Tag from "../Tag.jsx"; import { TagCreatable } from "./TagCreate.jsx"; import { BsBookmark, BsBookmarkFill } from "react-icons/bs"; import { HiOutlineDuplicate } from "react-icons/hi"; import { useSessionContext } from "../../context/session.context.js"; function CardComp({ step, setActive, deleted, tags, queryKey }) { const [tagMode, setTagMode] = useState(false); const router = useRouter(); const [selected, setSelected] = useState([]); const queryClient = useQueryClient(); const { mutate } = useMutation( async () => await setTags({ step: step.id_code, tags: [...selected] }), { onSuccess: (data, variables, context) => { queryClient.setQueryData(queryKey, (old) => { return { ...old, results: [ ...old.results.map((r) => { if (r.id_code === data.data.id_code) { return { ...r, tags: data.data.tags }; } return r; }), ], }; }); queryClient.invalidateQueries(queryKey[0], { active: false }); queryClient.invalidateQueries("tags"); setSelected([]); notificationService.success("Tags", `Tags de ${step.name} modifiés !`, { autoClose: true, }); setTagMode(false); }, onError: (data) => { notificationService.error( "Erreur", `Les tags de ${step.name} n'ont pu être modifiés !` ); }, } ); const { mutate: delTagMutate } = useMutation( async (tag) => { return await delTag({ tag: tag, step: step.id_code }); }, { onSuccess: (data) => { var tagsSelected = queryKey[2].tags || null; if (tagsSelected && tagsSelected.includes(data.data.tagId)) { queryClient.invalidateQueries(queryKey); } queryClient.setQueriesData(queryKey, (old) => { return { ...old, results: [ ...old.results.map((r) => { if (r.id_code === data.data.id_code) { return { ...r, tags: data.data.tags }; } return r; }), ], }; }); queryClient.invalidateQueries(queryKey[0], { active: false }); notificationService.success( "Tags", `Tag ${data.data.name} retiré à ${step.name} !`, { autoClose: true, } ); setTagMode(false); }, onError: (e) => { notificationService.error( "Erreur", `Le tag n'a pu être retiré de ${step.name} !` ); }, } ); const [tagOptions, setTagOptions] = useState( tags.map((tag) => { return { ...tag, isDisabled: step.tags.map((t) => t.id_code).includes(tag.id_code), }; }) ); useEffect(() => { setTagOptions( tags.map((tag) => { return { ...tag, isDisabled: step.tags.map((t) => t.id_code).includes(tag.id_code), }; }) ); return () => { setTagOptions([]); }; }, [step.tags, tags]); const {authData} = useSessionContext() return (
{/* */}
); } const StepsCardCompar = (prv, nxt) => { return ( prv.step.tags.length == nxt.step.tags.length && prv.tags.length == nxt.tags.length && prv.step.name == nxt.step.name && prv.step.exo_model == nxt.step.exo_model && prv.step.model_type == nxt.step.model_type && prv.step.isRegistered == nxt.step.isRegistered && prv.step.consigne == nxt.step.consigne && prv.queryKey == nxt.queryKey ); }; export const Card = React.memo(CardComp, StepsCardCompar);