Skip to content

Commit

Permalink
修复渠道多次加载的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion committed Dec 5, 2023
1 parent 044a2dc commit d6ce413
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions web/src/components/ChannelsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ const ChannelsTable = () => {
}
}

const loadChannels = async (startIdx) => {
const loadChannels = async (startIdx, pageSize, idSort) => {
setLoading(true);
const res = await API.get(`/api/channel/?p=${startIdx}&page_size=${pageSize}&id_sort=${idSort}`);
const {success, message, data} = res.data;
Expand All @@ -272,39 +272,31 @@ const ChannelsTable = () => {
setLoading(false);
};

useEffect(() => {
loadChannels(0)
.then()
.catch((reason) => {
showError(reason);
});
}, [pageSize]);

const refresh = async () => {
await loadChannels(activePage - 1);
await loadChannels(activePage - 1, pageSize, idSort);
};

useEffect(() => {
loadChannels(0)
// console.log('default effect')
const localIdSort = localStorage.getItem('id-sort') === 'true';
setIdSort(localIdSort)
loadChannels(0, pageSize, localIdSort)
.then()
.catch((reason) => {
showError(reason);
});
fetchGroups().then();
console.log(localStorage.getItem('id-sort'))
if (localStorage.getItem('id-sort') === 'true') {
setIdSort(true)
}
}, []);

useEffect(() => {
searchChannels()
}, [searchGroup]);
// useEffect(() => {
// console.log('search effect')
// searchChannels()
// }, [searchGroup]);

useEffect(() => {
refresh()
localStorage.setItem('id-sort', idSort + '');
}, [idSort]);
// useEffect(() => {
// localStorage.setItem('id-sort', idSort + '');
// refresh()
// }, [idSort]);

const manageChannel = async (id, action, record, value) => {
let data = {id};
Expand Down Expand Up @@ -404,10 +396,10 @@ const ChannelsTable = () => {
}
};

const searchChannels = async () => {
const searchChannels = async (searchKeyword, searchGroup) => {
if (searchKeyword === '' && searchGroup === '') {
// if keyword is blank, load files instead.
await loadChannels(0);
await loadChannels(0, pageSize, idSort);
setActivePage(1);
return;
}
Expand Down Expand Up @@ -511,11 +503,21 @@ const ChannelsTable = () => {
setActivePage(page);
if (page === Math.ceil(channels.length / pageSize) + 1) {
// In this case we have to load more data and then append them.
loadChannels(page - 1).then(r => {
loadChannels(page - 1, pageSize, idSort).then(r => {
});
}
};

const handlePageSizeChange = async(size) => {
setPageSize(size)
setActivePage(1)
loadChannels(0, size, idSort)
.then()
.catch((reason) => {
showError(reason);
})
};

const fetchGroups = async () => {
try {
let res = await API.get(`/api/group/`);
Expand Down Expand Up @@ -549,7 +551,7 @@ const ChannelsTable = () => {
return (
<>
<EditChannel refresh={refresh} visible={showEdit} handleClose={closeEdit} editingChannel={editingChannel}/>
<Form onSubmit={searchChannels} labelPosition='left'>
<Form onSubmit={() => {searchChannels(searchKeyword, searchGroup)}} labelPosition='left'>

<div style={{display: 'flex'}}>
<Space>
Expand All @@ -565,6 +567,7 @@ const ChannelsTable = () => {
/>
<Form.Select field="group" label='分组' optionList={groupOptions} onChange={(v) => {
setSearchGroup(v)
searchChannels(searchKeyword, v)
}}/>
</Space>
</div>
Expand All @@ -573,7 +576,13 @@ const ChannelsTable = () => {
<Space>
<Typography.Text strong>使用ID排序</Typography.Text>
<Switch checked={idSort} label='使用ID排序' uncheckedText="关" aria-label="是否用ID排序" onChange={(v) => {
localStorage.setItem('id-sort', v + '')
setIdSort(v)
loadChannels(0, pageSize, v)
.then()
.catch((reason) => {
showError(reason);
})
}}></Switch>
</Space>
</div>
Expand All @@ -585,8 +594,7 @@ const ChannelsTable = () => {
pageSizeOpts: [10, 20, 50, 100],
showSizeChanger: true,
onPageSizeChange: (size) => {
setPageSize(size)
setActivePage(1)
handlePageSizeChange(size).then()
},
onPageChange: handlePageChange,
}} loading={loading} onRow={handleRow}/>
Expand Down

0 comments on commit d6ce413

Please sign in to comment.