Skip to content

Commit

Permalink
MYB-2071 send message to RN (Blazity#32)
Browse files Browse the repository at this point in the history
* MYB-2071 send message to RN

* MYB-2071 send unauthorized error message to RN

* MYB-2071 change validation of window

* MYB-2071 test error

* MYB-2071 test error 2

* MYB-2071 test error 3

* MYB-2071 test error 4

* MYB-2071 test error 5
  • Loading branch information
pipenunezb authored Aug 30, 2024
1 parent 5c3e9eb commit e7bc014
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
14 changes: 12 additions & 2 deletions components/BackButton/BackButton.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
'use client';
import Image from 'next/image';
import { useRouter } from 'next/navigation';
import { useRouter, usePathname } from 'next/navigation';
import BackArrow from 'public/images/backArrow.svg';
import { sendMessageToRN } from 'utils/sendMessageToRN';

export const BackButton = () => {
const pathname = usePathname();
const router = useRouter();
return <Image onClick={router.back} priority src={BackArrow} alt="back button" />;

const handleClick = () => {
if (pathname === '/') {
sendMessageToRN('close-view');
} else {
router.back();
}
};
return <Image onClick={handleClick} priority src={BackArrow} alt="back button" />;
};
9 changes: 9 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare global {
interface Window {
ReactNativeWebView?: {
postMessage: (message: string) => void;
};
}
}

export {};
29 changes: 21 additions & 8 deletions lib/axiosClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import { sendMessageToRN } from 'utils/sendMessageToRN';

const apiClient = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
Expand All @@ -8,15 +9,27 @@ const apiClient = axios.create({
},
});

apiClient.interceptors.request.use(config => {
const token = localStorage.getItem('token')
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;
apiClient.interceptors.request.use(
(config) => {
const token = localStorage.getItem('token');
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;
}

return config;
},
(error) => {
return Promise.reject(error);
}
);

return config;
}, error => {
return Promise.reject(error);
});
apiClient.interceptors.response.use(
(response) => response,
(error) => {
if (error.response.status === 401) {
sendMessageToRN('unauthorized');
}
}
);

export default apiClient;
6 changes: 6 additions & 0 deletions utils/sendMessageToRN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function sendMessageToRN(text: string) {
if (typeof window === 'undefined') return;
if (window.ReactNativeWebView) {
window.ReactNativeWebView.postMessage(text);
}
}

0 comments on commit e7bc014

Please sign in to comment.