Skip to content

Commit 1f4e616

Browse files
authored
Add refreshToken prop and handleRefreshToken function (#350)
1 parent e7c94f4 commit 1f4e616

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

src/components/containers/EventsBrowser.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type EventBrowserProps = {
4747
tableHeaderItems?: any;
4848
theme?: string;
4949
dataLoading?: any;
50+
refreshToken?: () => void;
5051
};
5152

5253
interface EventBrowserState {
@@ -80,6 +81,7 @@ class EventsBrowser extends React.Component<EventBrowserProps, EventBrowserState
8081
constructor(props) {
8182
super(props);
8283
autobind(this);
84+
8385
this.state = {
8486
resultsPerPage: 20,
8587
filtersOpen: false,
@@ -155,6 +157,12 @@ class EventsBrowser extends React.Component<EventBrowserProps, EventBrowserState
155157
this.props.createSession(this.props.auditLogToken, this.props.host);
156158
}
157159

160+
handleRefreshToken() {
161+
if (typeof this.props.refreshToken === "function") {
162+
this.props.refreshToken();
163+
}
164+
}
165+
158166
componentWillReceiveProps(nextProps) {
159167
if (this.props.currentResults !== nextProps.currentResults) {
160168
this.onEventsChange(this.props.currentResults, nextProps.currentResults);
@@ -199,8 +207,7 @@ class EventsBrowser extends React.Component<EventBrowserProps, EventBrowserState
199207

200208
skipViewLogEvent: this.props.skipViewLogEvent,
201209
};
202-
203-
this.props.requestEventSearch(queryObj);
210+
this.props.requestEventSearch(queryObj, this.handleRefreshToken);
204211
}
205212

206213
nextPage() {
@@ -613,8 +620,8 @@ export default connect(
613620
tableHeaderItems: state.ui.eventsUiData.eventTableHeaderItems,
614621
}),
615622
(dispatch: any) => ({
616-
requestEventSearch(query) {
617-
return dispatch(requestEventSearch(query));
623+
requestEventSearch(query, handleRefreshToken) {
624+
return dispatch(requestEventSearch(query, handleRefreshToken));
618625
},
619626
createSession(token, host) {
620627
return dispatch(createSession(token, host));

src/dev/LogsViewerWrapper.tsx

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,32 @@ const actorId = "dev";
1010
const LogsViewerWrapper = (props) => {
1111
const [token, setToken] = useState("");
1212

13-
useEffect(() => {
14-
if (!token) {
15-
const urlWithQuery = `${endpoint}/publisher/v1/project/${projectId}/viewertoken?group_id=${groupId}&actor_id=${actorId}&is_admin=true`;
16-
fetch(urlWithQuery, {
17-
method: "GET",
18-
headers: {
19-
Accept: "application/json",
20-
Authorization: `Token token=${apiKey}`,
21-
},
22-
}).then((res) => {
23-
if (res.ok) {
24-
res.json().then((data) => {
25-
setToken(data.token);
26-
});
13+
const fetchToken = () => {
14+
const urlWithQuery = `${endpoint}/publisher/v1/project/${projectId}/viewertoken?group_id=${groupId}&actor_id=${actorId}&is_admin=true`;
15+
fetch(urlWithQuery, {
16+
method: "GET",
17+
headers: {
18+
Accept: "application/json",
19+
Authorization: `Token token=${apiKey}`,
20+
},
21+
}).then((res) => {
22+
if (res.ok) {
23+
res.json().then((data) => {
24+
setToken(data.token);
25+
});
2726

28-
return;
29-
}
27+
return;
28+
}
3029

31-
throw new Error("Failed to fetch viewer token");
32-
});
30+
throw new Error("Failed to fetch viewer token");
31+
});
32+
};
33+
34+
useEffect(() => {
35+
if (!token) {
36+
fetchToken();
3337
}
34-
}, []);
38+
}, [token]);
3539

3640
if (!token) {
3741
return <progress> </progress>;
@@ -70,6 +74,7 @@ const LogsViewerWrapper = (props) => {
7074
fields={props.fields ? (Array.isArray(props.fields) ? props.fields : defaultFields) : defaultFields}
7175
disableShowRawEvent={props.disableShowRawEvent ? props.disableShowRawEvent : false}
7276
skipViewLogEvent={true}
77+
refreshToken={fetchToken}
7378
/>
7479
);
7580
};

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type RetracedEventsBrowserProps = {
2424
disableShowRawEvent?: boolean;
2525
fields?: EventField[];
2626
skipViewLogEvent?: boolean;
27+
refreshToken?: () => void;
2728
};
2829

2930
const store = configStore();
@@ -46,6 +47,7 @@ export default class RetracedEventsBrowser extends React.Component<RetracedEvent
4647
fields={this.props.fields || []}
4748
disableShowRawEvent={this.props.disableShowRawEvent}
4849
skipViewLogEvent={this.props.skipViewLogEvent}
50+
refreshToken={this.props.refreshToken}
4951
/>
5052
</Provider>
5153
</div>

src/redux/data/events/thunks.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ from "lodash";
22
import { receiveEventList } from "./actions";
33
import { loadingData } from "../../ui/actions";
44

5-
export function requestEventSearch(query) {
5+
export function requestEventSearch(query, refreshToken) {
66
return async (dispatch, getState) => {
77
dispatch(loadingData("eventFetch", true));
88
let data;
@@ -83,10 +83,16 @@ export function requestEventSearch(query) {
8383
dispatch(loadingData("eventFetch", false));
8484
return null;
8585
}
86-
87-
const events = data.data.search.edges.map(({ node }) => node);
88-
const cursor = data.data.search.pageInfo.hasPreviousPage && _.last(data.data.search.edges).cursor;
89-
dispatch(receiveEventList(query, data.data.search.totalCount, events, cursor));
90-
dispatch(loadingData("eventFetch", false));
86+
if (data?.data?.search?.edges && Array.isArray(data.data.search.edges)) {
87+
const events = data.data.search.edges.map(({ node }) => node);
88+
const cursor = data.data.search.pageInfo.hasPreviousPage && _.last(data.data.search.edges).cursor;
89+
dispatch(receiveEventList(query, data.data.search.totalCount, events, cursor));
90+
dispatch(loadingData("eventFetch", false));
91+
} else {
92+
dispatch(loadingData("eventFetch", false));
93+
if (refreshToken && typeof refreshToken === "function") {
94+
refreshToken();
95+
}
96+
}
9197
};
9298
}

0 commit comments

Comments
 (0)