- the great reformat

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-02-06 18:20:58 -05:00
parent 30c530bcc4
commit e83badb454
912 changed files with 108516 additions and 107493 deletions

View File

@@ -1,35 +1,35 @@
import MessagingActionTypes from "./messaging.types";
export const toggleChatVisible = () => ({
type: MessagingActionTypes.TOGGLE_CHAT_VISIBLE,
//payload: user
type: MessagingActionTypes.TOGGLE_CHAT_VISIBLE,
//payload: user
});
export const sendMessage = (message) => ({
type: MessagingActionTypes.SEND_MESSAGE,
payload: message,
type: MessagingActionTypes.SEND_MESSAGE,
payload: message,
});
export const sendMessageSuccess = (message) => ({
type: MessagingActionTypes.SEND_MESSAGE_SUCCESS,
payload: message,
type: MessagingActionTypes.SEND_MESSAGE_SUCCESS,
payload: message,
});
export const sendMessageFailure = (error) => ({
type: MessagingActionTypes.SEND_MESSAGE_FAILURE,
payload: error,
type: MessagingActionTypes.SEND_MESSAGE_FAILURE,
payload: error,
});
export const setSelectedConversation = (conversationId) => ({
type: MessagingActionTypes.SET_SELECTED_CONVERSATION,
payload: conversationId,
type: MessagingActionTypes.SET_SELECTED_CONVERSATION,
payload: conversationId,
});
export const openChatByPhone = (phoneNumber) => ({
type: MessagingActionTypes.OPEN_CHAT_BY_PHONE,
payload: phoneNumber,
type: MessagingActionTypes.OPEN_CHAT_BY_PHONE,
payload: phoneNumber,
});
export const setMessage = (message) => ({
type: MessagingActionTypes.SET_MESSAGE,
payload: message,
type: MessagingActionTypes.SET_MESSAGE,
payload: message,
});

View File

@@ -1,55 +1,55 @@
import MessagingActionTypes from "./messaging.types";
const INITIAL_STATE = {
open: false,
selectedConversationId: null,
isSending: false,
error: null,
message: null,
searchingForConversation: false,
open: false,
selectedConversationId: null,
isSending: false,
error: null,
message: null,
searchingForConversation: false,
};
const messagingReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case MessagingActionTypes.SET_MESSAGE:
return { ...state, message: action.payload };
case MessagingActionTypes.TOGGLE_CHAT_VISIBLE:
return {
...state,
open: !state.open,
};
case MessagingActionTypes.OPEN_CHAT_BY_PHONE:
return {
...state,
searchingForConversation: true,
};
case MessagingActionTypes.SET_SELECTED_CONVERSATION:
return {
...state,
open: true,
searchingForConversation: false,
selectedConversationId: action.payload,
};
case MessagingActionTypes.SEND_MESSAGE:
return {
...state,
error: null,
isSending: true,
};
case MessagingActionTypes.SEND_MESSAGE_SUCCESS:
return {
...state,
message: "",
isSending: false,
};
case MessagingActionTypes.SEND_MESSAGE_FAILURE:
return {
...state,
error: action.payload,
};
default:
return state;
}
switch (action.type) {
case MessagingActionTypes.SET_MESSAGE:
return {...state, message: action.payload};
case MessagingActionTypes.TOGGLE_CHAT_VISIBLE:
return {
...state,
open: !state.open,
};
case MessagingActionTypes.OPEN_CHAT_BY_PHONE:
return {
...state,
searchingForConversation: true,
};
case MessagingActionTypes.SET_SELECTED_CONVERSATION:
return {
...state,
open: true,
searchingForConversation: false,
selectedConversationId: action.payload,
};
case MessagingActionTypes.SEND_MESSAGE:
return {
...state,
error: null,
isSending: true,
};
case MessagingActionTypes.SEND_MESSAGE_SUCCESS:
return {
...state,
message: "",
isSending: false,
};
case MessagingActionTypes.SEND_MESSAGE_FAILURE:
return {
...state,
error: action.payload,
};
default:
return state;
}
};
export default messagingReducer;

View File

@@ -1,112 +1,109 @@
import axios from "axios";
import parsePhoneNumber from "libphonenumber-js";
import { all, call, put, select, takeLatest } from "redux-saga/effects";
import { logImEXEvent } from "../../firebase/firebase.utils";
import {
CONVERSATION_ID_BY_PHONE,
CREATE_CONVERSATION,
} from "../../graphql/conversations.queries";
import { INSERT_CONVERSATION_TAG } from "../../graphql/job-conversations.queries";
import {all, call, put, select, takeLatest} from "redux-saga/effects";
import {logImEXEvent} from "../../firebase/firebase.utils";
import {CONVERSATION_ID_BY_PHONE, CREATE_CONVERSATION,} from "../../graphql/conversations.queries";
import {INSERT_CONVERSATION_TAG} from "../../graphql/job-conversations.queries";
import client from "../../utils/GraphQLClient";
import { selectBodyshop } from "../user/user.selectors";
import {
sendMessageFailure,
sendMessageSuccess,
setSelectedConversation,
} from "./messaging.actions";
import {selectBodyshop} from "../user/user.selectors";
import {sendMessageFailure, sendMessageSuccess, setSelectedConversation,} from "./messaging.actions";
import MessagingActionTypes from "./messaging.types";
export function* onToggleChatVisible() {
yield takeLatest(MessagingActionTypes.TOGGLE_CHAT_VISIBLE, toggleChatLogging);
yield takeLatest(MessagingActionTypes.TOGGLE_CHAT_VISIBLE, toggleChatLogging);
}
export function* toggleChatLogging() {
try {
yield logImEXEvent("messaging_toggle_popup");
} catch (error) {
console.log("Error in sendMessage saga.", error);
}
}
export function* onOpenChatByPhone() {
yield takeLatest(MessagingActionTypes.OPEN_CHAT_BY_PHONE, openChatByPhone);
}
export function* openChatByPhone({ payload }) {
logImEXEvent("messaging_open_by_phone");
const { phone_num, jobid } = payload;
const p = parsePhoneNumber(phone_num, "CA");
const bodyshop = yield select(selectBodyshop);
try {
const {
data: { conversations },
} = yield client.query({
query: CONVERSATION_ID_BY_PHONE,
variables: { phone: p.number },
});
if (conversations.length === 0) {
const {
data: {
insert_conversations: { returning: newConversationsId },
},
} = yield client.mutate({
mutation: CREATE_CONVERSATION,
variables: {
conversation: [
{
phone_num: p.number,
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));
//Check to see if this job ID is already a child of it. If not add the tag.
if (
jobid &&
!conversations[0].job_conversations.find((jc) => jc.jobid === jobid)
)
yield client.mutate({
mutation: INSERT_CONVERSATION_TAG,
variables: {
conversationId: conversations[0].id,
jobId: jobid,
},
});
} else {
console.log("ERROR: Multiple conversations found. ");
yield put(setSelectedConversation(null));
try {
yield logImEXEvent("messaging_toggle_popup");
} catch (error) {
console.log("Error in sendMessage saga.", error);
}
}
export function* onOpenChatByPhone() {
yield takeLatest(MessagingActionTypes.OPEN_CHAT_BY_PHONE, openChatByPhone);
}
export function* openChatByPhone({payload}) {
logImEXEvent("messaging_open_by_phone");
const {phone_num, jobid} = payload;
const p = parsePhoneNumber(phone_num, "CA");
const bodyshop = yield select(selectBodyshop);
try {
const {
data: {conversations},
} = yield client.query({
query: CONVERSATION_ID_BY_PHONE,
variables: {phone: p.number},
});
if (conversations.length === 0) {
const {
data: {
insert_conversations: {returning: newConversationsId},
},
} = yield client.mutate({
mutation: CREATE_CONVERSATION,
variables: {
conversation: [
{
phone_num: p.number,
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));
//Check to see if this job ID is already a child of it. If not add the tag.
if (
jobid &&
!conversations[0].job_conversations.find((jc) => jc.jobid === jobid)
)
yield client.mutate({
mutation: INSERT_CONVERSATION_TAG,
variables: {
conversationId: conversations[0].id,
jobId: jobid,
},
});
} else {
console.log("ERROR: Multiple conversations found. ");
yield put(setSelectedConversation(null));
}
} catch (error) {
console.log("Error in sendMessage saga.", error);
}
} catch (error) {
console.log("Error in sendMessage saga.", error);
}
}
export function* onSendMessage() {
yield takeLatest(MessagingActionTypes.SEND_MESSAGE, sendMessage);
yield takeLatest(MessagingActionTypes.SEND_MESSAGE, sendMessage);
}
export function* sendMessage({ payload }) {
try {
const response = yield call(axios.post, "/sms/send", payload);
if (response.status === 200) {
yield put(sendMessageSuccess(payload));
} else {
yield put(sendMessageFailure(response.data));
export function* sendMessage({payload}) {
try {
const response = yield call(axios.post, "/sms/send", payload);
if (response.status === 200) {
yield put(sendMessageSuccess(payload));
} else {
yield put(sendMessageFailure(response.data));
}
} catch (error) {
console.log("Error in sendMessage saga.", error);
yield put(sendMessageFailure(error));
}
} catch (error) {
console.log("Error in sendMessage saga.", error);
yield put(sendMessageFailure(error));
}
}
export function* messagingSagas() {
yield all([
call(onSendMessage),
call(onOpenChatByPhone),
call(onToggleChatVisible),
]);
yield all([
call(onSendMessage),
call(onOpenChatByPhone),
call(onToggleChatVisible),
]);
}

View File

@@ -1,33 +1,33 @@
import { createSelector } from "reselect";
import {createSelector} from "reselect";
const selectMessaging = (state) => state.messaging;
export const selectChatVisible = createSelector(
[selectMessaging],
(messaging) => messaging.open
[selectMessaging],
(messaging) => messaging.open
);
export const selectIsSending = createSelector(
[selectMessaging],
(messaging) => messaging.isSending
[selectMessaging],
(messaging) => messaging.isSending
);
export const selectError = createSelector(
[selectMessaging],
(messaging) => messaging.error
[selectMessaging],
(messaging) => messaging.error
);
export const selectSelectedConversation = createSelector(
[selectMessaging],
(messaging) => messaging.selectedConversationId
[selectMessaging],
(messaging) => messaging.selectedConversationId
);
export const selectMessage = createSelector(
[selectMessaging],
(messaging) => messaging.message
[selectMessaging],
(messaging) => messaging.message
);
export const searchingForConversation = createSelector(
[selectMessaging],
(messaging) => messaging.searchingForConversation
[selectMessaging],
(messaging) => messaging.searchingForConversation
);

View File

@@ -1,10 +1,10 @@
const MessagingActionTypes = {
TOGGLE_CHAT_VISIBLE: "TOGGLE_CHAT_VISIBLE",
SEND_MESSAGE: "SEND_MESSAGE",
SEND_MESSAGE_SUCCESS: "SEND_MESSAGE_SUCCESS",
SEND_MESSAGE_FAILURE: "SEND_MESSAGE_FAILURE",
SET_SELECTED_CONVERSATION: "SET_SELECTED_CONVERSATION",
OPEN_CHAT_BY_PHONE: "OPEN_CHAT_BY_PHONE",
SET_MESSAGE: "SET_MESSAGE",
TOGGLE_CHAT_VISIBLE: "TOGGLE_CHAT_VISIBLE",
SEND_MESSAGE: "SEND_MESSAGE",
SEND_MESSAGE_SUCCESS: "SEND_MESSAGE_SUCCESS",
SEND_MESSAGE_FAILURE: "SEND_MESSAGE_FAILURE",
SET_SELECTED_CONVERSATION: "SET_SELECTED_CONVERSATION",
OPEN_CHAT_BY_PHONE: "OPEN_CHAT_BY_PHONE",
SET_MESSAGE: "SET_MESSAGE",
};
export default MessagingActionTypes;