修复渠道多次加载的bug

This commit is contained in:
CaIon
2023-12-05 21:10:08 +08:00
parent 044a2dc7ef
commit d6ce413c92

View File

@@ -254,7 +254,7 @@ const ChannelsTable = () => {
} }
} }
const loadChannels = async (startIdx) => { const loadChannels = async (startIdx, pageSize, idSort) => {
setLoading(true); setLoading(true);
const res = await API.get(`/api/channel/?p=${startIdx}&page_size=${pageSize}&id_sort=${idSort}`); const res = await API.get(`/api/channel/?p=${startIdx}&page_size=${pageSize}&id_sort=${idSort}`);
const {success, message, data} = res.data; const {success, message, data} = res.data;
@@ -272,39 +272,31 @@ const ChannelsTable = () => {
setLoading(false); setLoading(false);
}; };
useEffect(() => {
loadChannels(0)
.then()
.catch((reason) => {
showError(reason);
});
}, [pageSize]);
const refresh = async () => { const refresh = async () => {
await loadChannels(activePage - 1); await loadChannels(activePage - 1, pageSize, idSort);
}; };
useEffect(() => { useEffect(() => {
loadChannels(0) // console.log('default effect')
const localIdSort = localStorage.getItem('id-sort') === 'true';
setIdSort(localIdSort)
loadChannels(0, pageSize, localIdSort)
.then() .then()
.catch((reason) => { .catch((reason) => {
showError(reason); showError(reason);
}); });
fetchGroups().then(); fetchGroups().then();
console.log(localStorage.getItem('id-sort'))
if (localStorage.getItem('id-sort') === 'true') {
setIdSort(true)
}
}, []); }, []);
useEffect(() => { // useEffect(() => {
searchChannels() // console.log('search effect')
}, [searchGroup]); // searchChannels()
// }, [searchGroup]);
useEffect(() => { // useEffect(() => {
refresh() // localStorage.setItem('id-sort', idSort + '');
localStorage.setItem('id-sort', idSort + ''); // refresh()
}, [idSort]); // }, [idSort]);
const manageChannel = async (id, action, record, value) => { const manageChannel = async (id, action, record, value) => {
let data = {id}; let data = {id};
@@ -404,10 +396,10 @@ const ChannelsTable = () => {
} }
}; };
const searchChannels = async () => { const searchChannels = async (searchKeyword, searchGroup) => {
if (searchKeyword === '' && searchGroup === '') { if (searchKeyword === '' && searchGroup === '') {
// if keyword is blank, load files instead. // if keyword is blank, load files instead.
await loadChannels(0); await loadChannels(0, pageSize, idSort);
setActivePage(1); setActivePage(1);
return; return;
} }
@@ -511,11 +503,21 @@ const ChannelsTable = () => {
setActivePage(page); setActivePage(page);
if (page === Math.ceil(channels.length / pageSize) + 1) { if (page === Math.ceil(channels.length / pageSize) + 1) {
// In this case we have to load more data and then append them. // 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 () => { const fetchGroups = async () => {
try { try {
let res = await API.get(`/api/group/`); let res = await API.get(`/api/group/`);
@@ -549,7 +551,7 @@ const ChannelsTable = () => {
return ( return (
<> <>
<EditChannel refresh={refresh} visible={showEdit} handleClose={closeEdit} editingChannel={editingChannel}/> <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'}}> <div style={{display: 'flex'}}>
<Space> <Space>
@@ -565,6 +567,7 @@ const ChannelsTable = () => {
/> />
<Form.Select field="group" label='分组' optionList={groupOptions} onChange={(v) => { <Form.Select field="group" label='分组' optionList={groupOptions} onChange={(v) => {
setSearchGroup(v) setSearchGroup(v)
searchChannels(searchKeyword, v)
}}/> }}/>
</Space> </Space>
</div> </div>
@@ -573,7 +576,13 @@ const ChannelsTable = () => {
<Space> <Space>
<Typography.Text strong>使用ID排序</Typography.Text> <Typography.Text strong>使用ID排序</Typography.Text>
<Switch checked={idSort} label='使用ID排序' uncheckedText="关" aria-label="是否用ID排序" onChange={(v) => { <Switch checked={idSort} label='使用ID排序' uncheckedText="关" aria-label="是否用ID排序" onChange={(v) => {
localStorage.setItem('id-sort', v + '')
setIdSort(v) setIdSort(v)
loadChannels(0, pageSize, v)
.then()
.catch((reason) => {
showError(reason);
})
}}></Switch> }}></Switch>
</Space> </Space>
</div> </div>
@@ -585,8 +594,7 @@ const ChannelsTable = () => {
pageSizeOpts: [10, 20, 50, 100], pageSizeOpts: [10, 20, 50, 100],
showSizeChanger: true, showSizeChanger: true,
onPageSizeChange: (size) => { onPageSizeChange: (size) => {
setPageSize(size) handlePageSizeChange(size).then()
setActivePage(1)
}, },
onPageChange: handlePageChange, onPageChange: handlePageChange,
}} loading={loading} onRow={handleRow}/> }} loading={loading} onRow={handleRow}/>