Skip to content
Snippets Groups Projects
ActionBegin.tsx 4.13 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Button } from '@material-ui/core'
    import defaultIcon from 'assets/icons/visu/ecogesture/default.svg'
    
    import ActionModal from 'components/Action/ActionModal/ActionModal'
    
    import StyledIcon from 'components/CommonKit/Icon/StyledIcon'
    
    import StarsContainer from 'components/CommonKit/StarsContainer/StarsContainer'
    import { Client, useClient } from 'cozy-client'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { useI18n } from 'cozy-ui/transpiled/react/I18n'
    
    import { Ecogesture, UserChallenge } from 'models'
    
    import React, { useCallback, useEffect, useState } from 'react'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import ActionService from 'services/action.service'
    
    import { useAppSelector } from 'store/hooks'
    
    import { importIconById } from 'utils/utils'
    import './actionBegin.scss'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    interface ActionBeginProps {
    
      action?: Ecogesture
      setShowList: React.Dispatch<React.SetStateAction<boolean>>
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      userChallenge: UserChallenge
    }
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    const ActionBegin = ({
    
      action,
      setShowList,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      userChallenge,
    }: ActionBeginProps) => {
    
      const client: Client = useClient()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const { t } = useI18n()
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const {
        global: { fluidTypes },
        profile: { isProfileTypeCompleted },
    
      } = useAppSelector(state => state.ecolyo)
    
      const [currentAction, setCurrentAction] = useState<Ecogesture>()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const [actionIcon, setActionIcon] = useState<string>('')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const [openLaunchModal, setOpenLaunchModal] = useState<boolean>(false)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const toggleLaunchModal = useCallback(() => {
        setOpenLaunchModal(prev => !prev)
      }, [])
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
      useEffect(() => {
    
        let subscribed = true
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        const getAction = async () => {
          if (action) {
            if (subscribed) setCurrentAction(action)
          } else {
    
            const actionService = new ActionService(client)
            let actions: Ecogesture[] = []
            if (isProfileTypeCompleted) {
              actions = await actionService.getCustomActions(fluidTypes)
            } else {
              actions = await actionService.getDefaultActions()
            }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            if (subscribed) {
              setCurrentAction(actions[0])
            }
    
    Yoan VALLET's avatar
    Yoan VALLET committed
        getAction()
    
        return () => {
          subscribed = false
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      }, [client, isProfileTypeCompleted, fluidTypes, action, currentAction])
    
    
      useEffect(() => {
    
        async function handleEcogestureIcon() {
          if (currentAction) {
    
            const icon = await importIconById(currentAction.id, 'ecogesture')
    
            setActionIcon(icon || defaultIcon)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
    
      }, [currentAction])
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
      return (
    
        <div className="action-begin">
    
          {currentAction && (
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            <div className="action-container">
              <div className="action-begin-container">
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                <div className="icon-container">
                  <StyledIcon
                    className="action-icon"
                    icon={actionIcon}
                    size={100}
                  />
                </div>
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                <StarsContainer
    
                  result={userChallenge.progress.actionProgress}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                  isQuizBegin={true}
                />
                <div className="action-title text-20-bold">
    
                  {currentAction.shortName}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                </div>
                <div className="action-duration text-18">
                  {t('action.duration', {
    
                    smartCount: currentAction.actionDuration,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                  })}
                </div>
                <div className="action-text text-18-bold">
    
                  {currentAction.actionName}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                </div>
                <div className="action-buttons">
                  <Button
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                    onClick={toggleLaunchModal}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                    classes={{
                      root: 'btn-secondary-negative',
                      label: 'text-16-normal',
                    }}
                  >
                    {t('action.apply')}
                  </Button>
                  <Button
    
                    onClick={() => setShowList(true)}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                    classes={{
                      root: 'btn-secondary-negative',
                      label: 'text-16-normal',
                    }}
                  >
                    {t('action.other')}
                  </Button>
                </div>
              </div>
    
    Yoan VALLET's avatar
    Yoan VALLET committed
              <ActionModal
                open={openLaunchModal}
                action={currentAction}
                handleCloseClick={toggleLaunchModal}
                userChallenge={userChallenge}
              />
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            </div>
          )}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      )
    }
    
    export default ActionBegin