Skip to content

Commit

Permalink
feat: 🎸 upgrade to React18 and Next13
Browse files Browse the repository at this point in the history
  • Loading branch information
BolajiAyodeji committed Feb 15, 2023
1 parent 606f7ad commit 66645ce
Show file tree
Hide file tree
Showing 70 changed files with 11,570 additions and 42,187 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
auto-install-peers = true
38 changes: 19 additions & 19 deletions components/Countries.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import React, { FunctionComponent } from 'react'
import Link from 'next/link'
import _ from 'lodash'
import { Country } from '@typings/models'
import React, { FunctionComponent } from "react";
import Link from "next/link";
import _ from "lodash";
import { Country } from "@typings/models";

type Item = Omit<Country, 'image'> & {
image: { title: string; url: string }
}
type Item = Omit<Country, "image"> & {
image: { title: string; url: string };
};

type Props = {
items: Item[]
cms?: string
searchBy?: string
}
items: Item[];
cms?: string;
searchBy?: string;
};

const Countries: FunctionComponent<Props> = ({ items, searchBy }) => {
const countries = items.map(({ image, defaultLocale, code }, key) => {
const lang = _.first(defaultLocale.toLowerCase().split(','))
const countryCode = code.toLowerCase()
const lang = _.first(defaultLocale.toLowerCase().split(","));
const countryCode = code.toLowerCase();
const href = !_.isEmpty(searchBy)
? {
pathname: `/[countryCode]/[lang]`,
Expand All @@ -32,7 +32,7 @@ const Countries: FunctionComponent<Props> = ({ items, searchBy }) => {
countryCode,
lang,
},
}
};
return (
<Link key={key} href={href}>
<div className="cursor-pointer">
Expand All @@ -43,16 +43,16 @@ const Countries: FunctionComponent<Props> = ({ items, searchBy }) => {
/>
</div>
</Link>
)
})
);
});
return (
<div className="bg-white shadow-md p-10 max-w-screen-sm mx-auto rounded">
<h1 className="text-xl md:text-2xl mb-8">Choose your country</h1>
<div className="grid grid-cols-2 gap-y-14 gap-x-16 md:grid-cols-4 md:gap-y-8 md:gap-x-12">
{countries}
</div>
</div>
)
}
);
};

export default Countries
export default Countries;
52 changes: 26 additions & 26 deletions components/CountrySelector.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import React, { FunctionComponent, useState } from 'react'
import { useRouter } from 'next/router'
import _ from 'lodash'
import { Transition } from '@headlessui/react'
import { Country } from '@typings/models'
import locale from '@locale/index'
import React, { FunctionComponent, useState } from "react";
import { useRouter } from "next/router";
import _ from "lodash";
import { Transition } from "@headlessui/react";
import { Country } from "@typings/models";
import locale from "@locale/index";

type Props = {
options: Country[]
}
options: Country[];
};

const CountrySelector: FunctionComponent<Props> = ({ options }) => {
const [show, setShow] = useState(false)
const [show, setShow] = useState(false);
const {
push,
query: { countryCode, searchBy, lang },
} = useRouter()
} = useRouter();
const optionComponents = options.map(
({ code, name, image, defaultLocale }) => {
return {
value: code.toLowerCase(),
name,
image,
defaultLocale: defaultLocale.toLowerCase(),
}
};
}
)
);
const selectedOption = _.first(
optionComponents.filter(({ value }) => value === countryCode)
)
);
const handleChange = (code: string, defaultLocale: string) => {
searchBy
? push(`/${code}/${defaultLocale}?searchBy=${searchBy}`)
: push(`/${code}/${defaultLocale}`)
setShow(!show)
}
: push(`/${code}/${defaultLocale}`);
setShow(!show);
};
return (
<div>
<div className="mt-1 relative">
Expand All @@ -47,7 +47,7 @@ const CountrySelector: FunctionComponent<Props> = ({ options }) => {
>
<span className="flex items-center">
<span className="flex-shrink-0 text-gray-700 truncate">
{locale[lang as string].shippingTo}:{' '}
{locale[lang as string].shippingTo}:{" "}
</span>
<img
src={selectedOption?.image?.url}
Expand Down Expand Up @@ -80,7 +80,7 @@ const CountrySelector: FunctionComponent<Props> = ({ options }) => {
>
<div
className={`absolute mt-1 w-full rounded-md bg-white shadow-lg ${
show ? 'z-10' : ''
show ? "z-10" : ""
}`}
onMouseLeave={() => setShow(false)}
>
Expand All @@ -93,13 +93,13 @@ const CountrySelector: FunctionComponent<Props> = ({ options }) => {
>
{optionComponents.map(
({ value, name, image, defaultLocale }, k) => {
const selected = value === selectedOption?.value
const selected = value === selectedOption?.value;
return (
<li
key={k}
role="option"
className={`cursor-default select-none relative py-2 pl-3 pr-9 hover:text-gray-50 hover:bg-blue-500 ${
selected ? '' : 'text-gray-900'
selected ? "" : "text-gray-900"
}`}
onClick={() => handleChange(value, defaultLocale)}
>
Expand All @@ -111,7 +111,7 @@ const CountrySelector: FunctionComponent<Props> = ({ options }) => {
/>
<span
className={`${
selected ? 'font-semibold' : 'font-normal'
selected ? "font-semibold" : "font-normal"
} ml-3 block font-normal truncate`}
>
{name}
Expand All @@ -120,7 +120,7 @@ const CountrySelector: FunctionComponent<Props> = ({ options }) => {
{/* Highlighted: "text-white", Not Highlighted: "text-indigo-600" */}
<span
className={`${
selected ? 'text-gray-900' : 'hidden'
selected ? "text-gray-900" : "hidden"
} absolute inset-y-0 right-0 flex items-center pr-4 hover:bg-blue-500`}
>
<svg
Expand All @@ -138,15 +138,15 @@ const CountrySelector: FunctionComponent<Props> = ({ options }) => {
</svg>
</span>
</li>
)
);
}
)}
</ul>
</div>
</Transition>
</div>
</div>
)
}
);
};

export default CountrySelector
export default CountrySelector;
44 changes: 22 additions & 22 deletions components/CustomHits.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { connectHits } from 'react-instantsearch-dom'
import Link from 'next/link'
import { useRouter } from 'next/router'
import _ from 'lodash'
import locale from '@locale/index'
import { connectHits } from "react-instantsearch-dom";
import Link from "next/link";
import { useRouter } from "next/router";
import _ from "lodash";
import locale from "@locale/index";

type Props = {
hits: {
name: string
image: string
reference: string
formattedAmount: string
formattedCompareAtAmount: string
description: string
category: string
objectID: string
slug: string
}[]
}
name: string;
image: string;
reference: string;
formattedAmount: string;
formattedCompareAtAmount: string;
description: string;
category: string;
objectID: string;
slug: string;
}[];
};

const Hits = ({ hits }: Props) => {
const {
query: { lang, countryCode },
} = useRouter()
} = useRouter();
return _.isEmpty(hits) ? (
<div className="w-full text-gray-900 h-96">
<p>{locale[lang as string].emptyProducts}</p>
Expand All @@ -37,7 +37,7 @@ const Hits = ({ hits }: Props) => {
formattedAmount,
formattedCompareAtAmount,
slug,
} = hit
} = hit;
return (
<li key={objectID}>
<Link
Expand Down Expand Up @@ -72,10 +72,10 @@ const Hits = ({ hits }: Props) => {
</div>
</Link>
</li>
)
);
})}
</ul>
)
}
);
};

export default connectHits(Hits)
export default connectHits(Hits);
24 changes: 12 additions & 12 deletions components/CustomPagination/First.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CustomPaginationProps } from '.'
import { FunctionComponent } from 'react'
import { CustomPaginationProps } from ".";
import { FunctionComponent } from "react";

type FirstProps = Pick<
CustomPaginationProps,
'showFirst' | 'currentRefinement' | 'createURL' | 'refine'
>
"showFirst" | "currentRefinement" | "createURL" | "refine"
>;

const First: FunctionComponent<FirstProps> = ({
showFirst,
Expand All @@ -13,24 +13,24 @@ const First: FunctionComponent<FirstProps> = ({
refine,
}) => {
const disabled =
currentRefinement === 1 ? 'cursor-not-allowed opacity-25' : ''
currentRefinement === 1 ? "cursor-not-allowed opacity-25" : "";
return !showFirst ? null : (
<li className={`pagination`}>
<a
className={disabled}
href={createURL(1)}
onClick={(event) => {
event.preventDefault()
event.preventDefault();
if (currentRefinement > 1) {
refine(1)
window.scrollTo(0, 0)
refine(1);
window.scrollTo(0, 0);
}
}}
>
{'<<'}
{"<<"}
</a>
</li>
)
}
);
};

export default First
export default First;
24 changes: 12 additions & 12 deletions components/CustomPagination/Last.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CustomPaginationProps } from '.'
import { FunctionComponent } from 'react'
import { CustomPaginationProps } from ".";
import { FunctionComponent } from "react";

type LastProps = Pick<
CustomPaginationProps,
'showLast' | 'currentRefinement' | 'createURL' | 'refine' | 'nbPages'
>
"showLast" | "currentRefinement" | "createURL" | "refine" | "nbPages"
>;

const Last: FunctionComponent<LastProps> = ({
showLast,
Expand All @@ -14,24 +14,24 @@ const Last: FunctionComponent<LastProps> = ({
nbPages,
}) => {
const disabled =
currentRefinement === nbPages ? 'cursor-not-allowed opacity-25' : ''
currentRefinement === nbPages ? "cursor-not-allowed opacity-25" : "";
return !showLast ? null : (
<li className={`pagination`}>
<a
className={disabled}
href={createURL(nbPages)}
onClick={(event) => {
event.preventDefault()
event.preventDefault();
if (currentRefinement < nbPages) {
refine(nbPages)
window.scrollTo(0, 0)
refine(nbPages);
window.scrollTo(0, 0);
}
}}
>
{'>>'}
{">>"}
</a>
</li>
)
}
);
};

export default Last
export default Last;
Loading

0 comments on commit 66645ce

Please sign in to comment.