Skip to content

Commit

Permalink
feat: added update postal address
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhishekPAnil committed Sep 22, 2023
1 parent f57f59a commit a706973
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 67 deletions.
182 changes: 116 additions & 66 deletions src/pages/Dashboard/CreateNewPlace/CreateNewPlace.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
import { urlProtocolCheck } from '../../../components/Input/Common/input.settings';
import { useAddImageMutation } from '../../../services/image';
import { usePrompt } from '../../../hooks/usePrompt';
import { useAddPostalAddressMutation } from '../../../services/postalAddress';
import { useAddPostalAddressMutation, useUpdatePostalAddressMutation } from '../../../services/postalAddress';
import PlacesAutocomplete, { geocodeByAddress, getLatLng } from 'react-places-autocomplete';

const { TextArea } = Input;
Expand Down Expand Up @@ -130,6 +130,7 @@ function CreateNewPlace() {
const [addPostalAddress] = useAddPostalAddressMutation();
const [getPlace] = useLazyGetPlaceQuery();
const [getAllTaxonomy] = useLazyGetAllTaxonomyQuery({ sessionId: timestampRef });
const [updatePostalAddress] = useUpdatePostalAddressMutation();

const reactQuillRefFr = useRef(null);
const reactQuillRefEn = useRef(null);
Expand All @@ -151,7 +152,7 @@ function CreateNewPlace() {

usePrompt(t('common.unsavedChanges'), showDialog);

const addUpdatePlaceApiHandler = (placeObj) => {
const addUpdatePlaceApiHandler = (placeObj, postalObj) => {
var promise = new Promise(function (resolve, reject) {
if (!placeId || placeId === '') {
if (artsDataId && artsData) {
Expand All @@ -167,52 +168,115 @@ function CreateNewPlace() {
sameAs: [artsData?.sameAs],
};
}
addPlace({
data: placeObj,
calendarId,
})
addPostalAddress({ data: postalObj, calendarId })
.unwrap()
.then((response) => {
resolve(response?.id);
notification.success({
description: t('dashboard.places.createNew.addPlace.notification.addSuccess'),
placement: 'top',
closeIcon: <></>,
maxCount: 1,
duration: 3,
});
navigate(`${PathName.Dashboard}/${calendarId}${PathName.Places}`);
if (response && response?.statusCode == 202) {
placeObj = {
...placeObj,
postalAddressId: {
entityId: response?.data?.id,
},
};
addPlace({
data: placeObj,
calendarId,
})
.unwrap()
.then((response) => {
resolve(response?.id);
notification.success({
description: t('dashboard.places.createNew.addPlace.notification.addSuccess'),
placement: 'top',
closeIcon: <></>,
maxCount: 1,
duration: 3,
});
navigate(`${PathName.Dashboard}/${calendarId}${PathName.Places}`);
})
.catch((errorInfo) => {
reject();
console.log(errorInfo);
});
}
})
.catch((errorInfo) => {
reject();
console.log(errorInfo);
});
.catch((error) => console.log(error));
} else {
placeObj = {
...placeObj,
sameAs: placeData?.sameAs,
};
updatePlace({
data: placeObj,
calendarId,
placeId,
})
.unwrap()
.then(() => {
resolve(placeId);
notification.success({
description: t('dashboard.places.createNew.addPlace.notification.editSuccess'),
placement: 'top',
closeIcon: <></>,
maxCount: 1,
duration: 3,
});
navigate(`${PathName.Dashboard}/${calendarId}${PathName.Places}`);
})
.catch((error) => {
reject();
console.log(error);
});
if (!placeData?.address) {
addPostalAddress({ data: postalObj, calendarId })
.unwrap()
.then((response) => {
if (response && response?.statusCode == 202) {
placeObj = {
...placeObj,
postalAddressId: {
entityId: response?.data?.id,
},
};
updatePlace({
data: placeObj,
calendarId,
placeId,
})
.unwrap()
.then(() => {
resolve(placeId);
notification.success({
description: t('dashboard.places.createNew.addPlace.notification.editSuccess'),
placement: 'top',
closeIcon: <></>,
maxCount: 1,
duration: 3,
});
navigate(`${PathName.Dashboard}/${calendarId}${PathName.Places}`);
})
.catch((error) => {
reject();
console.log(error);
});
}
})
.catch((error) => console.log(error));
} else {
updatePostalAddress({ data: postalObj, calendarId, id: placeData?.address?.id })
.unwrap()
.then((response) => {
if (response && response?.statusCode == 202) {
placeObj = {
...placeObj,
postalAddressId: {
entityId: placeData?.address?.id,
},
};
updatePlace({
data: placeObj,
calendarId,
placeId,
})
.unwrap()
.then(() => {
resolve(placeId);
notification.success({
description: t('dashboard.places.createNew.addPlace.notification.editSuccess'),
placement: 'top',
closeIcon: <></>,
maxCount: 1,
duration: 3,
});
navigate(`${PathName.Dashboard}/${calendarId}${PathName.Places}`);
})
.catch((error) => {
reject();
console.log(error);
});
}
})
.catch((error) => console.log(error));
}
}
});
return promise;
Expand Down Expand Up @@ -369,19 +433,12 @@ function CreateNewPlace() {
},
};
placeObj['image'] = imageCrop;
addPostalAddress({ data: postalObj, calendarId })
.unwrap()
.then((response) => {
if (response && response?.statusCode == 202) {
addUpdatePlaceApiHandler(placeObj)
.then((id) => resolve(id))
.catch((error) => {
reject();
console.log(error);
});
}
})
.catch((error) => console.log(error));
addUpdatePlaceApiHandler(placeObj, postalObj)
.then((id) => resolve(id))
.catch((error) => {
reject();
console.log(error);
});
})
.catch((error) => {
console.log(error);
Expand All @@ -394,19 +451,12 @@ function CreateNewPlace() {
else placeObj['image'] = imageCrop;
}

addPostalAddress({ data: postalObj, calendarId })
.unwrap()
.then((response) => {
if (response && response?.statusCode == 202) {
addUpdatePlaceApiHandler(placeObj)
.then((id) => resolve(id))
.catch((error) => {
reject();
console.log(error);
});
}
})
.catch((error) => console.log(error));
addUpdatePlaceApiHandler(placeObj, postalObj)
.then((id) => resolve(id))
.catch((error) => {
reject();
console.log(error);
});
}
})
.catch((error) => {
Expand Down
12 changes: 11 additions & 1 deletion src/services/postalAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ export const postalAddressApi = createApi({
body: data,
}),
}),
updatePostalAddress: builder.mutation({
query: ({ data, calendarId, id }) => ({
url: `postal-addresses/${id}`,
method: 'PATCH',
headers: {
'calendar-id': calendarId,
},
body: data,
}),
}),
}),
});

export const { useAddPostalAddressMutation } = postalAddressApi;
export const { useAddPostalAddressMutation, useUpdatePostalAddressMutation } = postalAddressApi;

0 comments on commit a706973

Please sign in to comment.