import update from 'immutability-helper' function find(state, action) { let _index = -1 state.videoes.some((item, index) => { // 同文件不同名字 fileHash也是一样的 if (item.loaded != 100 && (action.uploadInfo.fileHash == item.fileHash && action.uploadInfo.file.name == item.name)) { _index = index return true; } }) return _index; } export function reducer(state, action) { switch (action.type) { case 'addVideo': const uploadInfo = action.uploadInfo return {videoes: [...state.videoes, { name: uploadInfo.file.name, size: uploadInfo.file.size, type: uploadInfo.file.type, fileHash: uploadInfo.fileHash, // "ba1bbc53fdecd9eaaae479fbd9518442" state: uploadInfo.state, // "Uploading" "Ready" "Success" videoId: uploadInfo.videoId, // "719b82c875c34ac39f94feb145d25ad2" loaded: 0, title: '' }]}; case 'removeVideo': return { videoes: update(state.videoes, {$splice: [[action.index, 1]]}) } case 'updateProgress': let _index = find(state, action) let newVideoes = state.videoes // 删除先执行 if (_index != -1) { newVideoes = update(state.videoes, {[_index]: { loaded: {$set: action.progressPercent}, videoId: {$set: action.uploadInfo.videoId}, // addFileSuccess的时候没有fileHash fileHash: {$set: action.uploadInfo.fileHash} }}) } return {videoes: newVideoes}; case 'updateTitle': let _upadteIndex = action.index let newVideoes2 = state.videoes if (_upadteIndex != -1) { newVideoes2 = update(state.videoes, {[_upadteIndex]: { title: {$set: action.title}, }}) } return {videoes: newVideoes2}; default: throw new Error(); } } export const initialState = {videoes: []};