| 
 | 1 | +import React, { useState, FC } from 'react'  | 
 | 2 | +import SwipeableViews from 'react-swipeable-views'  | 
 | 3 | + | 
 | 4 | +import { FixedActionsDialog } from 'cozy-ui/transpiled/react/CozyDialogs'  | 
 | 5 | +import CozyTheme from 'cozy-ui/transpiled/react/providers/CozyTheme'  | 
 | 6 | +import MobileStepper from 'cozy-ui/transpiled/react/MobileStepper'  | 
 | 7 | +import IconButton from 'cozy-ui/transpiled/react/IconButton'  | 
 | 8 | +import Icon from 'cozy-ui/transpiled/react/Icon'  | 
 | 9 | +import LeftIcon from 'cozy-ui/transpiled/react/Icons/Left'  | 
 | 10 | +import RightIcon from 'cozy-ui/transpiled/react/Icons/Right'  | 
 | 11 | +import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints'  | 
 | 12 | + | 
 | 13 | +import { AnnouncementsDialogContent } from './AnnouncementsDialogContent'  | 
 | 14 | +import { Announcement } from './types'  | 
 | 15 | +import { useAnnouncementsSettings } from 'hooks/useAnnouncementsSettings'  | 
 | 16 | + | 
 | 17 | +interface AnnouncementsDialogProps {  | 
 | 18 | +  announcements: Array<Announcement>  | 
 | 19 | +  onDismiss: () => void  | 
 | 20 | +}  | 
 | 21 | + | 
 | 22 | +const AnnouncementsDialog: FC<AnnouncementsDialogProps> = ({  | 
 | 23 | +  announcements,  | 
 | 24 | +  onDismiss  | 
 | 25 | +}) => {  | 
 | 26 | +  const { values, save } = useAnnouncementsSettings()  | 
 | 27 | +  const { isMobile } = useBreakpoints()  | 
 | 28 | + | 
 | 29 | +  const [activeStep, setActiveStep] = useState(0)  | 
 | 30 | + | 
 | 31 | +  const handleBack = (): void => {  | 
 | 32 | +    setActiveStep(activeStep - 1)  | 
 | 33 | +  }  | 
 | 34 | + | 
 | 35 | +  const handleNext = (): void => {  | 
 | 36 | +    const uuid = announcements[activeStep].attributes.uuid  | 
 | 37 | +    if (!values?.seen.includes(uuid)) {  | 
 | 38 | +      save({  | 
 | 39 | +        seen: [...(values?.seen ?? []), uuid]  | 
 | 40 | +      })  | 
 | 41 | +    }  | 
 | 42 | +    setActiveStep(activeStep + 1)  | 
 | 43 | +  }  | 
 | 44 | + | 
 | 45 | +  const handleChangedIndex = (index: number): void => {  | 
 | 46 | +    setActiveStep(index)  | 
 | 47 | +  }  | 
 | 48 | + | 
 | 49 | +  const maxSteps = announcements.length  | 
 | 50 | + | 
 | 51 | +  return (  | 
 | 52 | +    <CozyTheme variant="normal">  | 
 | 53 | +      <FixedActionsDialog  | 
 | 54 | +        open  | 
 | 55 | +        onClose={onDismiss}  | 
 | 56 | +        content={  | 
 | 57 | +          <SwipeableViews  | 
 | 58 | +            index={activeStep}  | 
 | 59 | +            onChangeIndex={handleChangedIndex}  | 
 | 60 | +            animateTransitions={isMobile}  | 
 | 61 | +          >  | 
 | 62 | +            {announcements.map((announcement, index) => (  | 
 | 63 | +              <AnnouncementsDialogContent  | 
 | 64 | +                key={index}  | 
 | 65 | +                isLast={index === maxSteps - 1}  | 
 | 66 | +                announcement={announcement}  | 
 | 67 | +                onDismiss={onDismiss}  | 
 | 68 | +                onNext={handleNext}  | 
 | 69 | +              />  | 
 | 70 | +            ))}  | 
 | 71 | +          </SwipeableViews>  | 
 | 72 | +        }  | 
 | 73 | +        actions={  | 
 | 74 | +          maxSteps > 1 ? (  | 
 | 75 | +            <MobileStepper  | 
 | 76 | +              className="u-mh-auto"  | 
 | 77 | +              steps={maxSteps}  | 
 | 78 | +              position="static"  | 
 | 79 | +              activeStep={activeStep}  | 
 | 80 | +              nextButton={  | 
 | 81 | +                <IconButton  | 
 | 82 | +                  onClick={handleNext}  | 
 | 83 | +                  disabled={activeStep === maxSteps - 1}  | 
 | 84 | +                >  | 
 | 85 | +                  <Icon icon={RightIcon} />  | 
 | 86 | +                </IconButton>  | 
 | 87 | +              }  | 
 | 88 | +              backButton={  | 
 | 89 | +                <IconButton onClick={handleBack} disabled={activeStep === 0}>  | 
 | 90 | +                  <Icon icon={LeftIcon} />  | 
 | 91 | +                </IconButton>  | 
 | 92 | +              }  | 
 | 93 | +            />  | 
 | 94 | +          ) : null  | 
 | 95 | +        }  | 
 | 96 | +      />  | 
 | 97 | +    </CozyTheme>  | 
 | 98 | +  )  | 
 | 99 | +}  | 
 | 100 | + | 
 | 101 | +export { AnnouncementsDialog }  | 
0 commit comments