Skip to content

Commit

Permalink
CORS issue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
vijay-murugeasan committed Mar 22, 2024
1 parent d210a79 commit 0207a63
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/assets/css/order-sign_up.css
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
animation: checkmark-circle 0.6s ease-in-out backwards;
}
#register_bg {
background: #ccc url(/img/Lunch1.jpg) center center no-repeat fixed;
background: #ccc url(../img/Lunch1.jpg) center center no-repeat fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
Expand Down
20 changes: 11 additions & 9 deletions src/pages/AutoCompleteLocation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ function AutocompleteLocation() {
signal: controller.signal,
});
const res = await data.json();
const result = JSON.parse(res.contents);
myOptions.length = 0;
for (var i = 0; i < res.data.length; i++) {
for (var i = 0; i < result.data.length; i++) {
myOptions.push({
label: res.data[i].description,
id: res.data[i].place_id,
label: result.data[i].description,
id: result.data[i].place_id,
});
}
// console.log(res?.data);
Expand All @@ -51,13 +52,14 @@ function AutocompleteLocation() {
async function getGeoCode(id) {
const data = await fetch(place_ID + id);
const res = await data.json();
// console.log(res);
const result = JSON.parse(res.contents);

select.length = 0;
for (var i = 0; i < res.data.length; i++) {
for (var i = 0; i < result.data.length; i++) {
select.push({
label: res.data[i].geometry.location,
id: res.data[i].place_id,
address: res.data[i].formatted_address,
label: result.data[i].geometry.location,
id: result.data[i].place_id,
address: result.data[i].formatted_address,
});
}
// console.log(res?.data);
Expand Down Expand Up @@ -104,7 +106,7 @@ function AutocompleteLocation() {
}
}}
onChange={(e, value) => {
// console.log(value);
console.log(value);
getGeoCode(value?.id);
}}
getOptionLabel={(option) =>
Expand Down
19 changes: 11 additions & 8 deletions src/services/apiRestaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ export async function getRestaurants() {
if (!data.ok) throw Error();

const json = await data.json();
const result = JSON.parse(json.contents);

// was showing an error of data fetching because sometime data coming from cards[1] sometime cards[2] and different on other times so me make a function and check which value of i gives data in cards[i]
async function checkJsonData(jsonData) {
for (let i = 0; i < jsonData?.data?.cards.length; i++) {
// initialize checkData for Swiggy Restaurant data
let checkData = json?.data?.cards[i]?.card?.card?.gridElements?.infoWithStyle?.restaurants;
let checkData = result?.data?.cards[i]?.card?.card?.gridElements?.infoWithStyle?.restaurants;
// if checkData is not undefined then return it
if (checkData !== undefined) {
return checkData;
}
}
}
// call the checkJsonData() function which return Swiggy Restaurant data
const resData = await checkJsonData(json);
// console.log(resData)
const resData = await checkJsonData(result);
return resData
} catch (error) {
console.log(error);
Expand All @@ -54,19 +54,20 @@ export async function getCategories() {
if (!data.ok) throw Error();

const json = await data.json();
const result = JSON.parse(json.contents);

async function checkJsonData(jsonData) {
for (let i = 0; i < jsonData?.data?.cards.length; i++) {
// initialize checkData for Swiggy Restaurant data
if (json?.data?.cards[i]?.card?.card?.id === 'whats_on_your_mind') {
const checkData = json?.data?.cards[i]?.card?.card?.gridElements?.infoWithStyle?.info;
if (result?.data?.cards[i]?.card?.card?.id === 'whats_on_your_mind') {
const checkData = result?.data?.cards[i]?.card?.card?.gridElements?.infoWithStyle?.info;
return (checkData !== undefined) ? checkData : [];
}
}
}

// call the checkJsonData() function which return Swiggy Restaurant data
const itemData = await checkJsonData(json);
const itemData = await checkJsonData(result);
// console.log(itemData)
return itemData
} catch (error) {
Expand All @@ -83,6 +84,7 @@ export async function UseGetPlace(url) {

try {
const data = await fetch(search_API_URL + url);
console.log('data', data)
if (!data.ok) throw Error();

const json = await data.json();
Expand Down Expand Up @@ -113,17 +115,18 @@ export const UseRestaurant = (resId) => {
throw new Error(err);
} else {
const json = await response.json();
const result = JSON.parse(json.contents);
// Set restaurant data
const restaurantData =
json?.data?.cards
result?.data?.cards
?.map((x) => x.card)
?.find((x) => x && x.card["@type"] === RESTAURANT_TYPE_KEY)?.card
?.info || null;
setRestaurant(restaurantData)

// Set menu item data
const menuItemsData =
json?.data?.cards
result?.data?.cards
.find((x) => x.groupedCard)
?.groupedCard?.cardGroupMap?.REGULAR?.cards?.map(
(x) => x.card?.card
Expand Down
27 changes: 14 additions & 13 deletions src/services/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ export const MENU_ITEM_TYPE_KEY =
export const RESTAURANT_TYPE_KEY =
"type.googleapis.com/swiggy.presentation.food.v2.Restaurant";


export const apiId = 'https://api.allorigins.win/get?url=';
// const lat = '10.811115299341283'
// const lng = '78.70747714418124'


// console.log('menu api')
export const search_API_URL = `https://corsproxy.org/?https%3A%2F%2Fwww.swiggy.com%2Fdapi%2Fmisc%2Fplace-autocomplete%3Finput%3D`;
export const place_ID = `https://corsproxy.org/?https%3A%2F%2Fwww.swiggy.com%2Fdapi%2Fmisc%2Faddress-recommend%3Fplace_id%3D`
export const MENU_API = (lat, lng) =>
`https://corsproxy.org/?https%3A%2F%2Fwww.swiggy.com%2Fdapi%2Frestaurants%2Flist%2Fv5%3Flat%3D${lat}%26lng%3D${lng}%26is-seo-homepage-enabled%3Dtrue%26page_type%3DDESKTOP_WEB_LISTING`
export const search_API_URL = `${apiId}https%3A%2F%2Fwww.swiggy.com%2Fdapi%2Fmisc%2Fplace-autocomplete%3Finput%3D`;
export const place_ID = `${apiId}https%3A%2F%2Fwww.swiggy.com%2Fdapi%2Fmisc%2Faddress-recommend%3Fplace_id%3D`

// "https://corsproxy.org/?https://www.swiggy.com/dapi/restaurants/list/v5?lat=17.486463086305346&lng=78.3657343313098&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING"

// "https://cors-anywhere.herokuapp.com/https://www.swiggy.com/dapi/restaurants/list/v5?lat=12.853575&lng=80.068588&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING"
export const MENU_API = (lat, lng) =>
`${apiId}https%3A%2F%2Fwww.swiggy.com%2Fdapi%2Frestaurants%2Flist%2Fv5%3Flat%3D${lat}%26lng%3D${lng}%26is-seo-homepage-enabled%3Dtrue%26page_type%3DDESKTOP_WEB_LISTING`



export const swiggy_menu_api_URL = (lat, lng) =>

`https://corsproxy.org/?https%3A%2F%2Fwww.swiggy.com%2Fdapi%2Fmenu%2Fpl%3Fpage-type%3DREGULAR_MENU%26complete-menu%3Dtrue%26lat%3D${lat}%26lng%3D${lng}%26restaurantId%3D`

// "http://www.swiggy.com/dapi/menu/pl?page-type=REGULAR_MENU&complete-menu=true&lat=21.1702401&lng=72.83106070000001&&submitAction=ENTER&restaurantId=";


`${apiId}https%3A%2F%2Fwww.swiggy.com%2Fdapi%2Fmenu%2Fpl%3Fpage-type%3DREGULAR_MENU%26complete-menu%3Dtrue%26lat%3D${lat}%26lng%3D${lng}%26restaurantId%3D`


/**
* "https://www.swiggy.com/dapi/restaurants/list/v5?lat=17.486463086305346&lng=78.3657343313098&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING"
"https://www.swiggy.com/dapi/restaurants/list/v5?lat=10.8109783&lng=78.7074235&page_type=DESKTOP_WEB_LISTING";
"https://www.swiggy.com/dapi/restaurants/list/v5?lat=30.29844139999999&lng=77.99313599999999&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING";
// "https://corsproxy.org/?https://www.swiggy.com/dapi/restaurants/list/v5?lat=17.486463086305346&lng=78.3657343313098&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING"
// "https://cors-anywhere.herokuapp.com/https://www.swiggy.com/dapi/restaurants/list/v5?lat=12.853575&lng=80.068588&is-seo-homepage-enabled=true&page_type=DESKTOP_WEB_LISTING"
// "http://www.swiggy.com/dapi/menu/pl?page-type=REGULAR_MENU&complete-menu=true&lat=21.1702401&lng=72.83106070000001&&submitAction=ENTER&restaurantId=";
*/

0 comments on commit 0207a63

Please sign in to comment.