Skip to content

Commit

Permalink
refactor(HomePage): limit the use of HTML in React
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyngcphu committed Sep 23, 2023
1 parent 9124008 commit a6357a6
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 166 deletions.
28 changes: 23 additions & 5 deletions __mock-server__/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"orders": [
{
"id": "1233-defc",
"status": "Progressing",
"status": "progressing",
"location": "Thu Duc, Ho Chi Minh, Viet Nam",
"number": 2,
"fileName": "file1.pdf",
Expand All @@ -12,7 +12,7 @@
},
{
"id": "1245-afce",
"status": "Ready",
"status": "ready",
"location": "Tan Phu, Ho Chi Minh, Viet Nam",
"number": 2,
"fileName": "file2.docx",
Expand All @@ -22,7 +22,7 @@
},
{
"id": "1267-rfce",
"status": "Done",
"status": "done",
"location": "Tan Phu, Ho Chi Minh, Viet Nam",
"number": 2,
"fileName": "file3.txt",
Expand All @@ -32,7 +32,7 @@
},
{
"id": "1267-rfce",
"status": "Canceled",
"status": "canceled",
"location": "Tan Phu, Ho Chi Minh, Viet Nam",
"number": 2,
"fileName": "file3.txt",
Expand All @@ -42,13 +42,31 @@
},
{
"id": "1267-rfce",
"status": "Done",
"status": "done",
"location": "Tan Phu, Ho Chi Minh, Viet Nam",
"number": 2,
"fileName": "file3.txt",
"pageNumber": 5,
"coins": 30,
"paid": "Paid"
}
],
"slides": [
{
"src": "https://e-learning.hcmut.edu.vn/theme/boost/images/slbktv.jpg?1695219022292",
"alt": "slide1"
},
{
"src": "https://e-learning.hcmut.edu.vn/theme/boost/images/slbk.jpg?1695219022805",
"alt": "slide2"
},
{
"src": "https://static.tuoitre.vn/tto/i/s626/2016/04/05/dai-hoc-bach-khoa-tp-hcm-cong-bo-phuong-an-tuyen-sinh-2015jpg-1459820200.jpg",
"alt": "slide3"
},
{
"src": "https://we25.vn/media2018/Img_News/2020/06/08/khuon-vien-dh-bach-khoa-tp-hcm-khien-dan-tinh-me-man-voi-nhung-hanh-cay-xanh-tham-toa-bong-mat-quanh-nam-14_20200608150159.jpg",
"alt": "slide4"
}
]
}
Binary file removed src/assets/location-marker.jpg
Binary file not shown.
Binary file removed src/assets/printer.jpg
Binary file not shown.
103 changes: 53 additions & 50 deletions src/components/home/Orders.tsx
Original file line number Diff line number Diff line change
@@ -1,76 +1,79 @@
import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline';
import { useRef } from 'react';
import location from '@assets/location-marker.jpg';
import { Card, CardBody } from '@material-tailwind/react';
import { ArrowRightIcon, ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/24/outline';
import { MapPinIcon } from '@heroicons/react/24/solid';
import coin from '@assets/coin.png';
import { ORDER_STATUS } from '@constants';

export const Orders: Component<{ orders: OrderData[] }> = ({ orders }) => {
const sliderRef = useRef(document.createElement('div'));
const sliderRef = useRef<HTMLDivElement>(null);
/* Initialize sliderRef with useRef<HTMLDivElement>(null) instead of
document.createElement('div'). We don't need to create a div element using
document.createElement because we want to use a reference to an existing DOM element.*/

const scrollLeft = () => {
sliderRef.current.scrollLeft -= 400;
if (sliderRef.current) {
sliderRef.current.scrollLeft -= 400;
}
};
const scrollRight = () => {
sliderRef.current.scrollLeft += 400;
if (sliderRef.current) {
sliderRef.current.scrollLeft += 400;
}
};
return (
<div className='relative'>
<div className='flex justify-between items-center mb-4'>
<h3 className='text-[#495057] text-xl lg:text-4xl'>Order in progress</h3>
<div className='flex cursor-pointer opacity-40'>
<span className='text-[#495057] text-sm lg:text-lg mr-[5px] font-semibold opacity-40 hover:opacity-100'>
See more
</span>
<ArrowRightIcon className='w-3' />
<h3 className='font-semibold text-gray/4 text-xl lg:text-4xl'>Order in progress</h3>
<div className='flex items-center gap-1 cursor-pointer opacity-40 hover:opacity-100'>
<span className='text-gray/4 text-sm lg:text-lg font-semibold'>See more</span>
<ArrowRightIcon strokeWidth={3} className='w-4 h-4' />
</div>
</div>
<div className='flex gap-[8px] lg:gap-[24px] slider-container' ref={sliderRef}>
{orders.map((order, index) => {
return (
<div
key={index}
className='p-2 lg:p-4 bg-white grow shrink-0 flex flex-col rounded-lg border-[1px] shadow-[rgba(0, 0, 0, 0.05)] shadow-md'
>
<div className='text-xs flex justify-between mb-2 lg:mb-4'>
<span className='font-semibold leading-[18px]'>{order.id}</span>
<span
className={
order.status === 'Progressing'
? 'text-[#C27803]'
: order.status === 'Ready'
? 'text-[#0E9F6E]'
: order.status === 'Done'
? 'text-[#6875F5]'
: 'text-[#F05252]'
}
>
<div className='flex gap-2 lg:gap-6 slider-container' ref={sliderRef}>
{orders.map((order, index) => (
<Card key={index} className='grow shrink-0 flex flex-col rounded-lg shadow-md'>
<CardBody className='p-2 lg:p-4'>
<div className='flex items-center justify-between font-semibold text-xs lg:text-base mb-2 lg:mb-4'>
<span>{order.id}</span>
<span className={`capitalize text-${ORDER_STATUS[order.status]}-500`}>
{order.status}
</span>
</div>
<div className=''>
<span className='text-[#1488D8] mr-1'>{order.fileName}</span>
<span className='text-[#495057] opacity-40'>+{order.number}</span>
<div>
<span className='text-blue/1 mr-1'>{order.fileName}</span>
<span className='text-gray/4 opacity-40'>+{order.number}</span>
</div>
<div className='flex items-center mb-4'>
<img src={location} width={20} height={20} className='mr-2' />
<span className='text-[#ACB5BD]'>{order.location}</span>
<div className='flex items-center text-gray/3 mb-4'>
<MapPinIcon className='w-5 h-5 mr-1' />
<span>{order.location}</span>
</div>
<hr className='h-[1px] mb-4' />
<div className='flex items-center justify-between'>
<span className='text-[#495057] font-medium'>{order.pageNumber} pages</span>
<div className='flex'>
<img src={coin} width={20} />
<span className='mr-2'>{order.coins}</span>
<span className='text-[#495057] opacity-40'>({order.paid})</span>
<div className='flex items-center justify-between text-gray/4 text-base lg:text-lg'>
<p>
<span className='font-medium'>{order.pageNumber} </span>
<span className='font-normal'>pages</span>
</p>
<div className='flex items-center text-base lg:text-lg'>
<img src={coin} alt='coin' width={25} />
<span className='text-amber-600 font-bold mr-2'>{order.coins}</span>
<span className='text-gray/4 opacity-40 font-normal'>({order.paid})</span>
</div>
</div>
</div>
);
})}
<div className='prevButton hidden lg:block left-[-72px] opacity-60 hover:opacity-100 bg-white '>
<ArrowLeftIcon className='w-4 text-[#1488D8]' onClick={scrollLeft} />
</CardBody>
</Card>
))}
<div
className='prevButton hidden lg:block left-[-72px] opacity-60 hover:opacity-100 bg-blue-100'
onClick={scrollLeft}
>
<ChevronLeftIcon strokeWidth={3} className='w-6 h-6 text-blue/1' />
</div>
<div className='nextButton hidden lg:block right-[-72px] opacity-60 hover:opacity-100 bg-white'>
<ArrowRightIcon className='w-4 text-[#1488D8]' onClick={scrollRight} />
<div
className='nextButton hidden lg:block right-[-72px] opacity-60 hover:opacity-100 bg-blue-100'
onClick={scrollRight}
>
<ChevronRightIcon strokeWidth={3} className='w-6 h-6 text-blue/1' />
</div>
</div>
</div>
Expand Down
85 changes: 28 additions & 57 deletions src/components/home/Slides.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,32 @@
import { useState } from 'react';
import { FC } from 'react';
import { ArrowLeftIcon, ArrowRightIcon } from '@heroicons/react/24/outline';
import { Carousel } from '@material-tailwind/react';
import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/24/outline';

interface Slide {
src: string;
alt: string;
}
interface SlidesProps {
children: Slide[];
}
export const Slides: FC<SlidesProps> = ({ children }) => {
const [slide, setSlide] = useState(0);
const slideLength = children.length;
const handleleftArrow = () => {
const previousSlide = slide === 0 ? slideLength - 1 : slide - 1;
setSlide(previousSlide);
};
const handleRightArrow = () => {
const nextSlide = slide === slideLength - 1 ? 0 : slide + 1;
setSlide(nextSlide);
};
setTimeout(handleRightArrow, 3000);
export const Slides: Component<{ slides: SlideData[] }> = ({ slides }) => {
return (
<div className='relative mb-4 w-full lg:mb-12'>
<div className='absolute top-[40%] left-[5%] p-[5px] rounded-[999px] bg-white opacity-40 cursor-pointer hover:opacity-100'>
<ArrowLeftIcon className='w-4' onClick={handleleftArrow} />
</div>
{children.map((child, index) => {
return (
<img
src={child.src}
alt={child.alt}
key={index}
className={
slide === index ? 'rounded-lg h-[200px] object-cover w-full lg:h-[400px]' : 'hidden'
}
/>
);
})}
<div className='absolute top-[40%] right-[5%] p-[5px] rounded-[999px] bg-white opacity-40 cursor-pointer hover:opacity-100'>
<ArrowRightIcon className='w-4 ' onClick={handleRightArrow} />
</div>
<span className='flex absolute bottom-[1rem] left-[40%] lg:left-[50%]'>
{children.map((_, idx) => {
return (
<button
key={idx}
onClick={() => setSlide(idx)}
className={
slide === idx
? 'bg-white h-[0.5rem] w-[0.5rem] rounded-full border-0 outline-0 my-0 mx-[0.2rem] cursor-pointer'
: 'h-[0.5rem] w-[0.5rem] rounded-full border-0 outline-0 my-0 mx-[0.2rem] cursor-pointer bg-[#9CA3AF]'
}
></button>
);
})}
</span>
</div>
<Carousel
className='rounded-lg'
prevArrow={({ handlePrev }) => (
<div className='absolute top-[40%] left-[5%] p-[5px] rounded-full bg-white opacity-40 cursor-pointer hover:opacity-100'>
<ChevronLeftIcon strokeWidth={4} className='w-5 h-5' onClick={handlePrev} />
</div>
)}
nextArrow={({ handleNext }) => (
<div className='absolute top-[40%] right-[5%] p-[5px] rounded-full bg-white opacity-40 cursor-pointer hover:opacity-100'>
<ChevronRightIcon strokeWidth={4} className='w-5 h-5' onClick={handleNext} />
</div>
)}
loop={true}
autoplay={true}
autoplayDelay={3000}
>
{slides.map((slide, index) => (
<img
key={index}
src={slide.src}
alt={slide.alt}
className='h-[200px] object-cover w-full lg:h-[400px]'
/>
))}
</Carousel>
);
};
6 changes: 6 additions & 0 deletions src/constants/home.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const ORDER_STATUS = {
progressing: 'amber',
ready: 'green',
done: 'indigo',
canceled: 'red'
};
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
* @file Automatically generated by barrelsby.
*/

export * from './project';
export * from './home';
export * from './screen';
8 changes: 0 additions & 8 deletions src/constants/project.ts

This file was deleted.

66 changes: 24 additions & 42 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
import { useEffect } from 'react';
import { ArrowRightIcon, PrinterIcon } from '@heroicons/react/24/outline';
import { Orders, Slides } from '@components/home';
import printerPicture from '@assets/printer.jpg';
import { ArrowRightIcon } from '@heroicons/react/24/outline';
import axios from 'axios';
import { useEffect, useState } from 'react';
import { useOrderStore, useSlideStore } from '@states/home';

export function HomePage() {
const [orders, setOrders] = useState([]);
const { orderData, getOrderData } = useOrderStore();
const { slideData, getSlideData } = useSlideStore();

useEffect(() => {
const getOrders = async () => {
try {
const result = await axios.get('http://localhost:3002/orders');
setOrders(result.data);
} catch (e) {
console.log(e);
}
};
getOrders();
}, []);
const slidePictures = [
{
src: 'https://e-learning.hcmut.edu.vn/theme/boost/images/slbktv.jpg?1695219022292',
alt: 'slide1'
},
{
src: 'https://e-learning.hcmut.edu.vn/theme/boost/images/slbk.jpg?1695219022805',
alt: 'slide2'
},
{
src: 'https://static.tuoitre.vn/tto/i/s626/2016/04/05/dai-hoc-bach-khoa-tp-hcm-cong-bo-phuong-an-tuyen-sinh-2015jpg-1459820200.jpg',
alt: 'slide3'
},
{
src: 'https://we25.vn/media2018/Img_News/2020/06/08/khuon-vien-dh-bach-khoa-tp-hcm-khien-dan-tinh-me-man-voi-nhung-hanh-cay-xanh-tham-toa-bong-mat-quanh-nam-14_20200608150159.jpg',
alt: 'slide4'
}
];
getOrderData();
getSlideData();
}, [getOrderData, getSlideData]);

return (
<div className='p-6 lg:py-5 lg:px-56'>
<div className='mb-4 lg:mb-6'>
<h4 className='text-[#1488D8] text-base lg:text-2xl'>
<h4 className='font-normal text-blue/1 text-base lg:text-2xl'>
Student Smart Printing Service (SSPS)
</h4>
<h3 className='text-[#030391] text-xl lg:text-4xl font-bold'>Welcome Username!</h3>
<h3 className='font-bold text-blue/2 text-xl lg:text-4xl'>Welcome Username!</h3>
</div>
<div className='mb-4 lg:mb-12'>
<Slides slides={slideData} />
</div>
<Slides children={slidePictures} />
<div className='flex p-4 bg-[#1488D8] rounded-lg items-center mb-16 lg:mb-24 lg:h-[120px] lg:px-6 relative'>
<div className='p-[10px] rounded-full bg-white mr-4'>
<img src={printerPicture} />
<div className='flex items-center justify-between bg-blue/1 rounded-lg p-4 mb-16 lg:mb-24 lg:h-[120px] lg:px-6'>
<div className='flex items-center gap-4'>
<PrinterIcon
strokeWidth={2}
className='bg-white text-blue-500 rounded-full w-10 h-10 p-2 lg:w-12 lg:h-12 lg:p-3'
/>
<span className='text-white font-bold lg:text-2xl'>Order printing</span>
</div>
<span className='text-white font-bold lg:text-2xl'>Order printing</span>
<ArrowRightIcon className='w-4 text-white absolute top-50 right-[24px] lg:w-6' />
<ArrowRightIcon strokeWidth={3} className='text-white w-4 h-4 lg:w-6 lg:h-6' />
</div>
<Orders orders={orders} />
<Orders orders={orderData} />
</div>
);
}
1 change: 1 addition & 0 deletions src/services/home/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*/

export * from './order';
export * from './slide';
Loading

0 comments on commit a6357a6

Please sign in to comment.