completd updation of documents
This commit is contained in:
parent
ee1887de8f
commit
06503ac4d3
@ -15,6 +15,7 @@ import {
|
|||||||
} from "../../hooks/useDocument";
|
} from "../../hooks/useDocument";
|
||||||
import showToast from "../../services/toastService";
|
import showToast from "../../services/toastService";
|
||||||
import { useDocumentContext } from "./Documents";
|
import { useDocumentContext } from "./Documents";
|
||||||
|
import { isPending } from "@reduxjs/toolkit";
|
||||||
|
|
||||||
const toBase64 = (file) =>
|
const toBase64 = (file) =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
@ -24,6 +25,34 @@ const toBase64 = (file) =>
|
|||||||
reader.onerror = (err) => reject(err);
|
reader.onerror = (err) => reject(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const MergedTagsWithExistenStatus = (formTags = [], originalTags = []) => {
|
||||||
|
const tagMap = new Map();
|
||||||
|
|
||||||
|
const safeFormTags = Array.isArray(formTags) ? formTags : [];
|
||||||
|
const safeOriginalTags = Array.isArray(originalTags) ? originalTags : [];
|
||||||
|
|
||||||
|
safeOriginalTags.forEach(tag => {
|
||||||
|
if (tag?.name) {
|
||||||
|
tagMap.set(tag.name, { ...tag, isActive: tag.isActive ?? true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
safeFormTags.forEach(tag => {
|
||||||
|
if (tag?.name) {
|
||||||
|
tagMap.set(tag.name, { ...tag, isActive: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
safeOriginalTags.forEach(tag => {
|
||||||
|
if (tag?.name && !safeFormTags.some(t => t.name === tag.name)) {
|
||||||
|
tagMap.set(tag.name, { ...tag, isActive: false });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return Array.from(tagMap.values());
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
const ManageDocument = ({ closeModal, Document_Entity, Entity }) => {
|
const ManageDocument = ({ closeModal, Document_Entity, Entity }) => {
|
||||||
const { ManageDoc } = useDocumentContext();
|
const { ManageDoc } = useDocumentContext();
|
||||||
const isUpdateForm = Boolean(ManageDoc?.document);
|
const isUpdateForm = Boolean(ManageDoc?.document);
|
||||||
@ -45,19 +74,26 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => {
|
|||||||
reset,
|
reset,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
} = methods;
|
} = methods;
|
||||||
const { mutate: UploadDocument, isPending } = useUploadDocument(() => {
|
const { mutate: UploadDocument, isPending: isUploading } = useUploadDocument(
|
||||||
showToast("Document Uploaded Successfully", "success");
|
() => {
|
||||||
closeModal();
|
showToast("Document Uploaded Successfully", "success");
|
||||||
});
|
closeModal();
|
||||||
const { mutate: UpdateDocument, isPending: isUpdatinDoc } = useUpdateDocument(
|
}
|
||||||
|
);
|
||||||
|
const { mutate: UpdateDocument, isPending: isUpdating } = useUpdateDocument(
|
||||||
() => {
|
() => {
|
||||||
showToast("Document Updated Successfully", "success");
|
showToast("Document Updated Successfully", "success");
|
||||||
closeModal();
|
closeModal();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const onSubmit = (data) => {
|
const onSubmit = (data) => {
|
||||||
if (ManageDoc?.document) {
|
if (ManageDoc?.document) {
|
||||||
const DocumentPayload = { ...DocData, ...data };
|
const DocumentPayload = {
|
||||||
|
...data,
|
||||||
|
id: DocData.id,
|
||||||
|
tags: MergedTagsWithExistenStatus(data?.tags, DocData?.tags),
|
||||||
|
};
|
||||||
UpdateDocument({ documentId: DocData?.id, DocumentPayload });
|
UpdateDocument({ documentId: DocData?.id, DocumentPayload });
|
||||||
} else {
|
} else {
|
||||||
const DocumentPayload = { ...data, entityId: Entity };
|
const DocumentPayload = { ...data, entityId: Entity };
|
||||||
@ -176,6 +212,8 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => {
|
|||||||
|
|
||||||
if (isDocLoading) return <div>Loading...</div>;
|
if (isDocLoading) return <div>Loading...</div>;
|
||||||
if (isDocError) return <div>{DocError.message}</div>;
|
if (isDocError) return <div>{DocError.message}</div>;
|
||||||
|
|
||||||
|
const isPending = isUploading || isUpdating;
|
||||||
return (
|
return (
|
||||||
<div className="p-2">
|
<div className="p-2">
|
||||||
<p className="fw-bold fs-6">Upload New Document</p>
|
<p className="fw-bold fs-6">Upload New Document</p>
|
||||||
@ -270,7 +308,7 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => {
|
|||||||
{/* Upload */}
|
{/* Upload */}
|
||||||
<div className="row my-2">
|
<div className="row my-2">
|
||||||
<div className="col-md-12">
|
<div className="col-md-12">
|
||||||
<label className="form-label">Upload Document</label>
|
<Label htmlFor="attachment" required>Upload Document</Label>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
className="border border-secondary border-dashed rounded p-4 text-center bg-textMuted position-relative"
|
className="border border-secondary border-dashed rounded p-4 text-center bg-textMuted position-relative"
|
||||||
@ -336,7 +374,7 @@ const ManageDocument = ({ closeModal, Document_Entity, Entity }) => {
|
|||||||
|
|
||||||
{/* Description */}
|
{/* Description */}
|
||||||
<div className="mb-2">
|
<div className="mb-2">
|
||||||
<Label htmlFor="description">Description</Label>
|
<Label htmlFor="description" required>Description</Label>
|
||||||
<textarea
|
<textarea
|
||||||
rows="2"
|
rows="2"
|
||||||
className="form-control"
|
className="form-control"
|
||||||
|
|||||||
@ -1,37 +1,33 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState, useEffect } from "react";
|
||||||
import { useFormContext } from "react-hook-form";
|
import { useFormContext } from "react-hook-form";
|
||||||
|
|
||||||
const TagInput = ({
|
const TagInput = ({ label, name, placeholder, color = "#e9ecef", options = [] }) => {
|
||||||
label = "Tags",
|
const { setValue, watch } = useFormContext();
|
||||||
name = "tags",
|
const tags = watch(name) || [];
|
||||||
placeholder = "Start typing to add... like employee, manager",
|
|
||||||
color = "#e9ecef",
|
|
||||||
options = [],
|
|
||||||
}) => {
|
|
||||||
const [tags, setTags] = useState([]); // now array of objects
|
|
||||||
const [input, setInput] = useState("");
|
const [input, setInput] = useState("");
|
||||||
const [suggestions, setSuggestions] = useState([]);
|
const [suggestions, setSuggestions] = useState([]);
|
||||||
|
|
||||||
const { setValue } = useFormContext();
|
const handleAdd = (newTag) => {
|
||||||
|
if (!tags.some((t) => t.name === newTag)) {
|
||||||
// Keep form value synced
|
setValue(name, [...tags, { name: newTag, isActive: true }], { shouldValidate: true });
|
||||||
useEffect(() => {
|
|
||||||
setValue(name, tags, { shouldValidate: true });
|
|
||||||
}, [tags, name, setValue]);
|
|
||||||
|
|
||||||
const handleKeyDown = (e) => {
|
|
||||||
if (e.key === "Enter" && input.trim()) {
|
|
||||||
e.preventDefault();
|
|
||||||
if (!tags.some((t) => t.name === input.trim())) {
|
|
||||||
setTags((prev) => [...prev, { name: input.trim(), isActive: true }]);
|
|
||||||
}
|
|
||||||
setInput("");
|
|
||||||
setSuggestions([]);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleRemove = (tagName) => {
|
const handleRemove = (tagName) => {
|
||||||
setTags((prev) => prev.filter((t) => t.name !== tagName));
|
setValue(
|
||||||
|
name,
|
||||||
|
tags.filter((t) => t.name !== tagName),
|
||||||
|
{ shouldValidate: true }
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleKeyDown = (e) => {
|
||||||
|
if (e.key === "Enter" && input.trim()) {
|
||||||
|
e.preventDefault();
|
||||||
|
handleAdd(input.trim());
|
||||||
|
setInput("");
|
||||||
|
setSuggestions([]);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleChange = (e) => {
|
const handleChange = (e) => {
|
||||||
@ -52,10 +48,8 @@ const TagInput = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSuggestionClick = (suggestion) => {
|
const handleSuggestionClick = (sugg) => {
|
||||||
if (!tags.some((t) => t.name === suggestion.name)) {
|
handleAdd(sugg.name);
|
||||||
setTags((prev) => [...prev, suggestion]);
|
|
||||||
}
|
|
||||||
setInput("");
|
setInput("");
|
||||||
setSuggestions([]);
|
setSuggestions([]);
|
||||||
};
|
};
|
||||||
@ -66,10 +60,7 @@ const TagInput = ({
|
|||||||
{label}
|
{label}
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
<div
|
<div className="form-control form-control-sm p-1" style={{ minHeight: "38px", position: "relative" }}>
|
||||||
className="form-control form-control-sm p-1"
|
|
||||||
style={{ minHeight: "38px", position: "relative" }}
|
|
||||||
>
|
|
||||||
<div className="d-flex flex-wrap align-items-center gap-1">
|
<div className="d-flex flex-wrap align-items-center gap-1">
|
||||||
{tags.map((tag, index) => (
|
{tags.map((tag, index) => (
|
||||||
<span
|
<span
|
||||||
@ -135,4 +126,5 @@ const TagInput = ({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export default TagInput;
|
export default TagInput;
|
||||||
|
|||||||
@ -59,6 +59,7 @@ export const useUploadDocument =(onSuccessCallBack)=>{
|
|||||||
mutationFn:async(DocumentPayload)=>DocumentRepository.uploadDocument(DocumentPayload),
|
mutationFn:async(DocumentPayload)=>DocumentRepository.uploadDocument(DocumentPayload),
|
||||||
onSuccess:(data,variables)=>{
|
onSuccess:(data,variables)=>{
|
||||||
queryClient.invalidateQueries({queryKey:["DocumentList"]});
|
queryClient.invalidateQueries({queryKey:["DocumentList"]});
|
||||||
|
|
||||||
if(onSuccessCallBack) onSuccessCallBack()
|
if(onSuccessCallBack) onSuccessCallBack()
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
@ -75,7 +76,9 @@ export const useUpdateDocument =(onSuccessCallBack)=>{
|
|||||||
return useMutation(({
|
return useMutation(({
|
||||||
mutationFn:async({documentId,DocumentPayload})=>DocumentRepository.UpdateDocument(documentId,DocumentPayload),
|
mutationFn:async({documentId,DocumentPayload})=>DocumentRepository.UpdateDocument(documentId,DocumentPayload),
|
||||||
onSuccess:(data,variables)=>{
|
onSuccess:(data,variables)=>{
|
||||||
|
const {documentId} = variables;
|
||||||
queryClient.invalidateQueries({queryKey:["DocumentList"]});
|
queryClient.invalidateQueries({queryKey:["DocumentList"]});
|
||||||
|
queryClient.invalidateQueries({queryKey:["Document",documentId]})
|
||||||
if(onSuccessCallBack) onSuccessCallBack()
|
if(onSuccessCallBack) onSuccessCallBack()
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user