2020-09-14 09:44:26 +00:00
|
|
|
import fromUnixTime from 'date-fns/fromUnixTime';
|
|
|
|
import differenceInDays from 'date-fns/differenceInDays';
|
2020-02-16 11:50:38 +00:00
|
|
|
import Cookies from 'js-cookie';
|
2021-09-07 17:41:01 +00:00
|
|
|
import {
|
|
|
|
ANALYTICS_IDENTITY,
|
|
|
|
ANALYTICS_RESET,
|
|
|
|
CHATWOOT_RESET,
|
|
|
|
CHATWOOT_SET_USER,
|
|
|
|
} from '../../helper/scriptHelpers';
|
2022-10-04 04:29:02 +00:00
|
|
|
import { LocalStorage, LOCAL_STORAGE_KEYS } from '../../helper/localStorage';
|
2019-10-22 07:58:45 +00:00
|
|
|
|
2020-08-09 12:43:17 +00:00
|
|
|
Cookies.defaults = { sameSite: 'Lax' };
|
|
|
|
|
2019-10-22 07:58:45 +00:00
|
|
|
export const getLoadingStatus = state => state.fetchAPIloadingStatus;
|
|
|
|
export const setLoadingStatus = (state, status) => {
|
|
|
|
state.fetchAPIloadingStatus = status;
|
|
|
|
};
|
2020-02-16 11:50:38 +00:00
|
|
|
|
2022-04-14 15:24:26 +00:00
|
|
|
export const setUser = user => {
|
|
|
|
window.bus.$emit(CHATWOOT_SET_USER, { user });
|
|
|
|
window.bus.$emit(ANALYTICS_IDENTITY, { user });
|
2021-09-07 17:41:01 +00:00
|
|
|
};
|
2020-02-16 11:50:38 +00:00
|
|
|
|
2020-09-14 09:44:26 +00:00
|
|
|
export const getHeaderExpiry = response =>
|
|
|
|
fromUnixTime(response.headers.expiry);
|
2020-02-16 11:50:38 +00:00
|
|
|
|
|
|
|
export const setAuthCredentials = response => {
|
|
|
|
const expiryDate = getHeaderExpiry(response);
|
2022-04-14 15:24:26 +00:00
|
|
|
Cookies.set('cw_d_session_info', response.headers, {
|
2020-09-14 09:44:26 +00:00
|
|
|
expires: differenceInDays(expiryDate, new Date()),
|
2020-02-16 11:50:38 +00:00
|
|
|
});
|
|
|
|
setUser(response.data.data, expiryDate);
|
|
|
|
};
|
|
|
|
|
2022-03-03 15:19:51 +00:00
|
|
|
export const clearBrowserSessionCookies = () => {
|
2022-04-14 15:24:26 +00:00
|
|
|
Cookies.remove('cw_d_session_info');
|
2020-02-16 11:50:38 +00:00
|
|
|
Cookies.remove('auth_data');
|
|
|
|
Cookies.remove('user');
|
2022-03-03 15:19:51 +00:00
|
|
|
};
|
2022-02-25 11:06:36 +00:00
|
|
|
|
2022-10-04 04:29:02 +00:00
|
|
|
export const clearLocalStorageOnLogout = () => {
|
|
|
|
LocalStorage.remove(LOCAL_STORAGE_KEYS.DRAFT_MESSAGES);
|
|
|
|
};
|
|
|
|
|
2022-03-03 15:19:51 +00:00
|
|
|
export const clearCookiesOnLogout = () => {
|
|
|
|
window.bus.$emit(CHATWOOT_RESET);
|
|
|
|
window.bus.$emit(ANALYTICS_RESET);
|
|
|
|
clearBrowserSessionCookies();
|
2022-10-04 04:29:02 +00:00
|
|
|
clearLocalStorageOnLogout();
|
2022-02-25 11:06:36 +00:00
|
|
|
const globalConfig = window.globalConfig || {};
|
2022-04-14 15:24:26 +00:00
|
|
|
const logoutRedirectLink = globalConfig.LOGOUT_REDIRECT_LINK || '/';
|
2022-02-25 11:06:36 +00:00
|
|
|
window.location = logoutRedirectLink;
|
2020-02-16 11:50:38 +00:00
|
|
|
};
|
2022-06-24 17:45:46 +00:00
|
|
|
|
|
|
|
export const parseAPIErrorResponse = error => {
|
|
|
|
if (error?.response?.data?.message) {
|
|
|
|
return error?.response?.data?.message;
|
|
|
|
}
|
|
|
|
if (error?.response?.data?.error) {
|
|
|
|
return error?.response?.data?.error;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
};
|
2022-07-18 19:03:06 +00:00
|
|
|
|
|
|
|
export const throwErrorMessage = error => {
|
|
|
|
const errorMessage = parseAPIErrorResponse(error);
|
|
|
|
throw new Error(errorMessage);
|
|
|
|
};
|