|
| 1 | +/* @flow weak */ |
| 2 | + |
| 3 | +// libs |
| 4 | +import React, { Component, Element } from 'react'; |
| 5 | +import _ from 'lodash'; |
| 6 | +import moment from 'moment'; |
| 7 | + |
| 8 | +// components |
| 9 | +import WaitAnimation from 'components/core/wait-animation/wait-animation'; |
| 10 | +import ActionLink from 'components/core/action-link'; |
| 11 | + |
| 12 | +// styles |
| 13 | +import styles from './product-reviews-list.css'; |
| 14 | + |
| 15 | +// types |
| 16 | +type ReviewAttributes = { |
| 17 | + title: {t:'string', v: string}, |
| 18 | + body: {t:'string', v: string}, |
| 19 | + status: {t: 'string', v: string}, |
| 20 | +} |
| 21 | + |
| 22 | +type ReviewItem = { |
| 23 | + sku: string, |
| 24 | + id: number, |
| 25 | + scope: string, |
| 26 | + createdAt: string, |
| 27 | + updatedAt: string, |
| 28 | + archivedAt: ?string, |
| 29 | + userName: string, |
| 30 | + attributes: ReviewAttributes, |
| 31 | +} |
| 32 | + |
| 33 | +type Props = { |
| 34 | + isLoading: ?boolean, |
| 35 | + listItems: ?Array<ReviewItem>, |
| 36 | + title: string, |
| 37 | + emptyContentTitle: string, |
| 38 | + paginationSize: number, |
| 39 | + onLoadMoreReviews: Function, |
| 40 | + showLoadMore: ?boolean |
| 41 | +}; |
| 42 | + |
| 43 | +type State = { |
| 44 | + page: number, |
| 45 | +} |
| 46 | + |
| 47 | +const ReviewBody = (props): Element<any> => { |
| 48 | + const { title, userName, updatedAt, sku, body } = props; |
| 49 | + const updatedAtFormatted = moment(updatedAt).format('MMM Do YYYY'); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div styleName="product-review-container"> |
| 53 | + <div styleName="product-review-content"> |
| 54 | + <div styleName="product-review-title"> |
| 55 | + {title} |
| 56 | + </div> |
| 57 | + <div styleName="product-review-name-date"> |
| 58 | + From: {userName} on {updatedAtFormatted} |
| 59 | + </div> |
| 60 | + <div styleName="product-review-variant"> |
| 61 | + SKU: {sku} |
| 62 | + </div> |
| 63 | + <div styleName="product-review-body"> |
| 64 | + {body} |
| 65 | + </div> |
| 66 | + <div styleName="product-review-flag"> |
| 67 | + <ActionLink |
| 68 | + action={_.noop} |
| 69 | + title="Report Offensive Review" |
| 70 | + styleName="product-review-report-offense" |
| 71 | + /> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +function incrementPage(nextPage) { |
| 79 | + return { page: nextPage }; |
| 80 | +} |
| 81 | + |
| 82 | +class ProductReviewsList extends Component { |
| 83 | + |
| 84 | + props: Props; |
| 85 | + state: State = { |
| 86 | + page: 0, |
| 87 | + }; |
| 88 | + |
| 89 | + get reviewsEmptyContentTitle(): ?Element<any> { |
| 90 | + const { listItems, emptyContentTitle } = this.props; |
| 91 | + |
| 92 | + if (_.isEmpty(listItems)) { |
| 93 | + return ( |
| 94 | + <div styleName="product-reviews-subtitle"> |
| 95 | + {emptyContentTitle} |
| 96 | + </div> |
| 97 | + ); |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + get displayReviews(): ?Element<any> { |
| 103 | + const { listItems, showLoadMore } = this.props; |
| 104 | + const activeReviews = _.filter(listItems, (review) => { |
| 105 | + return review.attributes.status.v == 'submitted'; |
| 106 | + }); |
| 107 | + if (!_.isEmpty(activeReviews)) { |
| 108 | + const reviews = _.map(listItems, (review) => { |
| 109 | + return ( |
| 110 | + <ReviewBody |
| 111 | + key={review.id} |
| 112 | + title={review.attributes.title.v} |
| 113 | + userName={review.userName} |
| 114 | + updatedAt={review.updatedAt} |
| 115 | + sku={review.sku} |
| 116 | + body={review.attributes.body.v} |
| 117 | + /> |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + const loadMoreActionLink = (showLoadMore) |
| 122 | + ? ( |
| 123 | + <ActionLink |
| 124 | + action={this.handleLoadMoreReviews} |
| 125 | + title="LOAD MORE REVIEWS" |
| 126 | + styleName="product-review-load-more" |
| 127 | + /> |
| 128 | + ) |
| 129 | + : null; |
| 130 | + |
| 131 | + return ( |
| 132 | + <div> |
| 133 | + {reviews} |
| 134 | + {loadMoreActionLink} |
| 135 | + </div> |
| 136 | + ); |
| 137 | + } |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + handleLoadMoreReviews = () => { |
| 142 | + const { onLoadMoreReviews, paginationSize } = this.props; |
| 143 | + const { page } = this.state; |
| 144 | + |
| 145 | + const nextPage = page + 1; |
| 146 | + this.setState(incrementPage(nextPage)); |
| 147 | + onLoadMoreReviews(paginationSize * nextPage); |
| 148 | + } |
| 149 | + |
| 150 | + render(): Element<any> { |
| 151 | + const { title, isLoading } = this.props; |
| 152 | + |
| 153 | + if (isLoading) { |
| 154 | + return <WaitAnimation />; |
| 155 | + } |
| 156 | + |
| 157 | + return ( |
| 158 | + <div styleName="product-reviews-list-wrapper"> |
| 159 | + <div styleName="product-reviews-title"> |
| 160 | + {title} |
| 161 | + </div> |
| 162 | + {this.reviewsEmptyContentTitle} |
| 163 | + {this.displayReviews} |
| 164 | + </div> |
| 165 | + ); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +export default ProductReviewsList; |
0 commit comments