Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/new btn response #227

Merged
merged 8 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions apps/amakrushi/src/components/chat-message-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ const ChatMessageItem: FC<ChatMessageItemPropType> = ({ message, onSend }) => {
);
const getLists = useCallback(
({ choices, isWeather = false }: { choices: any, isWeather: Boolean }) => {
console.log('qwer12:', { choices, optionDisabled });
return (
<List className={`${styles.list}`}>
{choices?.map((choice: any, index: string) => (
Expand All @@ -160,12 +159,15 @@ const ChatMessageItem: FC<ChatMessageItemPropType> = ({ message, onSend }) => {
console.log('clearing chat');
context?.setMessages([]);
}
context?.sendMessage(choice);
if (isWeather)
context?.sendMessage(choice?.textInEnglish, false, true, choice);
else
context?.sendMessage(choice);
setOptionDisabled(true);
if (isWeather)
setTimeout(() => {
setOptionDisabled(false);
}, 7000)
}, 4000)
}
}}>
<div
Expand All @@ -180,7 +182,7 @@ const ChatMessageItem: FC<ChatMessageItemPropType> = ({ message, onSend }) => {
? 'var(--font)'
: 'var(--secondarygreen)',
}}>
<div>{choice}</div>
<div>{isWeather ? choice?.textInEnglish : choice}</div>
<div style={{ marginLeft: 'auto' }}>
<RightIcon
width="30px"
Expand Down Expand Up @@ -701,7 +703,7 @@ const ChatMessageItem: FC<ChatMessageItemPropType> = ({ message, onSend }) => {
`\n\n` +
t('message.options')}
{getLists({
choices: JSON.parse(content?.text)?.crops,
choices: JSON.parse(content?.text)?.buttons,
isWeather: true
})}
</span>
Expand Down
6 changes: 5 additions & 1 deletion apps/amakrushi/src/context/ContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ const ContextProvider: FC<{

//@ts-ignore
const sendMessage = useCallback(
(text: string, media: any, isVisibile = true): void => {
(text: string, media: any, isVisibile = true, selectedButton: any): void => {
if (!sessionStorage.getItem('conversationId')) {
const cId = uuidv4();
console.log('convId', cId);
Expand Down Expand Up @@ -408,10 +408,14 @@ const ContextProvider: FC<{
longitude: sessionStorage.getItem('longitude'),
city: sessionStorage.getItem('city'),
state: sessionStorage.getItem('state'),
subDistrict: sessionStorage.getItem('subDistrict'),
village: sessionStorage.getItem('village'),
captureMode: sessionStorage.getItem('captureMode'),
ip: sessionStorage.getItem('ip'),
asrId: sessionStorage.getItem('asrId'),
userId: localStorage.getItem('userID'),
conversationId: sessionStorage.getItem('conversationId'),
selectedButton: selectedButton || null
}
});
setStartTime(Date.now());
Expand Down
61 changes: 44 additions & 17 deletions apps/amakrushi/src/utils/location.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
import { logEvent } from 'firebase/analytics';
import { toast } from 'react-hot-toast';
import { analytics } from './firebase';

export async function recordUserLocation() {
let lat = 0, long = 0;

async function saveUserLocation(position: any) {
// Capturing user location through GPS
sessionStorage.setItem('latitude', position.coords.latitude);
sessionStorage.setItem('longitude', position.coords.longitude);
lat = position.coords.latitude;
long = position.coords.longitude;
}

try {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(saveUserLocation);
}

// Fetching user's ip
let apiRes: any = await fetch('https://api.ipify.org?format=json');

apiRes = await apiRes.json();

if (apiRes?.ip) {
navigator.permissions.query({ name: 'geolocation' }).then(async (res: any) => {

let locationRes: any = await fetch(`https://geoip.samagra.io/city/${apiRes.ip}`);
locationRes = await locationRes.json();
sessionStorage.setItem('city', locationRes.city);
sessionStorage.setItem('state', locationRes.regionName);
sessionStorage.setItem('ip', apiRes?.ip);

if (res.state != 'granted') {
navigator.permissions.query({ name: 'geolocation' }).then(async (res: any) => {
// If user doesn't grant gps permission
if (res.state != 'granted') {
if (apiRes?.ip) {
let locationRes: any = await fetch(`https://geoip.samagra.io/city/${apiRes.ip}`);
locationRes = await locationRes.json();
let latLongRes: any = await fetch(`https://geoip.samagra.io/georev?lat=${locationRes?.lat}&lon=${locationRes?.lon}`);
latLongRes = await latLongRes.json();
//@ts-ignore
logEvent(analytics, 'location_captured_through_ip');
sessionStorage.setItem('city', latLongRes?.district);
sessionStorage.setItem('state', latLongRes?.state);
sessionStorage.setItem('subDistrict', latLongRes?.subDistrict)
sessionStorage.setItem('village', latLongRes?.village || '')
sessionStorage.setItem('ip', apiRes?.ip);
sessionStorage.setItem('latitude', locationRes.lat);
sessionStorage.setItem('longitude', locationRes.lon);
sessionStorage.setItem('captureMode', 'ip');
}
})
}
} else {
// If user has provided geolocation access then
let locationRes: any = await fetch(`https://geoip.samagra.io/georev?lat=${lat || sessionStorage.getItem('latitude')}&lon=${long || sessionStorage.getItem('longitude')}`);
//@ts-ignore
logEvent(analytics, 'location_captured_through_gps');
locationRes = await locationRes.json();
sessionStorage.setItem('city', locationRes?.district);
sessionStorage.setItem('state', locationRes?.state);
sessionStorage.setItem('subDistrict', locationRes?.subDistrict)
sessionStorage.setItem('village', locationRes?.village || '')
sessionStorage.setItem('ip', apiRes?.ip);
sessionStorage.setItem('captureMode', 'gps');
}

})
} catch (err) {
console.log(err)
}
}

function saveUserLocation(position: any) {
sessionStorage.setItem('latitude', position.coords.latitude);
sessionStorage.setItem('longitude', position.coords.longitude);
}

Loading