Skip to main content

useReviewList

The useReviewsList hook is designed to fetch and manage a list of product reviews from a Shopify data source. It provides functionality for filtering, pagination, and handling loading states.

Usage

import { useReviewsList } from '@appmaker-xyz/shopify';

const {
reviews,
isLoading,
isError,
hasNextPage,
fetchNextPage,
isPageFetching,
filters,
setFilters,
resetFilters,
rating,
setRating,
limit,
setLimit
} = useReviewsList({ productId, filters });

Parameters

FieldTypeDescription
productIdstringThe ID of the product to fetch reviews for.
filters (optional)Initial filters to apply to the reviews.
defaultbooleanWhether to use default filters.
limitnumberThe maximum number of reviews to fetch.
ratingnumberThe minimum rating to filter by.

Returns

FieldTypeDescription
reviewsarrayThe list of fetched reviews.
isLoadingbooleanWhether the reviews are currently being fetched.
isErrorbooleanWhether an error occurred during fetching.
hasNextPagebooleanWhether there are more reviews to fetch.
fetchNextPagefunctionFunction to fetch the next page of reviews.
isPageFetchingbooleanWhether the next page is currently being fetched.
filtersobjectThe current filters applied.
setFiltersfunctionFunction to set new filters.
resetFiltersfunctionFunction to reset filters to default.
ratingnumberThe current rating filter.
setRatingfunctionFunction to set a new rating filter.
limitnumberThe current limit filter.
setLimitfunctionFunction to set a new limit filter.

Example

Here's an example of how to use the useReviewsList hook in a React component:

import React from 'react';
import {
StyleSheet,
View,
FlatList,
Pressable,
Text,
ActivityIndicator,
} from 'react-native';
import { useReviewsList } from '@appmaker-xyz/shopify';

const ReviewList = ({
pageData,
attributes,
BlocksView,
innerBlocks,
blockData,
BlockItem,
onAction,
BlockItemRender,
...props
}) => {
const {
reviews,
isLoading,
isError,
hasNextPage,
fetchNextPage,
isPageFetching,
rating,
setRating,
} = useReviewsList({
productId: blockData?.productId || blockData?.node?.id,
filters: {
default: true,
limit: 10,
},
});

const renderItem = ({ item }) => {
return (
<BlockItem
BlockItem={BlockItem}
BlocksView={BlocksView}
blockData={item}
onAction={onAction}
block={{
name: 'shopify/review-item',
innerBlocks,
clientId: 'review-item',
isValid: true,
}}
/>
);
};

return (
<View>
<View style={styles.headerContainer}>
{Array.from({ length: 6 }).map((_, index) => {
const isPressed =
index === rating || (rating === undefined && index === 0);
return (
<Pressable
key={index}
onPress={() => setRating(index === 0 ? undefined : index)}
style={[
styles.starsPressable,
{
backgroundColor: isPressed ? '#2c2c2c' : '#fff',
},
]}>
<Text
style={{
color: isPressed ? '#fff' : '#2c2c2c',
fontSize: 12,
}}>
{index === 0 ? 'All' : `${index} Star${index > 1 ? 's' : ''}`}
</Text>
</Pressable>
);
})}
</View>
{isError ? (
<Text>Error loading reviews</Text>
) : isLoading ? (
<ActivityIndicator size="small" color="#2c2c2c" />
) : null}
<FlatList
data={reviews}
renderItem={renderItem}
keyExtractor={(item, index) =>
item?.reviewer?.name?.toString() + item?.created_at?.toString()
}
onEndReached={() => hasNextPage && fetchNextPage()}
onEndReachedThreshold={0.1}
ListFooterComponent={() =>
isPageFetching ? (
<ActivityIndicator size="small" color="#2c2c2c" />
) : null
}
ListEmptyComponent={() => (
<Text style={styles.listEmptyComponent}>No reviews found</Text>
)}
/>
{reviews?.length > 0 && (
<BlockItem
BlockItem={BlockItem}
BlocksView={BlocksView}
blockData={{
productId: blockData?.productId || blockData?.node?.id,
}}
onAction={onAction}
block={{
name: 'shopify/show-all-review',
innerBlocks,
clientId: 'review-show-all',
isValid: true,
}}
/>
)}
</View>
);
};

const styles = StyleSheet.create({
headerContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingHorizontal: 10,
alignItems: 'center',
marginBottom: 10,
},
starsPressable: {
borderWidth: 1,
borderColor: '#ccc',
paddingHorizontal: 8,
paddingVertical: 4,
borderRadius: 10,
},
listEmptyComponent: {
color: '#2c2c2c',
fontSize: 12,
textAlign: 'center',
marginVertical: 10,
},
});

export default ReviewList;