Skip to content

Commit 7cad209

Browse files
committed
fix(actions): restore lbdashboard actions
1 parent c030059 commit 7cad209

3 files changed

Lines changed: 281 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {
2+
LISTING_FETCH_REQUEST,
3+
LISTING_FETCH_SUCCESS,
4+
LISTING_FETCH_FAIL,
5+
LISTING_AVAILABILITY_REQUEST,
6+
LISTING_AVAILABILITY_SUCCESS,
7+
LISTING_AVAILABILITY_FAIL,
8+
LISTING_BOOK_REQUEST,
9+
LISTING_BOOK_SUCCESS,
10+
LISTING_BOOK_FAIL,
11+
LISTING_BOOK_RESET,
12+
} from '../../constants/lbDashboard/listOverviewConstants';
13+
import { ENDPOINTS } from '../../utils/URL';
14+
15+
export const fetchListingById = (listingId) => async (dispatch) => {
16+
try {
17+
const token = localStorage.getItem('token');
18+
dispatch({ type: LISTING_FETCH_REQUEST });
19+
const response = await fetch(ENDPOINTS.LB_LISTING_GET_BY_ID, {
20+
method: 'POST',
21+
headers: {
22+
'Content-Type': 'application/json',
23+
...(token && { Authorization: token }),
24+
},
25+
body: JSON.stringify({ id: listingId }),
26+
});
27+
const data = await response.json();
28+
if (!response.ok || !data || data.error) {
29+
throw new Error(data.error || 'Failed to fetch listing');
30+
}
31+
dispatch({ type: LISTING_FETCH_SUCCESS, payload: data.data });
32+
} catch (error) {
33+
dispatch({ type: LISTING_FETCH_FAIL, payload: error.message });
34+
}
35+
};
36+
37+
export const fetchListingAvailability = (listingId) => async (dispatch) => {
38+
try {
39+
const token = localStorage.getItem('token');
40+
dispatch({ type: LISTING_AVAILABILITY_REQUEST });
41+
const response = await fetch(ENDPOINTS.LB_LISTING_AVAILABILITY, {
42+
method: 'GET',
43+
headers: {
44+
'Content-Type': 'application/json',
45+
...(token && { Authorization: token }),
46+
listingid: listingId,
47+
},
48+
});
49+
const data = await response.json();
50+
if (!response.ok || !data || data.error) {
51+
throw new Error(data.error || 'Failed to fetch availability');
52+
}
53+
dispatch({ type: LISTING_AVAILABILITY_SUCCESS, payload: data.data });
54+
} catch (error) {
55+
dispatch({ type: LISTING_AVAILABILITY_FAIL, payload: error.message });
56+
}
57+
};
58+
59+
export const bookListing = ({ listingId, from, to, userId }) => async (dispatch) => {
60+
try {
61+
const token = localStorage.getItem('token');
62+
dispatch({ type: LISTING_BOOK_REQUEST });
63+
const response = await fetch(ENDPOINTS.LB_LISTING_BOOK, {
64+
method: 'PUT',
65+
headers: {
66+
'Content-Type': 'application/json',
67+
...(token && { Authorization: token }),
68+
},
69+
body: JSON.stringify({ listingId, from, to, userId }),
70+
});
71+
const data = await response.json();
72+
if (!response.ok || !data || data.error) {
73+
throw new Error(data.error || 'Failed to book listing');
74+
}
75+
dispatch({ type: LISTING_BOOK_SUCCESS, payload: data.data });
76+
dispatch(fetchListingAvailability(listingId));
77+
} catch (error) {
78+
dispatch({ type: LISTING_BOOK_FAIL, payload: error.message });
79+
}
80+
};
81+
82+
export const resetBooking = () => ({ type: LISTING_BOOK_RESET });
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import axios from "axios";
2+
import { ENDPOINTS } from "../../utils/URL";
3+
import {
4+
FETCH_MESSAGES_REQUEST,
5+
FETCH_MESSAGES_SUCCESS,
6+
FETCH_MESSAGES_FAILURE,
7+
SEND_MESSAGE_REQUEST,
8+
SEND_MESSAGE_SUCCESS,
9+
SEND_MESSAGE_FAILURE,
10+
FETCH_EXISTING_CHATS_REQUEST,
11+
FETCH_EXISTING_CHATS_SUCCESS,
12+
FETCH_EXISTING_CHATS_FAILURE,
13+
MESSAGE_STATUS_UPDATED,
14+
MARK_MESSAGES_AS_READ_REQUEST,
15+
MARK_MESSAGES_AS_READ_SUCCESS,
16+
MARK_MESSAGES_AS_READ_FAILURE,
17+
} from "../../constants/lbdashboard/messagingConstants";
18+
19+
export const fetchMessages = (userId, selectedUserId) => async (dispatch) => {
20+
try {
21+
dispatch({ type: FETCH_MESSAGES_REQUEST });
22+
23+
const { data } = await axios.get(ENDPOINTS.LB_READ_MESSAGE, {
24+
headers: {
25+
'user-id': userId,
26+
'contact-id': selectedUserId,
27+
},
28+
});
29+
30+
dispatch({
31+
type: FETCH_MESSAGES_SUCCESS,
32+
payload: data,
33+
});
34+
return data;
35+
} catch (error) {
36+
dispatch({
37+
type: FETCH_MESSAGES_FAILURE,
38+
payload: error.response?.data?.message || error.message,
39+
});
40+
return null;
41+
}
42+
};
43+
44+
export const sendMessage = (messageData, socket) => (dispatch) => {
45+
if (socket && socket.readyState === WebSocket.OPEN) {
46+
dispatch({ type: SEND_MESSAGE_REQUEST });
47+
48+
socket.send(
49+
JSON.stringify({
50+
action: "SEND_MESSAGE",
51+
...messageData,
52+
})
53+
);
54+
55+
dispatch({ type: SEND_MESSAGE_SUCCESS });
56+
} else {
57+
dispatch({
58+
type: SEND_MESSAGE_FAILURE,
59+
payload: "WebSocket is not connected.",
60+
});
61+
}
62+
};
63+
64+
export const fetchMessageStatuses = (userId, selectedUserId, messageIds) => async (dispatch) => {
65+
if (!messageIds || messageIds.length === 0) return;
66+
try {
67+
const { data } = await axios.get(ENDPOINTS.LB_UPDATE_MESSAGE_STATUS, {
68+
headers: {
69+
'user-id': userId,
70+
'contact-id': selectedUserId,
71+
},
72+
});
73+
74+
data.forEach((statusUpdate) => {
75+
dispatch({
76+
type: MESSAGE_STATUS_UPDATED,
77+
payload: statusUpdate,
78+
});
79+
});
80+
} catch (error) {
81+
Error("Error fetching message statuses:", error);
82+
}
83+
84+
};
85+
86+
export const fetchExistingChats = (userId) => async (dispatch) => {
87+
try {
88+
dispatch({ type: FETCH_EXISTING_CHATS_REQUEST });
89+
90+
const { data } = await axios.get(ENDPOINTS.LB_EXISTING_CHATS, {
91+
headers: { "user-id": userId },
92+
});
93+
94+
dispatch({
95+
type: FETCH_EXISTING_CHATS_SUCCESS,
96+
payload: data,
97+
});
98+
} catch (error) {
99+
dispatch({
100+
type: FETCH_EXISTING_CHATS_FAILURE,
101+
payload: error.response?.data?.message || error.message,
102+
});
103+
}
104+
};
105+
106+
export const markMessagesAsRead = (userId, contactId) => async (dispatch) => {
107+
try {
108+
dispatch({ type: MARK_MESSAGES_AS_READ_REQUEST });
109+
110+
const { data } = await axios.post(ENDPOINTS.LB_MARK_MESSAGES_AS_READ, {
111+
userId,
112+
contactId,
113+
});
114+
115+
dispatch({
116+
type: MARK_MESSAGES_AS_READ_SUCCESS,
117+
payload: data.message,
118+
});
119+
} catch (error) {
120+
dispatch({
121+
type: MARK_MESSAGES_AS_READ_FAILURE,
122+
payload: error.response?.data?.message || "Failed to mark messages as read.",
123+
});
124+
}
125+
};
126+
127+
export const clearNotifications = () => (dispatch) => {
128+
dispatch({
129+
type: "CLEAR_NOTIFICATIONS",
130+
});
131+
};
132+
133+
export const clearDBNotifications = () => (dispatch) => {
134+
dispatch({
135+
type: "CLEAR_DB_NOTIFICATIONS",
136+
});
137+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import axios from "axios";
2+
import { ENDPOINTS } from "../../utils/URL";
3+
import {
4+
FETCH_USER_PREFERENCES_REQUEST,
5+
FETCH_USER_PREFERENCES_SUCCESS,
6+
FETCH_USER_PREFERENCES_FAILURE,
7+
UPDATE_USER_PREFERENCES_REQUEST,
8+
UPDATE_USER_PREFERENCES_SUCCESS,
9+
UPDATE_USER_PREFERENCES_FAILURE,
10+
} from "../../constants/lbdashboard/userPreferenceConstants";
11+
12+
// Fetch user preferences
13+
export const fetchUserPreferences = (userId, selectedUserId = null) => async (dispatch) => {
14+
try {
15+
dispatch({ type: FETCH_USER_PREFERENCES_REQUEST });
16+
17+
const { data } = await axios.post(ENDPOINTS.LB_GET_USER_PREFERENCES, {
18+
userId,
19+
selectedUserId,
20+
});
21+
22+
dispatch({
23+
type: FETCH_USER_PREFERENCES_SUCCESS,
24+
payload: data,
25+
});
26+
27+
return data;
28+
} catch (error) {
29+
Error("Error fetching user preferences:", error);
30+
dispatch({
31+
type: FETCH_USER_PREFERENCES_FAILURE,
32+
payload: error.response?.data?.message || error.message,
33+
});
34+
return null;
35+
}
36+
};
37+
38+
// Update user preferences
39+
export const updateUserPreferences = (userId, selectedUserId, preferences) => async (dispatch) => {
40+
try {
41+
dispatch({ type: UPDATE_USER_PREFERENCES_REQUEST });
42+
43+
const { data } = await axios.put(ENDPOINTS.LB_UPDATE_USER_PREFERENCES, {
44+
userId,
45+
selectedUserId,
46+
...preferences,
47+
});
48+
49+
dispatch({
50+
type: UPDATE_USER_PREFERENCES_SUCCESS,
51+
payload: data,
52+
});
53+
54+
return data;
55+
} catch (error) {
56+
dispatch({
57+
type: UPDATE_USER_PREFERENCES_FAILURE,
58+
payload: error.response?.data?.message || error.message,
59+
});
60+
throw error;
61+
}
62+
};

0 commit comments

Comments
 (0)