Keep checkboxes on AskDocument page
This commit is contained in:
parent
d2004b6308
commit
703942df4a
@ -15,6 +15,7 @@ type IProps = {
|
|||||||
isCreateDocumentModalVisible: boolean;
|
isCreateDocumentModalVisible: boolean;
|
||||||
closeModal: () => void;
|
closeModal: () => void;
|
||||||
folder: OfficeFolder;
|
folder: OfficeFolder;
|
||||||
|
onDocumentAdded: () => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function ParameterDocuments(props: IProps) {
|
export default function ParameterDocuments(props: IProps) {
|
||||||
@ -64,14 +65,12 @@ export default function ParameterDocuments(props: IProps) {
|
|||||||
}, [props]);
|
}, [props]);
|
||||||
|
|
||||||
const addDocument = useCallback(async () => {
|
const addDocument = useCallback(async () => {
|
||||||
if (addOrEditDocument === "add") {
|
try {
|
||||||
try {
|
if (addOrEditDocument === "add") {
|
||||||
const documentType = await DocumentTypes.getInstance().post({
|
const documentType = await DocumentTypes.getInstance().post({
|
||||||
name: documentName,
|
name: documentName,
|
||||||
private_description: visibleDescription,
|
private_description: visibleDescription,
|
||||||
office: {
|
office: { uid: props.folder.office!.uid! },
|
||||||
uid: props.folder.office!.uid!,
|
|
||||||
},
|
|
||||||
public_description: visibleDescription,
|
public_description: visibleDescription,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -79,15 +78,7 @@ export default function ParameterDocuments(props: IProps) {
|
|||||||
await Deeds.getInstance().put(props.folder.deed?.uid!, {
|
await Deeds.getInstance().put(props.folder.deed?.uid!, {
|
||||||
document_types: [...oldDocumentsType, documentType],
|
document_types: [...oldDocumentsType, documentType],
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
//await this.loadData();
|
|
||||||
handleClose();
|
|
||||||
window.location.reload();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
const oldDocumentsType = props.folder.deed?.document_types!;
|
const oldDocumentsType = props.folder.deed?.document_types!;
|
||||||
await Deeds.getInstance().put(props.folder.deed?.uid!, {
|
await Deeds.getInstance().put(props.folder.deed?.uid!, {
|
||||||
document_types: [
|
document_types: [
|
||||||
@ -95,13 +86,12 @@ export default function ParameterDocuments(props: IProps) {
|
|||||||
...selectedDocuments.map((document) => DocumentType.hydrate<DocumentType>({ uid: document.id as string })),
|
...selectedDocuments.map((document) => DocumentType.hydrate<DocumentType>({ uid: document.id as string })),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
//await this.loadData();
|
|
||||||
handleClose();
|
|
||||||
window.location.reload();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await props.onDocumentAdded(); // ✅ Refresh AskDocuments state with the new document
|
||||||
|
handleClose(); // ✅ Close modal
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
}
|
}
|
||||||
}, [addOrEditDocument, documentName, handleClose, props, selectedDocuments, visibleDescription]);
|
}, [addOrEditDocument, documentName, handleClose, props, selectedDocuments, visibleDescription]);
|
||||||
|
|
||||||
|
@ -22,30 +22,19 @@ export default function AskDocuments() {
|
|||||||
const [isCreateDocumentModalVisible, setIsCreateDocumentModalVisible] = useState<boolean>(false);
|
const [isCreateDocumentModalVisible, setIsCreateDocumentModalVisible] = useState<boolean>(false);
|
||||||
const [documentTypes, setDocumentTypes] = useState<IOption[]>([]);
|
const [documentTypes, setDocumentTypes] = useState<IOption[]>([]);
|
||||||
const [folder, setFolder] = useState<OfficeFolder | null>(null);
|
const [folder, setFolder] = useState<OfficeFolder | null>(null);
|
||||||
|
const [selectedDocuments, setSelectedDocuments] = useState<string[]>([]);
|
||||||
|
|
||||||
const closeModal = () => setIsCreateDocumentModalVisible(false);
|
const closeModal = () => setIsCreateDocumentModalVisible(false);
|
||||||
const openModal = () => setIsCreateDocumentModalVisible(true);
|
const openModal = () => setIsCreateDocumentModalVisible(true);
|
||||||
|
|
||||||
const onFormSubmit = useCallback(
|
const onFormSubmit = useCallback(
|
||||||
async (
|
async (e: React.FormEvent<HTMLFormElement> | null) => {
|
||||||
e: React.FormEvent<HTMLFormElement> | null,
|
|
||||||
values: {
|
|
||||||
[key: string]: any;
|
|
||||||
},
|
|
||||||
) => {
|
|
||||||
try {
|
try {
|
||||||
const documentAsked: [] = values["document_types"] as [];
|
for (let i = 0; i < selectedDocuments.length; i++) {
|
||||||
for (let i = 0; i < documentAsked.length; i++) {
|
|
||||||
await Documents.getInstance().post({
|
await Documents.getInstance().post({
|
||||||
folder: {
|
folder: { uid: folderUid },
|
||||||
uid: folderUid,
|
depositor: { uid: customerUid },
|
||||||
},
|
document_type: { uid: selectedDocuments[i] },
|
||||||
depositor: {
|
|
||||||
uid: customerUid,
|
|
||||||
},
|
|
||||||
document_type: {
|
|
||||||
uid: documentAsked[i],
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +47,7 @@ export default function AskDocuments() {
|
|||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[customerUid, folderUid, router],
|
[selectedDocuments, customerUid, folderUid, router],
|
||||||
);
|
);
|
||||||
|
|
||||||
const getAvailableDocuments = useCallback(
|
const getAvailableDocuments = useCallback(
|
||||||
@ -122,6 +111,30 @@ export default function AskDocuments() {
|
|||||||
}
|
}
|
||||||
}, [folderUid, getAvailableDocuments]);
|
}, [folderUid, getAvailableDocuments]);
|
||||||
|
|
||||||
|
const handleDocumentAdded = async () => {
|
||||||
|
if (!folder) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Fetch updated folder data to get the new document
|
||||||
|
const updatedFolder = await Folders.getInstance().getByUid(folderUid as string, {
|
||||||
|
q: {
|
||||||
|
deed: { include: { document_types: true } },
|
||||||
|
office: true,
|
||||||
|
documents: { include: { depositor: true, document_type: true } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!updatedFolder) return;
|
||||||
|
|
||||||
|
setFolder(updatedFolder); // ✅ Update the folder state
|
||||||
|
setDocumentTypes(await getAvailableDocuments(updatedFolder)); // ✅ Refresh the document list
|
||||||
|
|
||||||
|
console.log("Document list updated with new document:", updatedFolder);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to refresh document list:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadData();
|
loadData();
|
||||||
}, [loadData]);
|
}, [loadData]);
|
||||||
@ -147,6 +160,13 @@ export default function AskDocuments() {
|
|||||||
toolTip={documentType.description}
|
toolTip={documentType.description}
|
||||||
option={documentType}
|
option={documentType}
|
||||||
key={documentType.value as string}
|
key={documentType.value as string}
|
||||||
|
checked={selectedDocuments.includes(documentType.value as string)}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = documentType.value as string;
|
||||||
|
setSelectedDocuments((prev) =>
|
||||||
|
e.target.checked ? [...prev, value] : prev.filter((id) => id !== value),
|
||||||
|
);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -175,7 +195,12 @@ export default function AskDocuments() {
|
|||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
{folder && (
|
{folder && (
|
||||||
<ParameterDocuments folder={folder} closeModal={closeModal} isCreateDocumentModalVisible={isCreateDocumentModalVisible} />
|
<ParameterDocuments
|
||||||
|
folder={folder}
|
||||||
|
closeModal={closeModal}
|
||||||
|
isCreateDocumentModalVisible={isCreateDocumentModalVisible}
|
||||||
|
onDocumentAdded={handleDocumentAdded}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</DefaultDoubleSidePage>
|
</DefaultDoubleSidePage>
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user