Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make compatible with multiple store gallery images #627

Merged
merged 3 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/store/components/AdminItemPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as Yup from 'yup';
import Config from '../../../config';
import { history } from '../../../redux_store';
import { PublicMerchCollection, PublicMerchItem } from '../../../types';
import { fetchService, notify } from '../../../utils';
import { fetchService, getDefaultMerchItemPicture, notify } from '../../../utils';

import OptionDisplay from '../OptionDisplay';
import StoreButton from '../StoreButton';
Expand Down Expand Up @@ -104,7 +104,7 @@ const AdminItemPage: React.FC<AdminItemPageProps> = (props) => {
collection: item?.collection?.uuid ?? '',
itemName: item?.itemName ?? '',
description: item?.description ?? '',
existingPicture: item?.merchPhotos[0]?.uploadedPhoto,
existingPicture: getDefaultMerchItemPicture(item),
newPicture: undefined,
hidden: item?.hidden ?? false,
monthlyLimit: item?.monthlyLimit.toString() ?? '',
Expand Down
4 changes: 2 additions & 2 deletions src/store/components/CartDisplay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useDispatch } from 'react-redux';
import { Button, Select, Table, Typography } from 'antd';

import { CartItem, PublicMerchItem, PublicMerchItemOption } from '../../../types';
import { toProperCase } from '../../../utils';
import { getDefaultMerchItemPicture, toProperCase } from '../../../utils';
import { addToCart, editInCart, removeFromCart } from '../../storeActions';

import './style.less';
Expand Down Expand Up @@ -132,7 +132,7 @@ const CartDisplay: React.FC<CartDisplayProps> = (props) => {

const renderItemImage = (item: PublicMerchItem) => (
<div className="image-container">
<img className="image" src={item.merchPhotos[0]?.uploadedPhoto} alt={item.itemName} />
<img className="image" src={getDefaultMerchItemPicture(item)} alt={item.itemName} />
</div>
);

Expand Down
8 changes: 5 additions & 3 deletions src/store/components/ItemCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Link } from 'react-router-dom';

import { PublicMerchItem } from '../../../types';
import { processItem, processItemPrice } from '../../../utils';
import { getDefaultMerchItemPicture, processItem, processItemPrice } from '../../../utils';

import StorePlus from '../../../assets/icons/store-plus-icon.svg';
import EditableIcon from '../../../assets/icons/editable-icon.svg';
Expand Down Expand Up @@ -36,7 +36,9 @@ const ItemCard: React.FC<ItemCardProps> = (props) => {
return null;
}

const { uuid, itemName, description, merchPhotos, hidden } = item;
const { uuid, itemName, description, hidden } = item;

const picture = getDefaultMerchItemPicture(item);

const { outOfStock } = processItem(item.options);

Expand All @@ -50,7 +52,7 @@ const ItemCard: React.FC<ItemCardProps> = (props) => {
<Link to={`/store/item/${uuid}`}>
<div className={`item-card-contents${outOfStock ? ' out-of-stock' : ''}`}>
<div className="item-card-image-container">
<img className="item-card-image" src={merchPhotos[0]?.uploadedPhoto} alt={description} />
<img className="item-card-image" src={picture} alt={description} />
</div>
<div className="item-card-name">
{itemName}
Expand Down
9 changes: 5 additions & 4 deletions src/store/components/ItemPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { replace } from 'connected-react-router';
import { Modal } from 'antd';

import { PublicMerchItemWithPurchaseLimits, PublicMerchItemOption, UserAccessType } from '../../../types';
import { processItem, processItemPrice } from '../../../utils';
import { getDefaultMerchItemPicture, processItem, processItemPrice } from '../../../utils';
import { addToCart } from '../../storeActions';

import StoreHeader from '../StoreHeader';
Expand Down Expand Up @@ -42,7 +42,8 @@ const ItemPage: React.FC<ItemPageProps> = (props) => {
const { outOfStock: optionOutOfStock } = currentOption ? processItem([currentOption]) : { outOfStock: false };
const itemOptionPrice = currentOption ? processItemPrice([currentOption]) : null;

const { itemName, description, hasVariantsEnabled, options, merchPhotos, hidden } = item;
const { itemName, description, hasVariantsEnabled, options, hidden } = item;
const picture = getDefaultMerchItemPicture(item);

const limitHit = item.monthlyRemaining === 0 || item.lifetimeRemaining === 0;
let limitMessage;
Expand All @@ -60,7 +61,7 @@ const ItemPage: React.FC<ItemPageProps> = (props) => {
<StoreHeader breadcrumb breadcrumbTitle="Shopping" breadcrumbLocation="/store" showBalance showCart />
<div className="item-page">
<div className="item-image-container">
<img className="item-image" src={merchPhotos[0]?.uploadedPhoto} alt={description} />
<img className="item-image" src={picture} alt={description} />
</div>
<div className="item-contents">
<h2 className="item-name">{itemName}</h2>
Expand Down Expand Up @@ -141,7 +142,7 @@ const ItemPage: React.FC<ItemPageProps> = (props) => {
>
<p className="item-page-modal-title">Added to cart</p>
<div className="item-page-modal-content">
<img className="item-page-modal-item-image" src={merchPhotos[0]?.uploadedPhoto} alt={description} />
<img className="item-page-modal-item-image" src={picture} alt={description} />
<div className="item-page-modal-item-details">
<p className="item-page-modal-item-title">{itemName}</p>
{hasVariantsEnabled && currentOption && currentOption.metadata && (
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export interface PublicMerchItem {
uploadedPhoto: string;
uploadedAt: string;
position: number;
};
}[];
description: string;
monthlyLimit: number;
lifetimeLimit: number;
Expand Down
14 changes: 13 additions & 1 deletion src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { notification } from 'antd';

import Storage from './storage';
import { HttpRequestMethod, MimeType, FetchServiceOptions, PublicMerchItemOption, OrderStatus } from './types';
import { HttpRequestMethod, MimeType, FetchServiceOptions, PublicMerchItemOption, OrderStatus, PublicMerchItem } from './types';

import DiamondDisplay from './store/components/DiamondDisplay';

Expand Down Expand Up @@ -309,3 +309,15 @@ export const parseOrderStatus = (status: OrderStatus) => {
return '';
}
};

/**
* Given a merch item, return the first picture associated with it.
*/
export const getDefaultMerchItemPicture = (item: PublicMerchItem | undefined): string | undefined => {
if (item && item.merchPhotos.length !== 0) {
// Get the item here with the smallest position (since it doesn't always arrive sorted)
item.merchPhotos.sort((a, b) => a.position - b.position);
return item.merchPhotos[0].uploadedPhoto;
alexzhang1618 marked this conversation as resolved.
Show resolved Hide resolved
}
return undefined;
};