feat(client): ts-migrate /client/src/components/ redirects (#42642)

* rename

* migrate

* migrate create language redirect

* fix type of redirects

* migrate test

Co-authored-by: Parth Parth <thecodingaviator@users.noreply.github.com>
Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
This commit is contained in:
Parth Parth
2021-06-29 00:17:42 +05:30
committed by Mrugesh Mohapatra
parent e1e0fe02bf
commit 11d71adc6e
15 changed files with 44 additions and 27 deletions

View File

@ -27,7 +27,7 @@ import reallyWeirdErrorMessage from '../utils/reallyWeirdErrorMessage';
import { langCodes } from '../../../config/i18n/all-langs'; import { langCodes } from '../../../config/i18n/all-langs';
import envData from '../../../config/env.json'; import envData from '../../../config/env.json';
import RedirectHome from '../components/RedirectHome'; import RedirectHome from '../components/redirect-home';
import { Loader, Spacer } from '../components/helpers'; import { Loader, Spacer } from '../components/helpers';
import { isEmpty } from 'lodash-es'; import { isEmpty } from 'lodash-es';
import { UserType } from '../redux/prop-types'; import { UserType } from '../redux/prop-types';

View File

@ -22,8 +22,8 @@ import {
import { Link } from '../../helpers'; import { Link } from '../../helpers';
import { updateUserFlag } from '../../../redux/settings'; import { updateUserFlag } from '../../../redux/settings';
import envData from '../../../../../config/env.json'; import envData from '../../../../../config/env.json';
import createLanguageRedirect from '../../createLanguageRedirect'; import createLanguageRedirect from '../../create-language-redirect';
import createExternalRedirect from '../../createExternalRedirects'; import createExternalRedirect from '../../create-external-redirects';
import { import {
availableLangs, availableLangs,
i18nextCodes, i18nextCodes,

View File

@ -1,4 +1,4 @@
import createExternalRedirect from './createExternalRedirects'; import createExternalRedirect from './create-external-redirects';
describe('createExternalRedirects', () => { describe('createExternalRedirects', () => {
describe('english redirects', () => { describe('english redirects', () => {

View File

@ -2,7 +2,10 @@ import envData from '../../../config/env.json';
const { forumLocation } = envData; const { forumLocation } = envData;
const createExternalRedirect = (page, { clientLocale }) => { const createExternalRedirect = (
page: string,
{ clientLocale }: { clientLocale: string }
): string => {
// Handle Chinese // Handle Chinese
if (clientLocale === 'chinese' || clientLocale === 'chinese-traditional') { if (clientLocale === 'chinese' || clientLocale === 'chinese-traditional') {
return `https://chinese.freecodecamp.org/${page}`; return `https://chinese.freecodecamp.org/${page}`;

View File

@ -1,4 +1,4 @@
import createLanguageRedirect from './createLanguageRedirect'; import createLanguageRedirect from './create-language-redirect';
describe('createLanguageRedirect for clientLocale === english', () => { describe('createLanguageRedirect for clientLocale === english', () => {
const envVars = { const envVars = {
@ -20,8 +20,10 @@ describe('createLanguageRedirect for clientLocale === english', () => {
const originalLocation = window.location; const originalLocation = window.location;
beforeEach(() => { beforeEach(() => {
delete window.location; Object.defineProperty(window, 'location', {
window.location = new URL(currentPageURL); writable: true,
value: new URL(currentPageURL)
});
}); });
afterEach(() => { afterEach(() => {
@ -80,8 +82,10 @@ describe('createLanguageRedirect for clientLocale === english', () => {
const originalLocation = window.location; const originalLocation = window.location;
beforeEach(() => { beforeEach(() => {
delete window.location; Object.defineProperty(window, 'location', {
window.location = new URL(currentPageURL); writable: true,
value: new URL(currentPageURL)
});
}); });
afterEach(() => { afterEach(() => {
@ -150,8 +154,10 @@ describe('createLanguageRedirect for clientLocale === chinese', () => {
const originalLocation = window.location; const originalLocation = window.location;
beforeEach(() => { beforeEach(() => {
delete window.location; Object.defineProperty(window, 'location', {
window.location = new URL(currentPageURL); writable: true,
value: new URL(currentPageURL)
});
}); });
afterEach(() => { afterEach(() => {
@ -210,8 +216,10 @@ describe('createLanguageRedirect for clientLocale === chinese', () => {
const originalLocation = window.location; const originalLocation = window.location;
beforeEach(() => { beforeEach(() => {
delete window.location; Object.defineProperty(window, 'location', {
window.location = new URL(currentPageURL); writable: true,
value: new URL(currentPageURL)
});
}); });
afterEach(() => { afterEach(() => {

View File

@ -1,9 +1,15 @@
const createLanguageRedirect = ({ clientLocale, lang }) => { const createLanguageRedirect = ({
clientLocale,
lang
}: {
clientLocale: string;
lang: string;
}): string => {
// return early if requesting the same page // return early if requesting the same page
if (clientLocale === lang) return `${window?.location}`; if (clientLocale === lang) return window?.location.toString();
let path = window?.location?.pathname?.split('/'); const pathArray = window?.location?.pathname?.split('/');
path = path const path = pathArray
.filter(item => (item !== clientLocale && item !== lang ? item : '')) .filter(item => (item !== clientLocale && item !== lang ? item : ''))
.join('/'); .join('/');

View File

@ -1,10 +1,10 @@
import { navigate } from 'gatsby'; import { navigate } from 'gatsby';
const createRedirect = const createRedirect =
(to = '/') => (to = '/'): (() => JSX.Element | null) =>
() => { () => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
navigate(to); void navigate(to);
} }
return null; return null;
}; };

View File

@ -10,7 +10,7 @@ import {
isSignedInSelector, isSignedInSelector,
tryToShowDonationModal tryToShowDonationModal
} from '../../redux'; } from '../../redux';
import createRedirect from '../../components/createRedirect'; import createRedirect from '../create-redirect';
import DonateModal from '../Donation/DonationModal'; import DonateModal from '../Donation/DonationModal';
import './prism.css'; import './prism.css';

View File

@ -1,4 +1,4 @@
import createRedirect from './createRedirect'; import createRedirect from './create-redirect';
import { withPrefix } from 'gatsby'; import { withPrefix } from 'gatsby';
export default createRedirect(withPrefix('/')); export default createRedirect(withPrefix('/'));

View File

@ -2,7 +2,7 @@ import React from 'react';
import { Router } from '@reach/router'; import { Router } from '@reach/router';
import { withPrefix } from 'gatsby'; import { withPrefix } from 'gatsby';
import RedirectHome from '../components/RedirectHome'; import RedirectHome from '../components/redirect-home';
import ShowCertification from '../client-only-routes/show-certification'; import ShowCertification from '../client-only-routes/show-certification';
import './certification.css'; import './certification.css';

View File

@ -12,7 +12,7 @@ import { createSelector } from 'reselect';
import { ButtonSpacer, Spacer } from '../components/helpers'; import { ButtonSpacer, Spacer } from '../components/helpers';
import { acceptTerms, userSelector } from '../redux'; import { acceptTerms, userSelector } from '../redux';
import createRedirect from '../components/createRedirect'; import createRedirect from '../components/create-redirect';
import './email-sign-up.css'; import './email-sign-up.css';

View File

@ -2,7 +2,7 @@ import React from 'react';
import { Router } from '@reach/router'; import { Router } from '@reach/router';
import { withPrefix } from 'gatsby'; import { withPrefix } from 'gatsby';
import RedirectHome from '../components/RedirectHome'; import RedirectHome from '../components/redirect-home';
import ShowSettings from '../client-only-routes/show-settings'; import ShowSettings from '../client-only-routes/show-settings';
function Settings(): JSX.Element { function Settings(): JSX.Element {

View File

@ -2,7 +2,7 @@ import React from 'react';
import { Router } from '@reach/router'; import { Router } from '@reach/router';
import { withPrefix } from 'gatsby'; import { withPrefix } from 'gatsby';
import RedirectHome from '../components/RedirectHome'; import RedirectHome from '../components/redirect-home';
import ShowUnsubscribed from '../client-only-routes/show-unsubscribed'; import ShowUnsubscribed from '../client-only-routes/show-unsubscribed';
function Unsubscribed(): JSX.Element { function Unsubscribed(): JSX.Element {

View File

@ -2,7 +2,7 @@ import React from 'react';
import { Router } from '@reach/router'; import { Router } from '@reach/router';
import { withPrefix } from 'gatsby'; import { withPrefix } from 'gatsby';
import RedirectHome from '../components/RedirectHome'; import RedirectHome from '../components/redirect-home';
import ShowUser from '../client-only-routes/show-user'; import ShowUser from '../client-only-routes/show-user';
function User(): JSX.Element { function User(): JSX.Element {