Skip to content

Commit

Permalink
feat: add user-history page
Browse files Browse the repository at this point in the history
  • Loading branch information
huanghanzhilian committed Dec 28, 2023
1 parent 797925a commit b6535f3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
55 changes: 55 additions & 0 deletions app/(main)/(profile-layout)/profile/user-history/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use client'

import Link from 'next/link'

import { useAppSelector } from 'hooks'

import { truncate } from 'utils'

import { EmptyCart, PageContainer, ResponsiveImage } from 'components'

const UserHistory = () => {
//? Store
const { lastSeen } = useAppSelector(state => state.user)

//? selector
return (
<main>
<PageContainer title="最近访问">
{lastSeen.length > 0 ? (
<div className="px-3 space-y-4 md:py-4 md:space-y-0 md:grid md:grid-cols-2 md:gap-x-2 lg:grid-cols-3 md:gap-y-3">
{lastSeen.map(item => (
<article
className="border-b md:hover:shadow-3xl md:h-64 md:border-0 "
key={item.productID}
>
<Link
href={`/products/${item.productID}`}
className="flex items-center gap-4 py-4 md:items-start md:flex-col"
>
<ResponsiveImage
dimensions="w-36 h-36"
className="md:mx-auto"
src={item.image.url}
alt={item.title}
/>

<h5 className="flex-1 px-3 text-right text-gray-800 leadiri-6 md:h-32">
{truncate(item.title, 80)}
</h5>
</Link>
</article>
))}
</div>
) : (
<section className="py-20">
<EmptyCart className="mx-auto h-52 w-52" />
<p className="text-center">您的最近访问列表为空</p>
</section>
)}
</PageContainer>
</main>
)
}

export default UserHistory
6 changes: 3 additions & 3 deletions components/common/BoxLink.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Link from 'next/link'
import { useRouter } from 'next/navigation'
import { usePathname } from 'next/navigation'

import { Icons } from 'components'

Expand All @@ -8,13 +8,13 @@ export default function BoxLink(props) {
const { children, path, name } = props

//? Assets
const router = useRouter()
const asPath = usePathname()

//? Render(s)
return (
<div
className={`transition-colors hover:bg-gray-200 px-3 ${
router.pathname === path ? 'border-r-4 border-red-600' : 'border-r-4 border-white'
asPath === path ? 'border-r-4 border-red-600' : 'border-r-4 border-white'
}`}
>
<Link
Expand Down
12 changes: 12 additions & 0 deletions components/product/InitialStore.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import { useAppDispatch } from '@/hooks'

import { setTempColor, setTempSize, addToLastSeen } from '@/store'
import { useEffect } from 'react'

const InitialStore = props => {
const { product } = props

const dispatch = useAppDispatch()

if (product.colors.length > 0) {
dispatch(setTempColor(product?.colors[0]))
dispatch(setTempSize(null))
Expand All @@ -17,6 +20,15 @@ const InitialStore = props => {
dispatch(setTempColor(null))
dispatch(setTempSize(null))
}
useEffect(() => {
dispatch(
addToLastSeen({
productID: product._id,
image: product.images[0],
title: product.title,
})
)
}, [product._id])
return null
}

Expand Down
24 changes: 22 additions & 2 deletions store/slices/user.slice.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { createSlice } from '@reduxjs/toolkit'
import Cookies from 'js-cookie'

const getLastSeen = () => {
if (typeof window !== 'undefined') {
const lastSeenJSON = localStorage.getItem('lastSeen')
if (lastSeenJSON) return JSON.parse(lastSeenJSON)
}

return []
}

const token = Cookies.get('token') || ''

const initialState = { token }
const initialState = { token, lastSeen: getLastSeen() }

const userSlice = createSlice({
name: 'user',
Expand All @@ -18,9 +27,20 @@ const userSlice = createSlice({
Cookies.set('token', action.payload, { expires: 10 })
state.token = action.payload
},
addToLastSeen: (state, action) => {
let isItemExist = state.lastSeen.find(item => item.productID === action.payload.productID)

if (!isItemExist) {
if (state.lastSeen.length === 15) {
state.lastSeen.splice(14, 1)
}
state.lastSeen.unshift(action.payload)
localStorage.setItem('lastSeen', JSON.stringify(state.lastSeen))
}
},
},
})

export const { userLogout, userLogin } = userSlice.actions
export const { userLogout, userLogin, addToLastSeen } = userSlice.actions

export default userSlice.reducer

0 comments on commit b6535f3

Please sign in to comment.