import React, { useCallback, ChangeEvent, useReducer, useState, useEffect, CSSProperties, KeyboardEvent, useRef } from 'react'; import classNames from 'classnames'; import DesignerUpdatesSubscribeButtonStates, { DesignerUpdatesSubscribeButtonState } from './designer_updates_subscribe_button_states'; import WarningIcon from '@moda/icons/warning-16'; import {colors} from 'moda-themes'; import './designer_updates_subscribe_button.scss'; const progressStartPoint = 0; const progressIncrementalUnit = 2.5; const progressMaximumWidth = 100; const progressSpeed = 2.5; const progressColor = '#D8B2D1'; const threeFourths = 0.75; const enterKeyCode = 13; interface Props { email: string | undefined; gender: string; placement: string; trunkshowId: string; } type Status = 'initial' | 'subscribed' | 'error'; interface State { step: number; email: string; status: Status; } type Action = | {type: 'INCREMENT'; payload: number} | {type: 'SET_EMAIL'; payload: string} | {type: 'SET_STATUS'; payload: Status}; const blockName = 'DesignerUpdatesSubscribeButton'; const reducer = (state: State, action: Action): State => { switch (action.type) { case 'INCREMENT': return {...state, step: action.payload}; case 'SET_EMAIL': return {...state, email: action.payload}; case 'SET_STATUS': return {...state, status: action.payload}; } }; const DesignerUpdatesSubscribeButton: React.FC = (props: Props) => { const [progress, setProgress] = useState(0); const [state, dispatch] = useReducer(reducer, { step: 0, email: props.email || '', status: 'initial' }); const [disabled, setDisabled] = useState( Boolean(DesignerUpdatesSubscribeButtonStates[state.step].onChangeCallback) ); const [grabbedBackgroundColor, setGrabbedBackgroundColor] = useState( 'transparent' ); const rootRef = useRef(); const authenticated = Boolean(props.email); const handleStartProgress = useCallback(() => { setProgress(progressStartPoint + progressIncrementalUnit); }, []); useEffect(() => { if ( (progress > progressStartPoint && progress <= threeFourths * progressMaximumWidth - progressIncrementalUnit) || (progress >= threeFourths * progressMaximumWidth && progress <= progressMaximumWidth && state.status === 'subscribed') ) { setTimeout(() => { setProgress(progress + progressIncrementalUnit); }, progressSpeed); } else if (state.status === 'error') { setProgress(0); } }, [progress, state.status]); const handleClick = useCallback(async () => { let newIndex = state.step + 1; if (authenticated) { const foundIndex = DesignerUpdatesSubscribeButtonStates.findIndex( (item: DesignerUpdatesSubscribeButtonState, index: number) => item.unauthenticatedOnly === false && index > state.step ); if (foundIndex > newIndex) { newIndex = foundIndex; } } if (DesignerUpdatesSubscribeButtonStates[newIndex]) { const beforeChangeAction = DesignerUpdatesSubscribeButtonStates[newIndex].before; if (beforeChangeAction) { const args = {}; beforeChangeAction.args.forEach((arg: string) => Object.assign(args, { [arg]: state[arg as keyof State] ? state[arg as keyof State] : props[arg as keyof Props] }) ); handleStartProgress(); const middleActionResult = await beforeChangeAction.action(args); if (!middleActionResult) { dispatch({type: 'SET_STATUS', payload: 'error'}); return; } dispatch({type: 'SET_STATUS', payload: 'subscribed'}); } } if (newIndex < DesignerUpdatesSubscribeButtonStates.length) { dispatch({type: 'INCREMENT', payload: newIndex}); } }, [authenticated, handleStartProgress, props, state]); const handleChange = useCallback((event: ChangeEvent) => { dispatch({type: 'SET_EMAIL', payload: event.target.value}); }, []); useEffect(() => { const {onChangeCallback} = DesignerUpdatesSubscribeButtonStates[state.step]; if (onChangeCallback) { const args = {}; onChangeCallback.args.forEach((arg: string) => Object.assign(args, {[arg]: state[arg as keyof State]}) ); if (onChangeCallback.result === 'disable') { setDisabled(!onChangeCallback.action(args) as boolean); } } }, [state]); const handleKeyPress = useCallback( (event: KeyboardEvent) => { if (event.charCode === enterKeyCode && progress === 0 && !disabled) { handleClick(); } }, [disabled, handleClick, progress] ); const getIcon = useCallback( (placement: 'left' | 'right') => { if (DesignerUpdatesSubscribeButtonStates[state.step].icon) { if ( placement === DesignerUpdatesSubscribeButtonStates[state.step].icon.placement ) { return (
{disabled ? ( ) : ( DesignerUpdatesSubscribeButtonStates[state.step].icon.component )}
); } } return; }, [state.step, disabled, progress, handleClick] ); useEffect(() => { setDisabled( Boolean(DesignerUpdatesSubscribeButtonStates[state.step].onChangeCallback) ); }, [state.step]); const rootClassName = classNames( blockName, `${blockName}--state-${DesignerUpdatesSubscribeButtonStates[state.step].name}`, {[`${blockName}--disabled`]: disabled} ); useEffect(() => { if (rootRef.current) { const backgroundColor = getComputedStyle( rootRef.current ).getPropertyValue('background-color'); setGrabbedBackgroundColor(backgroundColor); } }, [state.step]); const rootStyle: CSSProperties = { background: progress > 0 ? `linear-gradient(90deg, ${progressColor} ${progress}%, ${grabbedBackgroundColor} 0)` : undefined }; const leftIcon = getIcon('left'); const rightIcon = getIcon('right'); const commonProps = { className: rootClassName, style: rootStyle, ref: rootRef }; switch (DesignerUpdatesSubscribeButtonStates[state.step].type) { case 'button': { return ( ); } case 'input': { return (
{leftIcon} {rightIcon}
); } } }; export default DesignerUpdatesSubscribeButton;