Skip to content

Commit

Permalink
Merge branch 'develop' into fix/admin-sidebar-navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
rique223 authored Sep 28, 2022
2 parents 8fb31fb + 3a2b6da commit bcc2e57
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
8 changes: 7 additions & 1 deletion packages/livechat/src/components/App/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,13 @@ export class App extends Component {
}
}

render = ({ sound, undocked, minimized, expanded, alerts, modal }, { initialized, poppedOut }) => {
render = ({ sound, undocked, minimized, expanded, alerts, modal, iframe }, { initialized, poppedOut }) => {
if (!initialized) {
return null;
}

const { department, name, email } = iframe.guest || {};

const screenProps = {
notificationsEnabled: sound && sound.enabled,
minimized: !poppedOut && (minimized || undocked),
Expand All @@ -217,6 +220,9 @@ export class App extends Component {
sound,
alerts,
modal,
nameDefault: name,
emailDefault: email,
departmentDefault: department,
onEnableNotifications: this.handleEnableNotifications,
onDisableNotifications: this.handleDisableNotifications,
onMinimize: this.handleMinimize,
Expand Down
13 changes: 11 additions & 2 deletions packages/livechat/src/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const api = {
Triggers.processRequest(info);
}

const { token, room: { _id: rid } = {} } = store.state;
const { token, room } = store.state;
const { _id: rid } = room || {};

const {
change,
title,
Expand Down Expand Up @@ -75,14 +77,21 @@ const api = {

async setDepartment(value) {
const {
user,
config: { departments = [] },
user: { department: existingDepartment } = {},
defaultAgent,
} = store.state;

const { department: existingDepartment } = user || {};

const department = departments.find((dep) => dep._id === value || dep.name === value)?._id || '';

updateIframeGuestData({ department });

if (defaultAgent && defaultAgent.department !== department) {
store.setState({ defaultAgent: null });
}

if (department !== existingDepartment) {
await loadConfig();
await loadMessages();
Expand Down
3 changes: 2 additions & 1 deletion packages/livechat/src/lib/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ export const loadMessages = async () => {
};

export const loadMoreMessages = async () => {
const { room: { _id: rid } = {}, messages = [], noMoreMessages = false } = store.state;
const { room, messages = [], noMoreMessages = false } = store.state;
const { _id: rid } = room || {};

if (!rid || noMoreMessages) {
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/livechat/src/lib/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const getAgent = (triggerAction) => {
return reject(error);
}

store.setState({ defaultAgent: { ...agent, ts: Date.now() } });
store.setState({ defaultAgent: { ...agent, department, ts: Date.now() } });
resolve(agent);
} else if (params.sender === 'custom') {
resolve({
Expand Down
3 changes: 2 additions & 1 deletion packages/livechat/src/routes/Chat/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ class ChatContainer extends Component {
return;
}

const { alerts, dispatch, room: { _id: rid } = {} } = this.props;
const { alerts, dispatch, room } = this.props;
const { _id: rid } = room || {};

await dispatch({ loading: true });
try {
Expand Down
34 changes: 13 additions & 21 deletions packages/livechat/src/routes/Register/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,34 +126,26 @@ class Register extends Component {
}

static getDerivedStateFromProps(nextProps, state) {
const { hasNameField, hasEmailField, hasDepartmentField, departmentDefault, departments, nameDefault, emailDefault } = nextProps;
const { hasNameField, hasEmailField, hasDepartmentField, nameDefault, emailDefault, departmentDefault } = nextProps;
const { name, email, department } = state;
const newState = {};

const nameValue = nameDefault || '';
if (hasNameField && (!state.name || state.name !== nameValue)) {
state = { ...state, name: { ...state.name, value: nameValue } };
} else if (!hasNameField) {
state = { ...state, name: null };
if (hasNameField && nameDefault && nameDefault !== name?.value) {
const error = validate(this.props, { name: 'name', value: nameDefault, regexp: name?.regexp });
newState.name = { ...name, value: nameDefault, error };
}

const emailValue = emailDefault || '';
if (hasEmailField && (!state.email || state.name !== emailValue)) {
state = { ...state, email: { ...state.email, value: emailValue } };
} else if (!hasEmailField) {
state = { ...state, email: null };
if (hasEmailField && emailDefault && emailDefault !== email?.value) {
const error = validate(this.props, { name: 'email', value: emailDefault, regexp: email?.regexp });
newState.email = { ...email, value: emailDefault, error };
}

const departmentValue = departmentDefault || getDefaultDepartment(departments);
const showDepartmentField = hasDepartmentField && departments && departments.length > 1;
if (showDepartmentField && (!state.department || state.department !== departmentValue)) {
state = { ...state, department: { ...state.department, value: departmentValue } };
} else if (!showDepartmentField) {
state = { ...state, department: null };
if (hasDepartmentField && departmentDefault && departmentDefault !== department?.value) {
const error = validate(this.props, { name: 'department', value: departmentDefault, regexp: department?.regexp });
newState.department = { ...department, value: departmentDefault, error };
}

for (const { fieldName: name, value, regexp } of getValidableFields(state)) {
const error = validate(nextProps, { name, value, regexp });
state = { ...state, [name]: { ...state[name], value, error, showError: false } };
}
return newState;
}

state = {
Expand Down

0 comments on commit bcc2e57

Please sign in to comment.