assistant-note/src/redux/dirMessage_reducer.js

68 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-02-04 21:59:35 -05:00
import {createSlice} from '@reduxjs/toolkit'
import {isEmpty} from "../utils/ObjectUtils";
/*
"fileName": filePath,
"filePath": filePath,
"dirFlag": true,
"childList": fileChildList
*/
2024-01-24 04:59:31 -05:00
export const dirMessageSlice = createSlice({
name: 'dirMessage',
initialState: {
2024-02-04 21:59:35 -05:00
data: [],
// selectDirKey:""
2024-01-24 04:59:31 -05:00
},
reducers: {
dirAdd: (state, action) => {
console.log("dirMessage:dirAdd", state, action)
2024-02-04 21:59:35 -05:00
if (action.payload) {
2024-01-24 21:07:09 -05:00
// 新添加进来的目录,要判断是否重复,如果重复则提示
2024-02-04 21:59:35 -05:00
let filter = state.data.filter((fileMessage) =>
fileMessage.filePath === action.payload[0].filePath
2024-01-24 21:07:09 -05:00
);
2024-02-04 21:59:35 -05:00
if (filter.length > 0) {
console.log('filter', filter)
} else {
2024-01-24 21:07:09 -05:00
// 添加进当前目录
2024-02-04 21:59:35 -05:00
state.data = [...new Set([...state.data, ...action.payload])];
console.log('state.data:', state.data)
2024-01-24 21:07:09 -05:00
}
2024-01-24 04:59:31 -05:00
}
2024-02-04 21:59:35 -05:00
},
nextDirAdd: (state, action) => {
console.log("dirMessage:nextDirAdd", state, action)
// 获取当前选中的key
let selectDirKey = action.payload.selectDirKey;
// 便利文件树找到对应的key并加入其中
// 如果包含下级目录则不更新,在刷新中更新。
state.data.forEach(file => {
if (file.filePath === selectDirKey && file.dirFlag && file.childList.length === 0) {
file.childList.push(action.payload.fileStateList[0].childList)
} else if (file.childList.length > 0) {
findChild(file.childList, action, selectDirKey)
}
})
2024-01-24 04:59:31 -05:00
}
}
})
2024-02-04 21:59:35 -05:00
function findChild(fileList, action, selectDirKey) {
fileList.forEach(file => {
if (file.filePath === selectDirKey && file.dirFlag &&
(isEmpty(file.childList) || (Array.isArray(file.childList) && file.childList.length === 0))) {
file.childList = action.payload.fileStateList[0].childList
return
}
if (file.dirFlag && Array.isArray(file.childList) && file.childList.length > 0) {
findChild(file.childList, action, selectDirKey)
}
})
}
export const {
dirAdd,
nextDirAdd
} = dirMessageSlice.actions
2024-01-24 04:59:31 -05:00
export default dirMessageSlice.reducer