Skip to content

Commit

Permalink
Adding disabled and loading states in the Ui
Browse files Browse the repository at this point in the history
  • Loading branch information
AnselmMarie committed Oct 1, 2024
1 parent 41484e8 commit bb8498e
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 33 deletions.
12 changes: 10 additions & 2 deletions libs/features/src/lib/cart/cart.modal/cart.modal.item.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ import { styles } from './cart.modal.module';
import useCartModalItemLogic from './use.cart.modal.item.logic';

const CartModalItem = ({ el }: any): ReactElement => {
const { onHandleRemoveFromCart, onHandleAddToCart, onHandleRemoveCartItem } =
useCartModalItemLogic();
const {
updateIsPending,
deleteIsPending,
onHandleRemoveFromCart,
onHandleAddToCart,
onHandleRemoveCartItem,
} = useCartModalItemLogic();
const { newStyles } = useRenderStyles(styles);
const { getThemeClass } = usePokemonThemeLogic(el?.types);

Expand Down Expand Up @@ -56,12 +61,14 @@ const CartModalItem = ({ el }: any): ReactElement => {
<UiIcon
icon={IconTypeEnum.ICON_MINUS}
classNameIcon={styles.quantityIcon}
isDisabled={el?.quantity === 1 || updateIsPending}
onClick={() => onHandleRemoveFromCart(el?.id)}
/>
<UiTypography>{el?.quantity}</UiTypography>
<UiIcon
icon={IconTypeEnum.ICON_PLUS}
classNameIcon={styles.quantityIcon}
isDisabled={updateIsPending}
onClick={() => onHandleAddToCart(el?.id)}
/>
</UiElementLayout>
Expand All @@ -70,6 +77,7 @@ const CartModalItem = ({ el }: any): ReactElement => {
<UiIcon
icon={IconTypeEnum.ICON_TRASH}
color={IconColorEnum.RED}
isDisabled={deleteIsPending}
onClick={() => onHandleRemoveCartItem(el?.id)}
/>
</UiElementLayout>
Expand Down
11 changes: 11 additions & 0 deletions libs/features/src/lib/cart/cart.modal/use.cart.modal.item.logic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useEffect } from 'react';

import { useDeleteCartItem, useUpdateCart } from '@pokemon-pet-shop/services';
import { GenericNonReturnType } from '@pokemon-pet-shop/typing';

interface UseCartModalReturn {
updateIsPending: boolean;
deleteIsPending: boolean;
onHandleRemoveFromCart: GenericNonReturnType;
onHandleAddToCart: GenericNonReturnType;
onHandleRemoveCartItem: GenericNonReturnType;
Expand Down Expand Up @@ -31,7 +35,14 @@ const useCartModalItemLogic = (): UseCartModalReturn => {
});
};

useEffect(() => {
console.log('updateCartMutation.error', updateCartMutation.error);
console.log('deleteCartMutation.error', deleteCartMutation.error);
}, [updateCartMutation.error, deleteCartMutation.error]);

return {
updateIsPending: updateCartMutation.isPending,
deleteIsPending: deleteCartMutation.isPending,
onHandleRemoveFromCart: handleRemoveFromCart,
onHandleAddToCart: handleAddToCart,
onHandleRemoveCartItem: handleRemoveCartItem,
Expand Down
18 changes: 15 additions & 3 deletions libs/features/src/lib/footer/footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { IconTypeEnum, UiElementLayout, UiIcon, UiTypography } from '@pokemon-pet-shop/ui';
import {
IconColorEnum,
IconTypeEnum,
UiElementLayout,
UiIcon,
UiTypography,
} from '@pokemon-pet-shop/ui';

import { styles } from './footer.module';

Expand All @@ -10,10 +16,16 @@ const Footer = () => {
return (
<UiElementLayout className={styles.footerWrapper}>
<UiElementLayout className={styles.githubWrapper} onClick={goToGithubProject}>
<UiIcon icon={IconTypeEnum.ICON_GITHUB} size={20} classNameIcon={styles.githubIcon} />
<UiIcon
icon={IconTypeEnum.ICON_GITHUB}
size={20}
classNameIcon={styles.githubIcon}
color={IconColorEnum.BLACK}
/>
<UiTypography>Github Project</UiTypography>
</UiElementLayout>
<UiTypography className={styles.footerDivider}>|</UiTypography> <UiIcon />
<UiTypography className={styles.footerDivider}>|</UiTypography>
<UiIcon color={IconColorEnum.BLACK} />
<UiTypography>Scan QR code for mobile app</UiTypography>
</UiElementLayout>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
UiTagWrapper,
UiTagItem,
ButtonSizeEnum,
UiSkeleton,
} from '@pokemon-pet-shop/ui';
import { globalStyles } from '@pokemon-pet-shop/ui/styles/global';
import {
Expand All @@ -31,6 +32,9 @@ const PokemonDetailModal = (): ReactElement => {
const {
modalData,
speciesData,
isPendingUpdateCart,
speciesIsLoading,
pricingIsLoading,
onConvertKgToLbs,
onConvertMetersToFtIn,
onGetPricingFormat,
Expand All @@ -53,7 +57,13 @@ const PokemonDetailModal = (): ReactElement => {
className={classNamesUtil(newStyles.imageContainer, newStyles?.[`${getThemeClass}ImageBg`])}
>
<UiElementLayout className={newStyles.priceWrapper}>
<UiTypography className={newStyles.priceText}>{onGetPricingFormat}</UiTypography>
<UiTypography className={newStyles.priceText}>
{!pricingIsLoading && !speciesIsLoading ? (
onGetPricingFormat
) : (
<UiSkeleton width="100px" />
)}
</UiTypography>
</UiElementLayout>
<UiImage
src={modalData?.sprites?.other?.['official-artwork']?.front_default}
Expand Down Expand Up @@ -95,7 +105,7 @@ const PokemonDetailModal = (): ReactElement => {
className={newStyles.cardDescription}
typographyType={TypographyTypeEnum.P}
>
{removeHtmlCodeInDescription}
{!speciesIsLoading ? removeHtmlCodeInDescription : <UiSkeleton count={2} />}
</UiTypography>

<UiElementLayout className={newStyles.weightHeightWrapper}>
Expand Down Expand Up @@ -144,8 +154,9 @@ const PokemonDetailModal = (): ReactElement => {
<UiButton
className={newStyles.btn}
text="Get Pet"
onClick={onHandleUpdateCartSubmit}
size={ButtonSizeEnum.LARGE}
isDisabled={isPendingUpdateCart}
onClick={onHandleUpdateCartSubmit}
/>
</UiElementLayout>
</UiElementLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import { pricingFormatUtil } from '@pokemon-pet-shop/utils';

interface PokemonDetailModalReturn {
modalData: PokemonListApi;
isPendingUpdateCart: boolean;
speciesData: PokemonSpeciesApi | undefined;
speciesIsError: boolean;
speciesIsLoading: boolean;
speciesIsFetching: boolean;
pricingData: PricingApi | undefined;
pricingIsError: boolean;
pricingIsLoading: boolean;
pricingIsFetching: boolean;
onConvertKgToLbs: any;
onConvertMetersToFtIn: any;
onGetPricingFormat: any;
Expand Down Expand Up @@ -130,16 +129,17 @@ const usePokemonDetailModalLogic = (): PokemonDetailModalReturn => {
pricingData,
]);

console.log('updateCartMutation', updateCartMutation);

return {
modalData,
speciesData,
isPendingUpdateCart: updateCartMutation?.isPending,
speciesIsError,
speciesIsLoading,
speciesIsFetching,
speciesIsLoading: speciesIsLoading || speciesIsFetching,
pricingData,
pricingIsError,
pricingIsLoading,
pricingIsFetching,
pricingIsLoading: pricingIsLoading || pricingIsFetching,
onConvertKgToLbs: convertKgToLbs,
onConvertMetersToFtIn: convertMetersToFtIn,
onGetPricingFormat: getPricingFormat,
Expand Down
1 change: 1 addition & 0 deletions libs/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './lib-base/ui/card';
export * from './lib-base/ui/hide.in.mobile';
export * from './lib-base/ui/tag';
export * from './lib-base/ui/switch';
export * from './lib-base/ui/skeleton';
21 changes: 19 additions & 2 deletions libs/ui/src/lib-base/styles/css/global.module.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
.fontBold {
font-weight: bold;
font-weight: bold !important;
}

.pointerCursor {
cursor: pointer;
cursor: pointer !important;
}

.disabledElement {
cursor: not-allowed !important;

/* opacity: 0.5 !important; */
}

.disabledElementText {
cursor: not-allowed !important;
color: var(--theme-netural-600) !important;
}

.disabledElementBg {
cursor: not-allowed !important;
color: var(--theme-netural-100) !important;
background-color: var(--theme-netural-600) !important;
}
1 change: 0 additions & 1 deletion libs/ui/src/lib-base/ui/button/button.module.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.button {
border-radius: var(--theme-radius-pill);
cursor: pointer;
background: none;
border: 0;
text-align: center;
}
Expand Down
14 changes: 13 additions & 1 deletion libs/ui/src/lib-base/ui/button/button.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { memo, ReactElement, useMemo } from 'react';
import { useRenderStyles } from '@pokemon-pet-shop/hooks';
import { classNamesUtil } from '@pokemon-pet-shop/utils';

import { globalStyles } from '../../styles/css/global.module';

import ButtonElement from './button.element.view';
import { ButtonSizeEnum, ButtonTypeEnum } from './button.enum';
import { ButtonProps } from './button.interface';
Expand Down Expand Up @@ -38,10 +40,20 @@ const Button = ({
}
}, [size, newStyles]);

const getDisabledStyles = useMemo(() => {
return isDisabled ? globalStyles.disabledElementBg : '';
}, [isDisabled]);

return (
<ButtonElement
text={text}
className={classNamesUtil(className, newStyles.button, getTypeStyles, getSizeStyles)}
className={classNamesUtil(
className,
newStyles.button,
getTypeStyles,
getSizeStyles,
getDisabledStyles
)}
isDisabled={isDisabled}
onClick={onClick}
/>
Expand Down
8 changes: 7 additions & 1 deletion libs/ui/src/lib-base/ui/icon/icon.enum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { THEME_NETURAL_100, THEME_RED_600, THEME_BLUE_600 } from '../../styles/ts/variables';
import {
THEME_NETURAL_100,
THEME_RED_600,
THEME_BLUE_600,
THEME_NETURAL_900,
} from '../../styles/ts/variables';

export enum IconTypeEnum {
ICON_SHOPPING_CART = 'IconShoppingCart',
Expand All @@ -18,4 +23,5 @@ export enum IconColorEnum {
BLUE = THEME_BLUE_600,
RED = THEME_RED_600,
WHITE = THEME_NETURAL_100,
BLACK = THEME_NETURAL_900,
}
7 changes: 0 additions & 7 deletions libs/ui/src/lib-base/ui/icon/icon.module.css

This file was deleted.

14 changes: 9 additions & 5 deletions libs/ui/src/lib-base/ui/icon/icon.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { memo, ReactElement, useCallback, useMemo } from 'react';

import { classNamesUtil } from '@pokemon-pet-shop/utils';

import { globalStyles } from '../../styles/css/global.module';
import { THEME_NETURAL_600 } from '../../styles/ts/variables';
import { UiElementLayout } from '../element.layout';

import { IconColorEnum, IconTypeEnum } from './icon.enum';
import { IconProps } from './icon.interface';
import styles from './icon.module.css';
import { IconMap } from './icon.util';

const Icon = ({
Expand Down Expand Up @@ -36,20 +37,23 @@ const Icon = ({
}, [onClick, isDisabled]);

const renderIcon = useMemo(() => {
const disabledStyles = isDisabled ? globalStyles.disabledElement : '';
const clickableStyles = onClick ? globalStyles.pointerCursor : '';

return (
<IconComponent
size={size}
color={color}
className={classNamesUtil(classNameIcon, onClick ? styles.icon : '')}
color={isDisabled ? THEME_NETURAL_600 : color}
className={classNamesUtil(classNameIcon, clickableStyles, disabledStyles)}
onClick={iconClick}
/>
);
}, [size, IconComponent, classNameIcon, iconClick, onClick]);
}, [size, IconComponent, color, classNameIcon, iconClick, isDisabled, onClick]);

if (displayWrapper) {
return (
<UiElementLayout
className={classNamesUtil(classNameWrapper, styles.iconWrapper)}
className={classNamesUtil(classNameWrapper, globalStyles.pointerCursor)}
onClick={wrapperClick}
>
{renderIcon}
Expand Down
4 changes: 2 additions & 2 deletions libs/ui/src/lib-base/ui/skeleton/skeleton.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { ReactElement } from 'react';

import Skeleton from 'react-loading-skeleton';

import { THEME_RADIUS_8, THEME_RADIUS_CIRCLE } from '../../styles/ts/variables';
import { UiElementLayout } from '../element.layout';
import { THEME_RADIUS_8 } from '../../styles/ts/variables';

import { SkeletonProps } from './skeleton.interface';

Expand Down Expand Up @@ -31,6 +30,7 @@ const SkeletonView = ({
return (
<Skeleton
key={i}
width={width}
height={circle ? '100%' : height}
borderRadius={THEME_RADIUS_8}
circle={circle}
Expand Down

0 comments on commit bb8498e

Please sign in to comment.