Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

[NEW] Introduce clearLocalStorageWhenChatEnded setting logic #666

Merged
merged 7 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/lib/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Livechat } from '../api';
import { CallStatus, isCallOngoing } from '../components/Calls/CallStatus';
import { setCookies, upsert, canRenderMessage, createToken } from '../components/helpers';
import I18n from '../i18n';
import { store } from '../store';
import { store, initialState } from '../store';
import { normalizeAgent } from './api';
import Commands from './commands';
import constants from './constants';
Expand All @@ -21,6 +21,14 @@ export const closeChat = async ({ transcriptRequested } = {}) => {
await handleTranscript();
}

const { config: { settings: { clearLocalStorageWhenChatEnded } = {} } = {} } = store.state;

if (clearLocalStorageWhenChatEnded) {
// exclude UI-affecting flags
const { minimized, visible, undocked, expanded, ...initial } = initialState();
await store.setState(initial);
}

await loadConfig();
parentCall('callback', 'chat-ended');
route('/chat-finished');
Expand Down Expand Up @@ -127,7 +135,7 @@ const isAgentHidden = () => {

const transformAgentInformationOnMessage = (message) => {
const { user } = store.state;
if (message.u && message.u._id !== user._id && isAgentHidden()) {
if (message && user && message.u && message.u._id !== user._id && isAgentHidden()) {
return { ...message, u: { _id: message.u._id } };
}

Expand Down
4 changes: 4 additions & 0 deletions src/store/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export default class Store {
this.emit('change', [this._state, prevState, partialState]);
}

cleanState() {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
this._state = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just set {} as _state is not enough because this way the token field value won't be recreated, and this is an issue, if you start a new chat and then refresh the page, the chat history is gone.
That's the reason we need to "reset" the default state, the same we do when the local storage is created as you can see here:

const initialState = {
token: createToken(),
typing: [],
config: {
messages: {},
settings: {},
theme: {},
triggers: [],
departments: [],
resources: {},
},
messages: [],
user: null,
sound: {
src: '',
enabled: true,
play: false,
},
iframe: {
guest: {},
theme: {},
visible: true,
},
gdpr: {
accepted: false,
},
alerts: [],
visible: true,
minimized: true,
unread: null,
incomingCallAlert: null,
ongoingCall: null, // TODO: store call info like url, startTime, timeout, etc here
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @renatobecker , can you take a look again?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works like a charm now, but we should not change the following fields as they impact UI/UX.

  • minimized
  • visible
  • undocked
  • expanded

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes done!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but please check this comment

}

setStoredState(storedState) {
const prevState = this._state;

Expand Down
6 changes: 3 additions & 3 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Store from './Store';

const createToken = () => Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);

const initialState = {
export const initialState = () => ({
token: createToken(),
typing: [],
config: {
Expand Down Expand Up @@ -36,10 +36,10 @@ const initialState = {
unread: null,
incomingCallAlert: null,
ongoingCall: null, // TODO: store call info like url, startTime, timeout, etc here
};
});

const dontPersist = ['messages', 'typing', 'loading', 'alerts', 'unread', 'noMoreMessages', 'modal', 'incomingCallAlert', 'ongoingCall'];
export const store = new Store(initialState, { dontPersist });
export const store = new Store(initialState(), { dontPersist });

if (process.env.NODE_ENV === 'development') {
store.on('change', ([, , partialState]) => {
Expand Down