Skip to content

Commit

Permalink
fix(storefront): 🐛 fixed not authenticated error on product review
Browse files Browse the repository at this point in the history
  • Loading branch information
sahrohit committed Aug 7, 2023
1 parent 84379bb commit a52ef49
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ProductReview as IProductReview,
ReviewSummaryResponse,
useAllReviewsQuery,
useMeQuery,
useReviewByUserAndProductQuery,
useReviewSummaryQuery,
useReviewsQuery,
Expand Down Expand Up @@ -232,10 +233,12 @@ interface ReviewFormProps extends ButtonProps {

export const CreateReviewButton = ({ productId, ...rest }: ReviewFormProps) => {
const modalRef: any = useRef();
const { data: user, loading: userLoading, error: userError } = useMeQuery();
const { data, loading, error } = useReviewByUserAndProductQuery({
variables: {
productId,
},
skip: !user?.me?.id,
});

const closeModal = () => {
Expand All @@ -244,21 +247,25 @@ export const CreateReviewButton = ({ productId, ...rest }: ReviewFormProps) => {
}
};

if (loading) {
if (loading || userLoading) {
return (
<Button colorScheme="blue" px={12} size="xl" isLoading>
Write a review
</Button>
);
}

if (error)
if (!userLoading && !user?.me?.id) {
return null;
}

if (error || userError)
return (
<Result
heading={error.name}
heading={error ? error.name : userError?.name!}
type="error"
text={error.message}
dump={error.stack}
text={error ? error.message : userError?.message!}
dump={error ? error.stack : userError?.stack}
/>
);

Expand Down
16 changes: 10 additions & 6 deletions apps/storefront/src/pages/products/favourite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Rating from "@/components/shared/product/Rating";
import {
Favourite,
useFavouritesWithProductQuery,
useMeQuery,
useRemoveFromFavouriteMutation,
} from "@/generated/graphql";
import {
Expand All @@ -29,15 +30,18 @@ import Image from "next/image";
import services from "../../../public/assets/services.svg";

const FavouritePage = () => {
const { data, loading, error } = useFavouritesWithProductQuery();
const { data: user, loading: userLoading, error: userError } = useMeQuery();
const { data, loading, error } = useFavouritesWithProductQuery({
skip: !user?.me?.id,
});

if (error) {
if (error || userError) {
return (
<Result
heading={error.name}
heading={error ? error.name : userError?.name!}
type="error"
text={error.message}
dump={error.stack}
text={error ? error.message : userError?.message!}
dump={error ? error.stack : userError?.stack}
/>
);
}
Expand All @@ -63,7 +67,7 @@ const FavouritePage = () => {
rowGap={{ base: "8", md: "10" }}
>
{/* eslint-disable-next-line no-nested-ternary */}
{loading ? (
{loading || userLoading ? (
Array(4)
.fill("favourite-skeleton")
.map((mock, index) => (
Expand Down

0 comments on commit a52ef49

Please sign in to comment.