import { groupBy } from '@/utils/utils'; const API_HOST = 'https://p9axztuwd7x8a7.mycht.cn/whatsapp_callback'; const NAME_SPACE = 'CONVERSATION/'; export const initWebsocket = (socket) => ({ type: NAME_SPACE + 'INIT_WEBSOCKET', payload: socket, }); export const closeWebsocket0 = () => ({ type: NAME_SPACE + 'CLOSE_WEBSOCKET', payload: null, }); export const closeWebsocket = () => {}; export const receivedNewMessage = (targetId, message) => ({ type: NAME_SPACE + 'RECEIVED_NEW_MESSAGE', payload: { targetId, message, }, }); export const sentNewMessage = (message) => ({ type: NAME_SPACE + 'SENT_NEW_MESSAGE', payload: { targetId: message.conversationid, message, }, }); export const addError = (error) => { return { type: NAME_SPACE + 'ADD_ERROR', payload: error, }; }; export const receivedTemplates = (data) => ({ type: NAME_SPACE + 'SET_TEMPLATE_LIST', payload: data, }); export const receivedConversationList = (data) => { return { type: NAME_SPACE + 'SET_CONVERSATION_LIST', payload: data, }; }; export const addConversationList = (data) => { return { type: NAME_SPACE + 'ADD_CONVERSATIONS', payload: data, }; }; export const updateConversationListItemNew = (message) => ({ type: NAME_SPACE + 'UPDATE_CONVERSATION_LIST_ITEM_NEW', payload: message, }); export const receivedCustomerProfile = (data) => ({ type: NAME_SPACE + 'SET_CUSTOMER_ORDER_PROFILE', payload: data, }); export const setActiveConversations = (obj) => ({ type: NAME_SPACE + 'SET_ACTIVE_CONVERSATIONS', payload: obj, }); export const setCurrentConversation = (obj) => ({ type: NAME_SPACE + 'SET_CURRENT_CONVERSATION', payload: obj, }); export const updateMessageItem = (message) => ({ type: NAME_SPACE + 'UPDATE_SENT_MESSAGE_ITEM', payload: message, }); export const fetchTemplates = async () => { const data = await fetchJSON(`${API_HOST}/listtemplates`); const canUseTemplates = (data?.result?.items || []) .filter((_t) => _t.status !== 'REJECTED') .map((ele) => ({ ...ele, components: groupBy(ele.components, (_c) => _c.type.toLowerCase()) })); return canUseTemplates; }; export const fetchConversationsList = async (opisn) => { const { result: data } = await fetchJSON(`${API_HOST}/getconversations`, { opisn }); const _data = []; // const _data = testConversations; const list = [..._data, ...data.map((ele) => ({ ...ele, customer_name: ele.whatsapp_name.trim() }))]; return list; }; export const fetchCustomerProfile = async (colisn) => { const { result } = await fetchJSON(`${API_HOST}/getorderinfo`, { colisn }); const data = result?.[0] || {}; return data; }; export async function fetchJSON(url, data) { let params = ''; let ifp = ''; if (data) { params = new URLSearchParams(data).toString(); ifp = params ? '?' : ifp; } ifp = url.includes('?') ? '' : ifp; const host = /^https?:\/\//i.test(url) ? '' : API_HOST; const response = await fetch(`${host}${url}${ifp}${params}`); return await response.json(); } export async function postJSON(url, obj) { try { const response = await fetch(url, { method: 'POST', body: JSON.stringify(obj), headers: { 'Content-Type': 'application/json', }, }); if (!response.ok) { throw new Error('Network response was not ok'); } return await response.json(); } catch (error) { console.error('fetch error:', error); throw error; } }