刷新页面, 定位指定的会话

dev/chat
Lei OT 2 years ago
parent 53f6a02032
commit d19c70968c

@ -16,6 +16,7 @@ const initialConversationState = {
// websocketRetrytimes: null,
errors: [], // 错误信息
initialState: false,
// templates: [],
@ -252,16 +253,19 @@ export const useConversationStore = create(devtools((set, get) => ({
// state actions
addError: (error) => set((state) => ({ errors: [...state.errors, error] })),
setInitial: (v) => set({ initialState: v }),
// side effects
fetchInitialData: async (userId) => {
const { addToConversationList, setTemplates } = get();
const { addToConversationList, setTemplates, setInitial } = get();
const conversationsList = await fetchConversationsList({ opisn: userId });
addToConversationList(conversationsList);
const templates = await fetchTemplates();
setTemplates(templates);
setInitial(true);
},
})));

@ -46,7 +46,7 @@ const Conversations = () => {
const navigate = useNavigate();
const { loginUser } = useAuthContext();
const { userId } = loginUser;
const { activeConversations, currentConversation, conversationsList, addToConversationList, setCurrentConversation, receivedMessageList, setMsgLoading } = useConversationStore();
const { initialState, activeConversations, currentConversation, conversationsList, addToConversationList, setCurrentConversation, receivedMessageList, setMsgLoading } = useConversationStore();
const [chatlist, setChatlist] = useState([]);
useEffect(() => {
setChatlist(
@ -76,12 +76,12 @@ const Conversations = () => {
const [shouldFetchCList, setShouldFetchCList] = useState(true);
useEffect(() => {
if (order_sn && shouldFetchCList) {
if (order_sn && shouldFetchCList && initialState) {
getOrderConversationList(order_sn);
}
return () => {};
}, [order_sn, shouldFetchCList]);
}, [order_sn, shouldFetchCList, initialState]);
const getOrderConversationList = async (colisn) => {
const { whatsapp_phone_number } = switchToC;
@ -89,8 +89,11 @@ const Conversations = () => {
const data = await fetchOrderConversationsList({ opisn: userId, colisn: colisn, whatsappid: whatsappID });
if (!isEmpty(data)) {
addToConversationList(data);
const ifCurrent = data.findIndex((item) => item.sn === currentConversation.sn);
switchConversation(data[ifCurrent === -1 ? 0 : ifCurrent]);
}
// const ifCurrent = data.findIndex((item) => item.sn === currentConversation.sn);
const ifCurrent = conversationsList.findIndex((item) => item.coli_sn === Number(colisn));
if (ifCurrent !== -1) {
switchConversation(conversationsList[ifCurrent === -1 ? 0 : ifCurrent]);
} else {
// reset chat window
setCurrentConversation({ sn: '', customer_name: '', coli_sn: order_sn });

Loading…
Cancel
Save