Advertisement
pradiptaagus

Untitled

May 2nd, 2022
1,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState } from "react";
  2. import { Link, useHistory } from "react-router-dom";
  3. import Background from "../components/Background";
  4. import useScreenWidth from "../hooks/useScreenWidth";
  5. import images from "../images/images";
  6.  
  7. const OnBoarding = () => {
  8.     const history = useHistory();
  9.     const screenWidth = useScreenWidth();
  10.     const [currentSlideIndex, setCurrentSlideIndex] = useState<number>(0);
  11.     const [translate, setTranslate] = useState<number>(0);
  12.  
  13.     const [touchStart, setTouchStart] = useState<number>(0);
  14.     const [touchEnd, setTouchEnd] = useState<number>(0);
  15.  
  16.     const slideItems = [
  17.         {
  18.             image: images.welcome1,
  19.             title: "Permudah Pemasaran Produk",
  20.             subtitle:
  21.                 "SenyuM Mobile membantu Anda memasarkan produk-produk Ultra Mikro BRI, PNM, dan Pegadaian.",
  22.         },
  23.         {
  24.             image: images.welcome2,
  25.             title: "Akses Referral Ultra Mikro",
  26.             subtitle:
  27.                 "SenyuM Mobile membantu Anda memberikan akses referral produk keuangan Ultra Mikro kepada masyarakat Ultra Mikro.",
  28.         },
  29.         {
  30.             image: images.welcome3,
  31.             title: "Perluas Nasabah Ultra Mikro",
  32.             subtitle:
  33.                 "Manfaatkan referral Ultra Mikro untuk membentuk komunitas nasabah Ultra Mikro di wilayah kerja Anda.",
  34.         },
  35.     ];
  36.  
  37.     function next() {
  38.         const _index =
  39.             currentSlideIndex < slideItems.length - 1
  40.                 ? currentSlideIndex + 1
  41.                 : 0;
  42.         setCurrentSlideIndex(_index);
  43.         setTranslate(screenWidth * _index);
  44.     }
  45.  
  46.     function prev() {
  47.         const _index = currentSlideIndex > 0 ? currentSlideIndex - 1 : 0;
  48.         setCurrentSlideIndex(_index);
  49.         setTranslate(screenWidth * _index);
  50.     }
  51.  
  52.     function goTo(index: number) {
  53.         setTranslate(screenWidth * index);
  54.         setCurrentSlideIndex(index);
  55.     }
  56.  
  57.     return (
  58.         <Background>
  59.             <div
  60.                 className={`pt-8 px-8 text-right mb-16 ${
  61.                     currentSlideIndex === slideItems.length - 1
  62.                         ? "invisible"
  63.                         : "visible"
  64.                 }`}
  65.             >
  66.                 <Link
  67.                     to="/create-pin"
  68.                     className="font-sans-medium font-semibold text-base text-white border-b border-white w-initial"
  69.                 >
  70.                     Lewati
  71.                 </Link>
  72.             </div>
  73.  
  74.             {screenWidth && (
  75.                 <div
  76.                     className="transition-all duration-200 ease-in-out flex flex-wrap"
  77.                     style={{
  78.                         width: screenWidth * slideItems.length,
  79.                         transform: `translateX(-${translate}px)`,
  80.                     }}
  81.                     onTouchStart={(e: React.TouchEvent<HTMLDivElement>) =>
  82.                         setTouchStart(e.targetTouches[0].clientX)
  83.                     }
  84.                     onTouchMove={(e: React.TouchEvent<HTMLDivElement>) =>
  85.                         setTouchEnd(e.targetTouches[0].clientX)
  86.                     }
  87.                     onTouchEnd={(e: React.TouchEvent<HTMLDivElement>) => {
  88.                         // Go to next slide
  89.                         if (
  90.                             touchStart - touchEnd > 150 &&
  91.                             currentSlideIndex < slideItems.length - 1
  92.                         ) {
  93.                             next();
  94.                         }
  95.  
  96.                         // Go to previous slide
  97.                         else if (
  98.                             touchStart - touchEnd < -150 &&
  99.                             currentSlideIndex > 0
  100.                         ) {
  101.                             prev();
  102.                         }
  103.                     }}
  104.                 >
  105.                     {slideItems.map((item, index) => (
  106.                         <div style={{ width: screenWidth }} key={index}>
  107.                             <div className="flex justify-center">
  108.                                 <img src={item.image} alt="Welcome" />
  109.                             </div>
  110.                             <div className="mt-16 px-8">
  111.                                 <p className="font-sans-bold font-bold text-caption text-center text-white">
  112.                                     {item.title}
  113.                                 </p>
  114.                                 <p className="font-sans text-paragraph text-center text-white mt-2">
  115.                                     {item.subtitle}
  116.                                 </p>
  117.                             </div>
  118.                         </div>
  119.                     ))}
  120.                 </div>
  121.             )}
  122.  
  123.             <div className="absolute bottom-8 w-full">
  124.                 <div className="flex justify-center mb-7">
  125.                     {slideItems.map((_, index) => (
  126.                         <button key={index} onClick={() => goTo(index)}>
  127.                             <div
  128.                                 className={`bg-white h-2 rounded-sm mr-2 ${
  129.                                     index === currentSlideIndex ? "w-6" : "w-2"
  130.                                 } ${
  131.                                     index < slideItems.length - 1
  132.                                         ? "mr-2"
  133.                                         : "mr-0"
  134.                                 }`}
  135.                             ></div>
  136.                         </button>
  137.                     ))}
  138.                 </div>
  139.                 <div className="flex justify-center">
  140.                     <button
  141.                         className="btn btn-md btn-primary"
  142.                         style={{ width: "140px" }}
  143.                         onClick={() => {
  144.                             if (currentSlideIndex >= slideItems.length - 1) {
  145.                                 history.push("/create-pin");
  146.                                 return;
  147.                             } else {
  148.                                 next();
  149.                             }
  150.                         }}
  151.                     >
  152.                         {currentSlideIndex === slideItems.length - 1
  153.                             ? "Mulai Registrasi"
  154.                             : "Selanjutnya"}
  155.                     </button>
  156.                 </div>
  157.             </div>
  158.         </Background>
  159.     );
  160. };
  161.  
  162. export default OnBoarding;
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement