Skip to content

Commit

Permalink
removed shared cookie logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tigransimonyan committed Mar 14, 2024
1 parent ff973eb commit 7dc290b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 32 deletions.
11 changes: 2 additions & 9 deletions src/providers/Comments/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useReducer, useCallback, useEffect } from 'react';
import React, { useReducer, useCallback } from 'react';
import { useRequest } from 'providers/Requests';

export type IComment = {
Expand Down Expand Up @@ -111,7 +111,7 @@ type Props = {
};

export default function CommentsProvider(props: Props) {
const { instance, token } = useRequest();
const { instance } = useRequest();

const [state, dispatch] = useReducer(reducer, {
...initialState
Expand Down Expand Up @@ -150,13 +150,6 @@ export default function CommentsProvider(props: Props) {
dispatch({ type: 'REPLY_COMMENT', payload: comment });
}, []);

useEffect(() => {
console.log('token ', token);
if (token) {
getComments();
}
}, [getComments, token]);

return (
<CommentsStateContext.Provider value={state}>
<CommentsDispatchContext.Provider
Expand Down
29 changes: 6 additions & 23 deletions src/providers/Requests/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { useState, useMemo, useEffect } from 'react';
import React, { useState, useMemo } from 'react';
import axios, { AxiosInstance } from 'axios';
import { ClientJS } from 'clientjs';
import { getCookie } from 'react-use-cookie';
import { ErrorMessage, Close } from '../Comments/style';
import { FadeIn } from './style';

const RequestContext = React.createContext<
| {
instance: AxiosInstance;
token: string;
}
| undefined
>(undefined);
Expand All @@ -26,17 +26,18 @@ type Props = {

export default function RequestProvider(props: Props) {
const [error, setError] = useState('');
const [token, setToken] = useState('');

const instance = useMemo(() => {
const pageId = `${window.location.hostname}${window.location.pathname}`;
const client = new ClientJS();
const fingerprint = client.getFingerprint();
const token = getCookie('token');

return axios.create({
baseURL: process.env.REACT_APP_API_URL,
headers: {
fingerprint
fingerprint,
token
},
transformRequest: [
function (data) {
Expand All @@ -59,31 +60,13 @@ export default function RequestProvider(props: Props) {
});
}, [axios]);

useEffect(() => {
const messageListener = (e: any) => {
if (!e.data || e.data.sender !== 'zoomment' || !e.data.token) {
return;
}

instance.defaults.headers['token'] = e.data.token;
setToken(e.data.token);
};

window.addEventListener('message', messageListener, false);

return () => {
window.removeEventListener('message', messageListener);
};
}, []);

return (
<RequestContext.Provider value={{ instance, token }}>
<RequestContext.Provider value={{ instance }}>
{error && (
<ErrorMessage>
{error} <Close onClick={() => setError('')} />
</ErrorMessage>
)}
<iframe src="https://zoomment.com/token" style={{ display: 'none' }} />
<FadeIn>{props.children}</FadeIn>
</RequestContext.Provider>
);
Expand Down

0 comments on commit 7dc290b

Please sign in to comment.