BOD-118 Added Saga to start conversations from anywhere. Removed gql apollo-boost due to package size and replaced with graphql-tag
This commit is contained in:
@@ -15,7 +15,11 @@ const messagingReducer = (state = INITIAL_STATE, action) => {
|
||||
visible: !state.visible,
|
||||
};
|
||||
case MessagingActionTypes.SET_SELECTED_CONVERSATION:
|
||||
return { ...state, selectedConversationId: action.payload };
|
||||
return {
|
||||
...state,
|
||||
visible: true,
|
||||
selectedConversationId: action.payload,
|
||||
};
|
||||
case MessagingActionTypes.SEND_MESSAGE:
|
||||
return {
|
||||
...state,
|
||||
|
||||
@@ -1,16 +1,69 @@
|
||||
import { all, call, put, takeLatest } from "redux-saga/effects";
|
||||
import { sendMessageFailure, sendMessageSuccess } from "./messaging.actions";
|
||||
import MessagingActionTypes from "./messaging.types";
|
||||
import axios from "axios";
|
||||
import { sendEmailFailure } from "../email/email.actions";
|
||||
import { withApollo } from "react-apollo";
|
||||
import phone from "phone";
|
||||
import { all, call, put, select, takeLatest } from "redux-saga/effects";
|
||||
import { client } from "../../App/App.container";
|
||||
import {
|
||||
CONVERSATION_ID_BY_PHONE,
|
||||
CREATE_CONVERSATION,
|
||||
} from "../../graphql/conversations.queries";
|
||||
import { INSERT_CONVERSATION_TAG } from "../../graphql/job-conversations.queries";
|
||||
import {
|
||||
sendMessageFailure,
|
||||
sendMessageSuccess,
|
||||
setSelectedConversation,
|
||||
} from "./messaging.actions";
|
||||
import MessagingActionTypes from "./messaging.types";
|
||||
import { selectBodyshop } from "../user/user.selectors";
|
||||
|
||||
export function* onOpenChatByPhone() {
|
||||
yield takeLatest(MessagingActionTypes.OPEN_CHAT_BY_PHONE, openChatByPhone);
|
||||
}
|
||||
export function* openChatByPhone({ payload: phone, client }) {
|
||||
console.log("Payload: Phone, Client", phone, client);
|
||||
export function* openChatByPhone({ payload }) {
|
||||
const { phone_num, jobid } = payload;
|
||||
const bodyshop = yield select(selectBodyshop);
|
||||
try {
|
||||
const {
|
||||
data: { conversations },
|
||||
} = yield client.query({
|
||||
query: CONVERSATION_ID_BY_PHONE,
|
||||
variables: { phone: phone(phone_num)[0] },
|
||||
});
|
||||
if (conversations.length === 0) {
|
||||
const {
|
||||
data: {
|
||||
insert_conversations: { returning: newConversationsId },
|
||||
},
|
||||
} = yield client.mutate({
|
||||
mutation: CREATE_CONVERSATION,
|
||||
variables: {
|
||||
conversation: [
|
||||
{
|
||||
phone_num: phone(phone_num)[0],
|
||||
bodyshopid: bodyshop.id,
|
||||
job_conversations: jobid ? { data: { jobid: jobid } } : null,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
yield put(setSelectedConversation(newConversationsId[0].id));
|
||||
} else if (conversations.length === 1) {
|
||||
//got the ID. Open it.
|
||||
yield put(setSelectedConversation(conversations[0].id));
|
||||
yield client.mutate({
|
||||
mutation: INSERT_CONVERSATION_TAG,
|
||||
variables: {
|
||||
conversationId: conversations[0].id,
|
||||
jobId: jobid,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
console.log("ERROR: Multiple conversations found. "); //TODO Graceful handling of this situation
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Error in sendMessage saga.", error);
|
||||
}
|
||||
}
|
||||
|
||||
export function* onSendMessage() {
|
||||
yield takeLatest(MessagingActionTypes.SEND_MESSAGE, sendMessage);
|
||||
}
|
||||
@@ -20,7 +73,7 @@ export function* sendMessage({ payload }) {
|
||||
if (response.status === 200) {
|
||||
yield put(sendMessageSuccess(payload));
|
||||
} else {
|
||||
yield put(sendEmailFailure(response.data));
|
||||
yield put(sendMessageFailure(response.data));
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Error in sendMessage saga.", error);
|
||||
|
||||
Reference in New Issue
Block a user