Skip to content
Snippets Groups Projects
ActionCard.tsx 2.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Button } from '@material-ui/core'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import defaultIcon from 'assets/icons/visu/ecogesture/default.svg'
    
    import StyledIcon from 'components/CommonKit/Icon/StyledIcon'
    
    import EcogestureModal from 'components/Ecogesture/EcogestureModal/EcogestureModal'
    
    import { useClient } from 'cozy-client'
    
    import { UsageEventType } from 'enums'
    
    import { Ecogesture } from 'models'
    import React, { useCallback, useEffect, useState } from 'react'
    import UsageEventService from 'services/usageEvent.service'
    
    import { useAppSelector } from 'store/hooks'
    
    import { importIconById } from 'utils/utils'
    
    import './actionCard.scss'
    
    interface ActionCardProps {
      action: Ecogesture
      setSelectedAction: React.Dispatch<React.SetStateAction<Ecogesture | null>>
      setShowList: React.Dispatch<React.SetStateAction<boolean>>
    }
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const ActionCard = ({
    
      action,
      setSelectedAction,
      setShowList,
    }: ActionCardProps) => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const [actionIcon, setActionIcon] = useState<string>('')
      const [openEcogestureModal, setOpenEcogestureModal] = useState<boolean>(false)
    
      const client = useClient()
    
      const { currentChallenge } = useAppSelector(state => state.ecolyo.challenge)
    
      const toggleModal = useCallback(() => {
        setOpenEcogestureModal(prev => !prev)
      }, [])
    
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const selectEcogesture = useCallback(() => {
    
        setSelectedAction(action)
        setShowList(false)
    
        UsageEventService.addEvent(client, {
          type: UsageEventType.ACTION_CHANGE_EVENT,
          target: action.id,
          context: currentChallenge ? currentChallenge.id : '',
        })
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        toggleModal()
    
      }, [
        setSelectedAction,
        setShowList,
        action,
        toggleModal,
        currentChallenge,
        client,
      ])
    
    
      useEffect(() => {
    
          const icon = await importIconById(action.id, 'ecogesture')
    
          setActionIcon(icon || defaultIcon)
    
      }, [action])
    
      return (
        <>
          {action && (
            <Button key={action.id} className="action-card" onClick={toggleModal}>
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              <StyledIcon className="action-icon" icon={actionIcon} size={100} />
    
              <div className="action-title text-18-bold">{action.shortName}</div>
            </Button>
          )}
    
          {action && (
    
            <EcogestureModal
    
              open={openEcogestureModal}
    
              ecogesture={action}
              isAction={true}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              handleCloseClick={toggleModal}
              selectEcogesture={selectEcogesture}
    
            />
          )}
        </>
      )
    }
    
    export default ActionCard