added update contact modal
This commit is contained in:
parent
42b769c1b5
commit
d10cbdd4bd
@ -8,35 +8,49 @@ import { useDirectory } from "../../hooks/useDirectory";
|
|||||||
import { DirectoryRepository } from "../../repositories/DirectoryRepository";
|
import { DirectoryRepository } from "../../repositories/DirectoryRepository";
|
||||||
import { getCachedData } from "../../slices/apiDataManager";
|
import { getCachedData } from "../../slices/apiDataManager";
|
||||||
import showToast from "../../services/toastService";
|
import showToast from "../../services/toastService";
|
||||||
|
import UpdateContact from "../../components/Directory/UpdateContact";
|
||||||
|
|
||||||
const Directory = () => {
|
const Directory = () => {
|
||||||
const [isOpenModal, setIsOpenModal] = useState(false);
|
const [isOpenModal, setIsOpenModal] = useState(false);
|
||||||
const closedModel = () => setIsOpenModal(false);
|
const [selectedContact, setSelectedContact] = useState(null);
|
||||||
const [ContatList, setContactList] = useState([]);
|
const [ContatList, setContactList] = useState([]);
|
||||||
|
|
||||||
const { contacts, loading } = useDirectory();
|
const { contacts, loading } = useDirectory();
|
||||||
const submitContact = async (data, setLoading, reset) => {
|
const submitContact = async (data) => {
|
||||||
try
|
try {
|
||||||
{
|
let response;
|
||||||
debugger
|
let updatedContacts;
|
||||||
const resp = await DirectoryRepository.CreateContact(data)
|
|
||||||
const contacts_cache = getCachedData("contacts") || [];
|
const contacts_cache = getCachedData("contacts") || [];
|
||||||
const updated_contacts = [...contacts_cache, resp.data];
|
|
||||||
|
|
||||||
setContactList(updated_contacts);
|
if (selectedContact) {
|
||||||
showToast(resp.message || "Contact created successfully", "success");
|
setSelectedContact(null);
|
||||||
setLoading(false);
|
response = await DirectoryRepository.UpdateContact(data.id, data);
|
||||||
reset();
|
updatedContacts = contacts_cache.map((contact) =>
|
||||||
setIsOpenModal(false);
|
contact.id === data.id ? response.data : contact
|
||||||
|
);
|
||||||
|
showToast("Contact updated successfully", "success");
|
||||||
|
setIsOpenModal(false);
|
||||||
|
} else {
|
||||||
|
response = await DirectoryRepository.CreateContact(data);
|
||||||
|
updatedContacts = [...contacts_cache, response.data];
|
||||||
|
showToast("Contact created successfully", "success");
|
||||||
|
setIsOpenModal(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
setContactList(updatedContacts);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const msg =
|
const msg =
|
||||||
error.response.data.message ||
|
error.response?.data?.message ||
|
||||||
error.message ||
|
error.message ||
|
||||||
"Error Occured during api calling !";
|
"Error occurred during API call!";
|
||||||
showToast(msg, "error");
|
showToast(msg, "error");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const closedModel = () => {
|
||||||
|
setIsOpenModal(false);
|
||||||
|
setSelectedContact(null);
|
||||||
|
};
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setContactList(contacts);
|
setContactList(contacts);
|
||||||
}, [contacts]);
|
}, [contacts]);
|
||||||
@ -50,11 +64,23 @@ const Directory = () => {
|
|||||||
></Breadcrumb>
|
></Breadcrumb>
|
||||||
|
|
||||||
{isOpenModal && (
|
{isOpenModal && (
|
||||||
<GlobalModel isOpen={isOpenModal} closeModal={()=>setIsOpenModal(false)} size="lg">
|
<GlobalModel
|
||||||
<ManageDirectory
|
isOpen={isOpenModal}
|
||||||
submitContact={submitContact}
|
closeModal={() => setIsOpenModal(false)}
|
||||||
onCLosed={closedModel}
|
size="lg"
|
||||||
/>
|
>
|
||||||
|
{selectedContact ? (
|
||||||
|
<UpdateContact
|
||||||
|
existingContact={selectedContact}
|
||||||
|
submitContact={submitContact}
|
||||||
|
onCLosed={closedModel}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<ManageDirectory
|
||||||
|
submitContact={submitContact}
|
||||||
|
onCLosed={closedModel}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</GlobalModel>
|
</GlobalModel>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@ -93,7 +119,7 @@ const Directory = () => {
|
|||||||
<span>Name</span>
|
<span>Name</span>
|
||||||
</div>
|
</div>
|
||||||
</th>
|
</th>
|
||||||
<th className="px-2 text-start">
|
<th className="px-2 text-start">
|
||||||
<div className="d-flex text-center align-items-center gap-1 justify-content-start">
|
<div className="d-flex text-center align-items-center gap-1 justify-content-start">
|
||||||
<IconButton
|
<IconButton
|
||||||
size={12}
|
size={12}
|
||||||
@ -169,21 +195,22 @@ const Directory = () => {
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody className="table-border-bottom-0 overflow-auto ">
|
<tbody className="table-border-bottom-0 overflow-auto ">
|
||||||
{(loading && ContatList.length === 0) && (
|
{loading && ContatList.length === 0 && (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={10}>Loading...</td>
|
<td colSpan={10}>Loading...</td>
|
||||||
</tr>
|
</tr>
|
||||||
)}
|
)}
|
||||||
{(!loading && contacts.length == 0 && ContatList.length === 0) && (
|
{!loading && contacts.length == 0 && ContatList.length === 0 && (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={10}>No Contact Found</td>
|
<td colSpan={10}>No Contact Found</td>
|
||||||
</tr>
|
</tr>
|
||||||
)}
|
)}
|
||||||
{!loading &&
|
{!loading &&
|
||||||
ContatList.map((contact) => (
|
ContatList.map((contact) => (
|
||||||
<ListViewDirectory
|
<ListViewDirectory
|
||||||
contact={contact}
|
contact={contact}
|
||||||
submitContact={submitContact}
|
setSelectedContact={setSelectedContact}
|
||||||
|
setIsOpenModal={setIsOpenModal}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user