Skip to content

Commit

Permalink
Added location capturing
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-s19 committed Jan 12, 2024
1 parent 4b13903 commit da09ee0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 54 deletions.
2 changes: 1 addition & 1 deletion apps/amakrushi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.0",
"react-toastify": "^9.0.5",
"socket-package": "0.13.0",
"socket-package": "0.14.0",
"socket.io-client": "^4.5.1",
"typescript": "^4.9.3",
"underscore": "^1.13.6",
Expand Down
74 changes: 37 additions & 37 deletions apps/amakrushi/src/components/ChatWindow/ChatUiWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import shareIcon from '../../assets/icons/share.svg';
import downloadIcon from '../../assets/icons/download.svg';
import Image from 'next/image';
import Draggable from 'react-draggable'
import { recordUserLocation } from '../../utils/location';

const ChatUiWindow: React.FC = () => {
const t = useLocalization();
Expand All @@ -34,8 +35,7 @@ const ChatUiWindow: React.FC = () => {
await context?.fetchIsDown();
if (!context?.isDown) {
const chatHistory = await axios.get(
`${
process.env.NEXT_PUBLIC_BASE_URL
`${process.env.NEXT_PUBLIC_BASE_URL
}/user/chathistory/${sessionStorage.getItem('conversationId')}`,
{
headers: {
Expand Down Expand Up @@ -68,6 +68,7 @@ const ChatUiWindow: React.FC = () => {
});
}
};
recordUserLocation();
!context?.loading && fetchData();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [context?.setMessages, context?.fetchIsDown, context?.isDown]);
Expand Down Expand Up @@ -132,13 +133,13 @@ const ChatUiWindow: React.FC = () => {
const msgToRender = useMemo(() => {
return context?.isMsgReceiving
? [
...normalizeMsgs,
{
type: 'loader',
position: 'left',
botUuid: '1',
},
]
...normalizeMsgs,
{
type: 'loader',
position: 'left',
botUuid: '1',
},
]
: normalizeMsgs;
}, [context?.isMsgReceiving, normalizeMsgs]);

Expand All @@ -148,11 +149,10 @@ const ChatUiWindow: React.FC = () => {

const downloadShareHandler = async (type: string) => {
try {
const url = `${
process.env.NEXT_PUBLIC_BASE_URL
}/user/chathistory/generate-pdf/${sessionStorage.getItem(
'conversationId'
)}`;
const url = `${process.env.NEXT_PUBLIC_BASE_URL
}/user/chathistory/generate-pdf/${sessionStorage.getItem(
'conversationId'
)}`;

const response = await axios.post(url, null, {
headers: {
Expand Down Expand Up @@ -191,10 +191,10 @@ const ChatUiWindow: React.FC = () => {

if (!navigator.canShare) {
//@ts-ignore
if(window?.AndroidHandler?.shareUrl){
if (window?.AndroidHandler?.shareUrl) {
//@ts-ignore
window.AndroidHandler.shareUrl(pdfUrl);
}else{
} else {
context?.shareChat(pdfUrl);
}
} else if (navigator.canShare({ files: [file] })) {
Expand Down Expand Up @@ -262,30 +262,30 @@ const ChatUiWindow: React.FC = () => {
placeholder={placeholder}
/>
<Draggable axis="y">
<div
style={{
position: 'absolute',
right: 0,
top: '40%',
background: 'white',
padding: '5px',
borderRadius: '5px 0 0 5px',
boxShadow: 'rgba(50, 50, 93, 0.25) 0px 6px 12px -2px, rgba(0, 0, 0, 0.3) 0px 3px 7px -3px'
}}>
<div onClick={() => downloadShareHandler('share')}>
{/* Share */}
<Image src={shareIcon} alt="" width={24} height={24} />
</div>
<div
style={{
borderBottom: '1px solid var(--secondarygreen)',
margin: '5px 0',
}}></div>
<div onClick={() => downloadShareHandler('download')}>
{/* Download */}
<Image src={downloadIcon} alt="" width={24} height={24} />
position: 'absolute',
right: 0,
top: '40%',
background: 'white',
padding: '5px',
borderRadius: '5px 0 0 5px',
boxShadow: 'rgba(50, 50, 93, 0.25) 0px 6px 12px -2px, rgba(0, 0, 0, 0.3) 0px 3px 7px -3px'
}}>
<div onClick={() => downloadShareHandler('share')}>
{/* Share */}
<Image src={shareIcon} alt="" width={24} height={24} />
</div>
<div
style={{
borderBottom: '1px solid var(--secondarygreen)',
margin: '5px 0',
}}></div>
<div onClick={() => downloadShareHandler('download')}>
{/* Download */}
<Image src={downloadIcon} alt="" width={24} height={24} />
</div>
</div>
</div>
</Draggable>
</div>
);
Expand Down
11 changes: 7 additions & 4 deletions apps/amakrushi/src/components/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { v4 as uuidv4 } from 'uuid';
import RenderVoiceRecorder from '../recorder/RenderVoiceRecorder';
import { useFlags } from 'flagsmith/react';
import DownTimePage from '../down-time-page';
import { recordUserLocation } from '../../utils/location';

const HomePage: NextPage = () => {
const context = useContext(AppContext);
Expand Down Expand Up @@ -197,6 +198,9 @@ const HomePage: NextPage = () => {
sessionStorage.setItem('conversationId', newConversationId);
context?.setConversationId(newConversationId);
}
recordUserLocation();


// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down Expand Up @@ -391,9 +395,8 @@ const HomePage: NextPage = () => {
<div
key={index}
onClick={() => suggestionClickHandler(elem)}
className={`${styles.suggestion} ${
activeSuggestion === index ? styles.active : ''
}`}
className={`${styles.suggestion} ${activeSuggestion === index ? styles.active : ''
}`}
onMouseEnter={(e) => suggestionHandler(e, index)}>
{elem}
</div>
Expand All @@ -411,7 +414,7 @@ const HomePage: NextPage = () => {
<button
type="submit"
className={styles.sendButton}>
<Image src={SendIcon} width={50} height={50} alt="sendIcon" onClick={() => sendMessage(inputMsg)}/>
<Image src={SendIcon} width={50} height={50} alt="sendIcon" onClick={() => sendMessage(inputMsg)} />
</button>
</div>
</form>
Expand Down
18 changes: 11 additions & 7 deletions apps/amakrushi/src/context/ContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const ContextProvider: FC<{
.get(
`${process.env.NEXT_PUBLIC_BASE_URL}/incrementaudioused/${content?.data?.messageId}`
)
.then((res) => {})
.then((res) => { })
.catch((err) => {
console.log(err);
});
Expand Down Expand Up @@ -381,14 +381,18 @@ const ContextProvider: FC<{
newSocket.sendMessage({
text: text,
to: localStorage.getItem('userID'),
from: localStorage.getItem('phoneNumber'),
optional: {
payload: {
from: localStorage.getItem('phoneNumber'),
appId: 'AKAI_App_Id',
channel: 'AKAI',
},
asrId: sessionStorage.getItem('asrId'),
userId: localStorage.getItem('userID'),
conversationId: sessionStorage.getItem('conversationId'),
latitude: sessionStorage.getItem('latitude'),
longitude: sessionStorage.getItem('longitude'),
city: sessionStorage.getItem('city'),
state: sessionStorage.getItem('state'),
asrId: sessionStorage.getItem('asrId'),
userId: localStorage.getItem('userID'),
conversationId: sessionStorage.getItem('conversationId')
}
});
setStartTime(Date.now());
if (isVisibile)
Expand Down
24 changes: 19 additions & 5 deletions apps/amakrushi/src/utils/location.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { toast } from 'react-hot-toast';

export function recordUserLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(saveUserLocation);
export async function recordUserLocation() {
try {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(saveUserLocation);
}
let apiRes: any = await fetch('https://api.ipify.org?format=json');
apiRes = await apiRes.json();
if (apiRes?.ip) {
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);
}
} catch (err) {
console.log(err)
toast.error('Unable to record user location')
}
}

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

0 comments on commit da09ee0

Please sign in to comment.