You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB
TypeScript

import { AnyAction } from 'redux';
import { ShareActionTypes } from '../actions/share-actions';
import { ShareState, ShareFileInfo, ShareItem } from './interfaces';
const defaultState: ShareState = {
root: {
name: '/',
type: 'DIR',
children: [],
},
};
export default function (state = defaultState, action: AnyAction): ShareState {
switch (action.type) {
case ShareActionTypes.LOAD_SHARE_DATA_SUCCESS: {
const { values } = action.payload;
const { directory } = action.payload;
// Find directory item in storage
let dir = state.root;
for (const dirName of directory.split('/')) {
if (dirName) {
[dir] = dir.children.filter(
(child): boolean => child.name === dirName,
);
}
}
// Update its children
dir.children = (values as ShareFileInfo[])
.map((value): ShareItem => ({
...value,
children: [],
}));
return {
...state,
};
}
default:
return {
...state,
};
}
}