tiefen_space_h5/app/noticeDetail/page.js

168 lines
4.8 KiB
JavaScript

"use client";
import React, { useEffect, useRef, useState } from "react";
import { Tabs, Swiper, Toast } from "antd-mobile";
import Link from "next/link";
import requireAPI from "@/utils/requireAPI";
import MessageList from "./components/MessageList";
import { get } from "@/utils/storeInfo";
import OwnIcon from "@/components/OwnIcon";
const tabItems = [{ key: "space", title: "消息" }];
// const scrollHeight = 700;
// const scrollHeight = window.innerHeight-126
export default function Space() {
const [activeIndex, setActiveIndex] = useState(0);
const [dataList, setDataList] = useState([]);
// const [spacePost, setSpacePost] = useState([]);
// const [hasMore, setHasMore] = useState(1);
// const [scrollHeight, setScrollHeight] = useState(0);
const [offset, setOffset] = useState(0);
const mesListEl = useRef(null);
const [account, setAccount] = useState(null);
// 获取屏幕高度
// const scrollHeight = 600;
useEffect(() => {
setAccount(get("account"));
// setScrollHeight(window.innerHeight - 57 - 44);
// const handleResize = () => {
// setScrollHeight(window.innerHeight - 126);
// };
// window.addEventListener("resize", handleResize);
// return () => {
// // window.removeEventListener("resize", handleResize);
// };
}, []);
useEffect(() => {
firstRequest();
}, [activeIndex]);
const firstRequest = () => {
resetOffset();
if (activeIndex === 0) {
getSpaceList();
}
if (activeIndex === 1) {
getSpacePosts(0).then((res) => {
res && setDataList(res);
});
}
};
const resetOffset = () => {
setOffset(0);
// setDataList([]);
};
const getSpaceList = async () => {
// setLoading(true);
try {
const data = await requireAPI("POST", "/api/zone/list_by_visitor_mid");
// setLoading(false);
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
//在末尾添加元素以展示查看更多卡片
if (data.data.list.length !== 0) {
const finalData = [...data.data.list];
setDataList(finalData);
return;
}
} catch (error) {
// console.error(error);
}
};
const getSpacePosts = async (offset) => {
// setLoading(true);
try {
const data = await requireAPI(
"POST",
"/api/zone_moment/list_by_visitor_mid",
{
body: { offset, limit: 4 },
}
);
// setLoading(false);
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
//在末尾添加元素以展示查看更多卡片
// setHasMore(data.data.more);
setOffset(data.data.offset);
if (data.data.list.length !== 0) {
const finalData = [...data.data.list];
return finalData;
}
} catch (error) {
// console.error(error);
}
};
async function loadMore() {
if (!offset) return;
const append = await getSpacePosts(offset);
if (append) {
setDataList((val) => [...val, ...append]);
// setHasMore(append.length > 0);
}
}
const handleReadAll = () => {
if (mesListEl.current) mesListEl.current.readAllMsg([0, 1, 2, 3]);
};
return (
<div className="">
<div className="flex justify-between items-center px-2 custom-tabs text-gray-400 sticky top-0 z-10 bg-deepBg">
<Tabs
activeKey={tabItems[activeIndex].key}
onChange={(key) => {
const index = tabItems.findIndex((item) => item.key === key);
setActiveIndex(index);
// swiperRef.current?.swipeTo(index);
}}
>
{tabItems.map((item) => (
<Tabs.Tab
forceRender={false}
title={item.title}
key={item.key}
className="text-left"
/>
))}
</Tabs>
<div className="flex space-x-4">
<Link
href="search"
className="w-9 h-9 flex items-center justify-center bg-[#FFFFFF1A] rounded-full"
>
<OwnIcon src="/icons/search.png" className="scale-90" />
</Link>
<div
className="w-9 h-9 flex items-center justify-center rounded-full"
onClick={handleReadAll}
>
<OwnIcon src="/icons/32DP/remove.png" className="scale-90" />
</div>
</div>
</div>
<Swiper
direction="horizontal"
indicator={() => null}
// ref={swiperRef}
defaultIndex={activeIndex}
onIndexChange={(index) => {
setActiveIndex(index);
}}
>
<Swiper.Item>
<MessageList ref={mesListEl} mid={account?.mid} />
</Swiper.Item>
</Swiper>
</div>
);
}