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

[TS migration] Migrate 'IFrame.js' component to TypeScript #32199

Merged
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
43 changes: 19 additions & 24 deletions src/components/IFrame.js → src/components/IFrame.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
/* eslint-disable es/no-nullish-coalescing-operators */
import PropTypes from 'prop-types';
import React, {useEffect, useState} from 'react';
import {withOnyx} from 'react-native-onyx';
import {OnyxEntry, withOnyx} from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import {Session} from '@src/types/onyx';

function getNewDotURL(url) {
type OldDotIFrameOnyxProps = {
session: OnyxEntry<Session>;
};

type OldDotIFrameProps = OldDotIFrameOnyxProps;

function getNewDotURL(url: string): string {
const urlObj = new URL(url);
const paramString = urlObj.searchParams.get('param') ?? '';
const pathname = urlObj.pathname.slice(1);

let params;
let params: Record<string, string>;
try {
params = JSON.parse(paramString);
} catch {
Expand Down Expand Up @@ -48,7 +53,7 @@ function getNewDotURL(url) {
return pathname;
}

function getOldDotURL(url) {
function getOldDotURL(url: string): string {
const urlObj = new URL(url);
const pathname = urlObj.pathname;
const paths = pathname.slice(1).split('/');
Expand Down Expand Up @@ -86,35 +91,27 @@ function getOldDotURL(url) {
return pathname;
}

const propTypes = {
// The session of the logged in person
session: PropTypes.shape({
// The email of the logged in person
email: PropTypes.string,

// The authToken of the logged in person
authToken: PropTypes.string,
}).isRequired,
};

function OldDotIFrame({session}) {
function OldDotIFrame({session}: OldDotIFrameProps) {
const [oldDotURL, setOldDotURL] = useState('https://staging.expensify.com');

useEffect(() => {
setOldDotURL(`https://expensify.com.dev/${getOldDotURL(window.location.href)}`);

window.addEventListener('message', (event) => {
window.addEventListener('message', (event: MessageEvent<string>) => {
const url = event.data;
// TODO: use this value to navigate to a new path
// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const newDotURL = getNewDotURL(url);
});
}, []);

useEffect(() => {
if (!session) {
return;
}
document.cookie = `authToken=${session.authToken}; domain=expensify.com.dev; path=/;`;
document.cookie = `email=${session.email}; domain=expensify.com.dev; path=/;`;
}, [session.authToken, session.email]);
}, [session]);

return (
<iframe
Expand All @@ -125,9 +122,7 @@ function OldDotIFrame({session}) {
);
}

OldDotIFrame.propTypes = propTypes;

export default withOnyx({
export default withOnyx<OldDotIFrameProps, OldDotIFrameOnyxProps>({
session: {
key: ONYXKEYS.SESSION,
},
Expand Down