Skip to content

Commit c5c82a3

Browse files
authored
Merge pull request #25 from Semeikin-Kirill/moving_history
[#7] moving through history
2 parents b674307 + 63de714 commit c5c82a3

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

src/components/Header.jsx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,57 @@ import { ArrowLeft, ArrowRight, ArrowClockwise } from 'react-bootstrap-icons';
66
import { useDispatch, useSelector } from 'react-redux';
77
import Tabs from './Tabs.jsx';
88
import Search from './Search.jsx';
9-
import { actions as tabDataActions } from '../slices/tabsData';
9+
import { updateTabData } from '../slices/tabsData';
1010

1111
import TabsContent from './TabsContent.jsx';
1212
import * as selectors from '../selectors';
1313

1414
const Header = () => {
1515
const activeTabId = useSelector(selectors.activeTabIdSelector);
1616
const currentLinks = useSelector(selectors.currentLinksSelector);
17+
const historyTabs = useSelector(selectors.historyTabsSelector);
18+
const currentHistoryTab = historyTabs[activeTabId] ?? [];
19+
const url = currentLinks[activeTabId];
20+
const currentIndexURL = currentHistoryTab.indexOf(url);
21+
const lastIndexHistory = currentHistoryTab.length - 1;
1722
const dispatch = useDispatch();
1823

1924
const refresh = async () => {
2025
if (activeTabId in currentLinks) {
21-
const url = currentLinks[activeTabId];
22-
try {
23-
const response = await fetch(url);
24-
const htmlDoc = await response.text();
25-
const data = { [activeTabId]: htmlDoc };
26-
dispatch(tabDataActions.updateTabData(data));
27-
} catch (e) {
28-
console.error('something wrong: ', e);
29-
}
26+
dispatch(updateTabData({ activeTabId, url }));
3027
}
3128
};
3229

30+
const backwardHistory = () => {
31+
const previosIndex = currentIndexURL - 1;
32+
dispatch(updateTabData({ activeTabId, url: currentHistoryTab[previosIndex] }));
33+
};
34+
35+
const forwardsHistory = () => {
36+
const nextIndex = currentIndexURL + 1;
37+
dispatch(updateTabData({ activeTabId, url: currentHistoryTab[nextIndex] }));
38+
};
39+
40+
const isFirstHistory = () => currentIndexURL === 0 || currentIndexURL === -1;
41+
42+
const isLastHistory = () => currentIndexURL === lastIndexHistory || currentIndexURL === -1;
43+
3344
return (
3445
<Tab.Container id="left-tabs-example" defaultActiveKey="first">
3546
<Tabs />
3647
<Row>
37-
<Col sm={1} className="d-flex justify-content-between align-items-center">
38-
<ArrowLeft size={26} />
39-
<ArrowRight size={26} />
48+
<Col sm={2} className="d-flex justify-content-around align-items-center">
49+
<Button disabled={isFirstHistory()} variant="outline-secondary" size="sm" onClick={backwardHistory}>
50+
<ArrowLeft size={26} />
51+
</Button>
52+
<Button disabled={isLastHistory()} variant="outline-secondary" size="sm" onClick={forwardsHistory}>
53+
<ArrowRight size={26} />
54+
</Button>
4055
<Button variant="outline-secondary" size="sm" onClick={refresh}>
4156
<ArrowClockwise size={26} />
4257
</Button>
4358
</Col>
44-
<Col sm={11}>
59+
<Col sm={10}>
4560
<Search />
4661
</Col>
4762
</Row>

src/selectors/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ export const tabsListSelector = (state) => state.tabs.tabsList;
77
export const tabsDataSelector = (state) => state.tabsData.data;
88

99
export const currentLinksSelector = (state) => state.tabsData.currentLinks;
10+
11+
export const historyTabsSelector = (state) => state.tabsData.history;

src/slices/tabsData.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/* eslint-disable no-param-reassign */
2-
import { createSlice } from '@reduxjs/toolkit';
2+
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
3+
4+
export const updateTabData = createAsyncThunk('tabsData/updateTabData', async ({ activeTabId, url }) => {
5+
const response = await fetch(url);
6+
const htmlDoc = await response.text();
7+
const data = { [activeTabId]: htmlDoc };
8+
return { data, url: { [activeTabId]: url } };
9+
});
310

411
const initialState = {
512
data: {},
@@ -17,9 +24,13 @@ const tabsData = createSlice({
1724
state.history[tabId] = tabId in state.history ? [...state.history[tabId], url] : [url];
1825
state.currentLinks = { ...state.currentLinks, [tabId]: url };
1926
},
20-
updateTabData: (state, { payload }) => {
21-
state.data = { ...state.data, ...payload };
22-
},
27+
},
28+
extraReducers(builder) {
29+
builder.addCase(updateTabData.fulfilled, (state, { payload }) => {
30+
const { data, url } = payload;
31+
state.data = { ...state.data, ...data };
32+
state.currentLinks = { ...state.currentLinks, ...url };
33+
});
2334
},
2435
});
2536

0 commit comments

Comments
 (0)