From e2f502c7cdf977d1d29a29aa92af566c8d4e08e7 Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Sat, 15 Feb 2025 22:48:58 +0100 Subject: [PATCH 1/2] Remove id in urls --- .../AlgoliaSearch/AlgoliaSearchBox.component.tsx | 2 +- .../AlgoliaSearch/SearchResults.component.tsx | 7 ++----- .../Product/DisplayProducts.component.tsx | 8 ++------ src/components/Product/ProductCard.component.tsx | 4 ++-- src/pages/produkt/[slug].tsx | 14 ++++++++++++-- src/utils/gql/GQL_QUERIES.ts | 4 ++-- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/components/AlgoliaSearch/AlgoliaSearchBox.component.tsx b/src/components/AlgoliaSearch/AlgoliaSearchBox.component.tsx index 0254677c3..4f1bea8b0 100644 --- a/src/components/AlgoliaSearch/AlgoliaSearchBox.component.tsx +++ b/src/components/AlgoliaSearch/AlgoliaSearchBox.component.tsx @@ -51,7 +51,7 @@ const AlgoliaSearchBox = () => { }} /> {search && ( -
+
)} diff --git a/src/components/AlgoliaSearch/SearchResults.component.tsx b/src/components/AlgoliaSearch/SearchResults.component.tsx index 0176403c0..a3eb72629 100644 --- a/src/components/AlgoliaSearch/SearchResults.component.tsx +++ b/src/components/AlgoliaSearch/SearchResults.component.tsx @@ -11,6 +11,7 @@ interface ISearchResultProps { on_sale: boolean; short_description: string; objectID: number; + slug: string; }; } @@ -37,14 +38,10 @@ const SearchResults = ({ objectID, }, }: ISearchResultProps) => { - // Replace empty spaces with dash (-) - const trimmedProductName = product_name.replace(/ /g, '-'); - return (
diff --git a/src/components/Product/DisplayProducts.component.tsx b/src/components/Product/DisplayProducts.component.tsx index d6b75eef1..738cbab84 100644 --- a/src/components/Product/DisplayProducts.component.tsx +++ b/src/components/Product/DisplayProducts.component.tsx @@ -79,9 +79,7 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => ( return (
{image ? ( @@ -102,9 +100,7 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => (
diff --git a/src/components/Product/ProductCard.component.tsx b/src/components/Product/ProductCard.component.tsx index 77f535b57..a29a78a06 100644 --- a/src/components/Product/ProductCard.component.tsx +++ b/src/components/Product/ProductCard.component.tsx @@ -33,7 +33,7 @@ const ProductCard = ({ return (
- + {image?.sourceUrl ? (
- +

{name} diff --git a/src/pages/produkt/[slug].tsx b/src/pages/produkt/[slug].tsx index 711da637c..395ef2c70 100644 --- a/src/pages/produkt/[slug].tsx +++ b/src/pages/produkt/[slug].tsx @@ -48,11 +48,21 @@ const Produkt: NextPage = ({ export default withRouter(Produkt); export const getServerSideProps: GetServerSideProps = async ({ - query: { id }, + params, + query, + res, }) => { + // Handle legacy URLs with ID parameter by removing it + if (query.id) { + res.setHeader('Location', `/produkt/${params?.slug}`); + res.statusCode = 301; + res.end(); + return { props: {} }; + } + const { data, loading, networkStatus } = await client.query({ query: GET_SINGLE_PRODUCT, - variables: { id }, + variables: { slug: params?.slug }, }); return { diff --git a/src/utils/gql/GQL_QUERIES.ts b/src/utils/gql/GQL_QUERIES.ts index b3edca9dd..e39955232 100644 --- a/src/utils/gql/GQL_QUERIES.ts +++ b/src/utils/gql/GQL_QUERIES.ts @@ -1,8 +1,8 @@ import { gql } from '@apollo/client'; export const GET_SINGLE_PRODUCT = gql` - query Product($id: ID!) { - product(id: $id, idType: DATABASE_ID) { + query Product($slug: ID!) { + product(id: $slug, idType: SLUG) { id databaseId averageRating From 443107451b07be35bbfec9b0bc278f1a96c90628 Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Sat, 15 Feb 2025 22:51:53 +0100 Subject: [PATCH 2/2] Fix npm run build --- .../AlgoliaSearch/SearchResults.component.tsx | 4 ---- .../Product/DisplayProducts.component.tsx | 18 ++++++------------ .../User/UserRegistration.component.tsx | 4 ++-- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/components/AlgoliaSearch/SearchResults.component.tsx b/src/components/AlgoliaSearch/SearchResults.component.tsx index a3eb72629..968b0458c 100644 --- a/src/components/AlgoliaSearch/SearchResults.component.tsx +++ b/src/components/AlgoliaSearch/SearchResults.component.tsx @@ -1,5 +1,4 @@ import Link from 'next/link'; - import { trimmedStringToLength } from '@/utils/functions/functions'; interface ISearchResultProps { @@ -10,7 +9,6 @@ interface ISearchResultProps { sale_price: string; on_sale: boolean; short_description: string; - objectID: number; slug: string; }; } @@ -24,7 +22,6 @@ interface ISearchResultProps { * @param {string} sale_price Price when on sale * @param {boolean} on_sale Is the product on sale? True or false * @param {string} short_description Short description of product - * @param {number} objectID ID of product } */ const SearchResults = ({ @@ -35,7 +32,6 @@ const SearchResults = ({ sale_price, on_sale, short_description, - objectID, }, }: ISearchResultProps) => { return ( diff --git a/src/components/Product/DisplayProducts.component.tsx b/src/components/Product/DisplayProducts.component.tsx index 738cbab84..cb2aa13ce 100644 --- a/src/components/Product/DisplayProducts.component.tsx +++ b/src/components/Product/DisplayProducts.component.tsx @@ -23,7 +23,6 @@ interface Variations { interface RootObject { __typename: string; - databaseId: number; name: string; onSale: boolean; slug: string; @@ -55,7 +54,6 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => ( {products ? ( products.map( ({ - databaseId, name, price, regularPrice, @@ -78,9 +76,7 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => ( return (

- +
{image ? ( ( id="product-image" className="w-full h-full object-cover object-center transition duration-300 group-hover:scale-105" alt={name} - src={process.env.NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL} + src={ + process.env.NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL + } /> )}
- +

@@ -123,9 +119,7 @@ const DisplayProducts = ({ products }: IDisplayProductsProps) => (

) : ( - - {price} - + {price} )}
diff --git a/src/components/User/UserRegistration.component.tsx b/src/components/User/UserRegistration.component.tsx index 9f0aeed44..7c1f67ff4 100644 --- a/src/components/User/UserRegistration.component.tsx +++ b/src/components/User/UserRegistration.component.tsx @@ -36,8 +36,8 @@ const UserRegistration = () => { } else { throw new Error('Failed to register customer'); } - } catch (err: unknown) { - console.error('Registration error'); + } catch (error: unknown) { + console.error('Registration error:', error); } };