From 68955395401687b42ec8c4b9d64e3dea0a034620 Mon Sep 17 00:00:00 2001 From: sonnguyenkieio Date: Thu, 9 Sep 2021 14:28:25 +0700 Subject: [PATCH] module account --- .../AccountNavigation/AccountNavigation.tsx | 79 +++++------- .../AccountNavigationItem.module.scss | 1 - .../components/AccountNavigationItem.tsx | 9 +- .../account/AccountPage/AccountPage.tsx | 114 ++++++++---------- .../AccountInfomation/AccountInfomation.tsx | 8 +- 5 files changed, 89 insertions(+), 122 deletions(-) diff --git a/src/components/modules/account/AccountNavigation/AccountNavigation.tsx b/src/components/modules/account/AccountNavigation/AccountNavigation.tsx index 928ab3a5e..97bb0733c 100644 --- a/src/components/modules/account/AccountNavigation/AccountNavigation.tsx +++ b/src/components/modules/account/AccountNavigation/AccountNavigation.tsx @@ -1,72 +1,49 @@ -import React, { useState, useRef, RefObject, useEffect } from "react" +import React, { useRef, RefObject, useEffect } from "react" import s from './AccountNavigation.module.scss' import AccountNavigationItem from './components/AccountNavigationItem' interface AccountNavigationProps { - setAccountActive: ()=>void; - setOrderActive: ()=>void; - setFavActive: ()=>void; + items: {ref: RefObject, active: boolean, itemName: string, onClick: (tabIndex: number)=>void}[]; + defaultActiveIndex: number; } -const AccountNavigation = ({ setAccountActive, setOrderActive, setFavActive } : AccountNavigationProps) => { - const active = "active", unActive = ""; +const AccountNavigation = ({ items, defaultActiveIndex } : AccountNavigationProps) => { - const [item1Active, setItem1Active] = useState(unActive); - const [item2Active, setItem2Active] = useState(active); - const [item3Active, setItem3Active] = useState(unActive); + const sliderRef = useRef(null); - const item1 = useRef(null); - const item2 = useRef(null); - const item3 = useRef(null); - const slider = useRef(null); - - function slide(ref: RefObject) { - const top = ref.current.offsetTop; - slider.current.style.top = top.toString()+"px"; + function slide(index: number) { + const previousItem = items[index].ref.current; + const slider = sliderRef.current; + if (previousItem && slider) { + const top = previousItem.offsetTop; + slider.style.top = top.toString()+"px"; + } } - function toggleItem1():void { - setItem1Active(active); - setAccountActive(); - - setItem2Active(unActive); - setItem3Active(unActive); - slide(item1); - } - function toggleItem2():void { - setItem2Active(active); - setOrderActive(); - - setItem1Active(unActive); - setItem3Active(unActive); - slide(item2); - } - function toggleItem3():void { - setItem3Active(active); - setFavActive(); - - setItem1Active(unActive); - setItem2Active(unActive); - slide(item3); + const handleClick = (item: {ref: RefObject, active: boolean, itemName: string, onClick: (tabIndex: number)=>void}, + index: number) => { + slide(index); + item.onClick(index); + } useEffect(() => { - slide(item2); + slide(defaultActiveIndex); }, []) return (
-
- Customer Information -
-
- Your Orders -
-
- Favourites -
-
+ { + items.map((item, i) => { + return ( +
+ {handleClick(item, i)}} active={item.active}>{item.itemName} +
+ ) + }) + } +
) } diff --git a/src/components/modules/account/AccountNavigation/components/AccountNavigationItem.module.scss b/src/components/modules/account/AccountNavigation/components/AccountNavigationItem.module.scss index 179c1ab08..10037cdc7 100644 --- a/src/components/modules/account/AccountNavigation/components/AccountNavigationItem.module.scss +++ b/src/components/modules/account/AccountNavigation/components/AccountNavigationItem.module.scss @@ -13,6 +13,5 @@ &.active { background-color: #FBFBFB; border-radius: 0 1.6rem 1.6rem 0; - // border-left: 2px solid var(--primary); } } \ No newline at end of file diff --git a/src/components/modules/account/AccountNavigation/components/AccountNavigationItem.tsx b/src/components/modules/account/AccountNavigation/components/AccountNavigationItem.tsx index 8bb18cb58..dd53909e7 100644 --- a/src/components/modules/account/AccountNavigation/components/AccountNavigationItem.tsx +++ b/src/components/modules/account/AccountNavigation/components/AccountNavigationItem.tsx @@ -1,18 +1,17 @@ -import React, { RefObject } from "react"; +import React from "react"; import classNames from "classnames"; import s from './AccountNavigationItem.module.scss' interface AccountNavigationItemProps { children?: string; - active?: string; - target?: string; + active?: boolean; onClick: () => void; } -const AccountNavigationItem = ({ children, active="", onClick } : AccountNavigationItemProps) => { +const AccountNavigationItem = ({ children, active, onClick } : AccountNavigationItemProps) => { return (
{children}
diff --git a/src/components/modules/account/AccountPage/AccountPage.tsx b/src/components/modules/account/AccountPage/AccountPage.tsx index cbe808239..782b59735 100644 --- a/src/components/modules/account/AccountPage/AccountPage.tsx +++ b/src/components/modules/account/AccountPage/AccountPage.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from "react" +import React, { useRef, useState } from "react" import s from './AccountPage.module.scss' import AccountNavigation from '../AccountNavigation/AccountNavigation' @@ -7,70 +7,50 @@ import AccountInfomation from "./components/AccountInfomation/AccountInfomation" import OrderInfomation from './components/OrderInformation/OrderInformation' import EditInfoModal from './components/EditInfoModal/EditInfoModal' -interface AccountPageProps { +const waiting = [ + { + id: "NO 123456", + products: ["Tomato", "Fish", "Pork", "Onion"], + totalPrice : 1000 + } +] +const delivering = [ + { + id: "NO 123456", + products: ["Tomato", "Fish", "Pork", "Onion"], + totalPrice : 1000 + } +] + +const delivered = [ + { + id: "NO 123456", + products: ["Tomato", "Fish", "Pork", "Onion"], + totalPrice : 1000 + } +] + +let account = { + name: "vu duong", + email: "vuduong@gmail.com", + address: "234 Dien Bien Phu Bis, Dakao ward", + state: "District 1", + city: "HCMC", + postalCode: "700000", + phoneNumber: "(+84) 937 937 195" } -const AccountPage = ({} : AccountPageProps) => { - const waiting = [ - { - id: "NO 123456", - products: ["Tomato", "Fish", "Pork", "Onion"], - totalPrice : 1000 - } - ] - - const delivering = [ - { - id: "NO 123456", - products: ["Tomato", "Fish", "Pork", "Onion"], - totalPrice : 1000 - } - ] - - const delivered = [ - { - id: "NO 123456", - products: ["Tomato", "Fish", "Pork", "Onion"], - totalPrice : 1000 - } - ] - - let account = { - name: "vu duong", - email: "vuduong@gmail.com", - address: "234 Dien Bien Phu Bis, Dakao ward", - state: "District 1", - city: "HCMC", - postalCode: "700000", - phoneNumber: "(+84) 937 937 195" - } - - const [accountInfoActive, setAccountInfoActive] = useState(false); - const [orderInfoActive, setOrderInfoActive] = useState(true); - const [favoritesActive, setFavoritesActive] = useState(false); - - function accountActive() { - setAccountInfoActive(true); - setOrderInfoActive(false); - setFavoritesActive(false); - } - - function orderActive() { - setAccountInfoActive(false); - setOrderInfoActive(true); - setFavoritesActive(false); - } - - function favActive() { - setAccountInfoActive(false); - setOrderInfoActive(false); - setFavoritesActive(true); - } +interface AccountPageProps { + defaultActiveContent?: "info" | "orders" | "favorites" +} +const AccountPage = ({defaultActiveContent="orders"} : AccountPageProps) => { + + const [activeTab, setActiveTab] = useState(defaultActiveContent==="info"? 0 : defaultActiveContent==="orders"? 1 : 2) const [modalVisible, setModalVisible] = useState(false); - function showEditForm() { + function showModal() { setModalVisible(true); } @@ -78,19 +58,29 @@ const AccountPage = ({} : AccountPageProps) => { setModalVisible(false); } + const changeTab = (tabIndex: number) => { + setActiveTab(tabIndex); + } + + const accNavItems = [ + {ref: useRef(null), active: activeTab===0, itemName: "Customer Information", onClick: changeTab}, + {ref: useRef(null), active: activeTab===1, itemName: "Your Orders", onClick: changeTab}, + {ref: useRef(null), active: activeTab===2, itemName: "Favorites", onClick: changeTab} + ] + return ( <>
Account
- +
- - + + {/* Thieu cai favorite */} {/* */} diff --git a/src/components/modules/account/AccountPage/components/AccountInfomation/AccountInfomation.tsx b/src/components/modules/account/AccountPage/components/AccountInfomation/AccountInfomation.tsx index 643ebeea7..6bc1c4a72 100644 --- a/src/components/modules/account/AccountPage/components/AccountInfomation/AccountInfomation.tsx +++ b/src/components/modules/account/AccountPage/components/AccountInfomation/AccountInfomation.tsx @@ -1,4 +1,4 @@ -import React, { useState } from "react" +import React from "react" import s from './AccountInfomation.module.scss' import Image from "next/image" @@ -7,13 +7,15 @@ import avatar from '../../assets/avatar.png'; interface AccountInfomationProps { account: {name: string, email: string, address: string, state: string, city: string, postalCode: string, phoneNumber: string}; active: boolean; - showEditForm: () => void; + onClick: () => void; } -const AccountInfomation = ({ account, active=false, showEditForm } : AccountInfomationProps) => { +const AccountInfomation = ({ account, active=false, onClick } : AccountInfomationProps) => { // need to handle call back when edit account information + const showEditForm = () => onClick() + return (
{