diff --git a/src/redux/explorer/action.ts b/src/redux/explorer/action.ts index 01adb28..3094f8f 100644 --- a/src/redux/explorer/action.ts +++ b/src/redux/explorer/action.ts @@ -1,86 +1,97 @@ -import { AnyAction } from 'redux' -import { ThunkAction } from 'redux-thunk' -import { CloudreveFile, SortMethod } from './../../types/index' +import { AnyAction } from "redux"; +import { ThunkAction } from "redux-thunk"; +import { CloudreveFile, SortMethod } from "./../../types/index"; export interface ActionSetFileList extends AnyAction { - type: 'SET_FILE_LIST'; - list: CloudreveFile[]; + type: "SET_FILE_LIST"; + list: CloudreveFile[]; } export const setFileList = (list: CloudreveFile[]): ActionSetFileList => { - return { - type: 'SET_FILE_LIST', - list, - } -} - -export interface ActionSetDirList extends AnyAction { - type: 'SET_DIR_LIST'; - list: CloudreveFile[]; -} -export const setDirList = (list: CloudreveFile[]): ActionSetDirList => { - return { - type: 'SET_DIR_LIST', - list, - } -} - -export interface ActionSetSortMethod extends AnyAction { - type: 'SET_SORT_METHOD'; - method: SortMethod; -} -export const setSortMethod = (method: SortMethod): ActionSetSortMethod => { - return { - type: 'SET_SORT_METHOD', - method, - } -} -type SortFunc = (a: CloudreveFile, b: CloudreveFile) => number -const sortMethodFuncs: Record = { - sizePos: (a: CloudreveFile, b: CloudreveFile) => { - return a.size-b.size - }, - sizeRes: (a: CloudreveFile, b: CloudreveFile) => { - return b.size-a.size - }, - namePos: (a: CloudreveFile, b: CloudreveFile) => { - return a.name.localeCompare(b.name) - }, - nameRev: (a: CloudreveFile, b: CloudreveFile) => { - return b.name.localeCompare(a.name) - }, - timePos: (a: CloudreveFile, b: CloudreveFile) => { - return Date.parse(a.date)-Date.parse(b.date) - }, - timeRev: (a: CloudreveFile, b: CloudreveFile) => { - return Date.parse(b.date)-Date.parse(a.date) - }, -} - -export const updateFileList = (list: CloudreveFile[]): ThunkAction => { - return (dispatch, getState): void => { - const state = getState() - // TODO: define state type - const { sortMethod } = state.viewUpdate - const dirList = list.filter((x) => { - return x.type === "dir"; - }) - const fileList = list.filter((x) => { - return x.type === "file"; - }) - const sortFunc = sortMethodFuncs[sortMethod as SortMethod] - dispatch(setDirList(dirList.sort(sortFunc))) - dispatch(setFileList(fileList.sort(sortFunc))) - } -} - -export const changeSortMethod = (method: SortMethod): ThunkAction => { - return (dispatch, getState): void => { - const state = getState() - const { fileList, dirList } = state.explorer - const sortFunc = sortMethodFuncs[method] - dispatch(setSortMethod(method)) - dispatch(setDirList(dirList.sort(sortFunc))) - dispatch(setFileList(fileList.sort(sortFunc))) - } + return { + type: "SET_FILE_LIST", + list + }; }; +export interface ActionSetDirList extends AnyAction { + type: "SET_DIR_LIST"; + list: CloudreveFile[]; +} +export const setDirList = (list: CloudreveFile[]): ActionSetDirList => { + return { + type: "SET_DIR_LIST", + list + }; +}; + +export interface ActionSetSortMethod extends AnyAction { + type: "SET_SORT_METHOD"; + method: SortMethod; +} +export const setSortMethod = (method: SortMethod): ActionSetSortMethod => { + return { + type: "SET_SORT_METHOD", + method + }; +}; +type SortFunc = (a: CloudreveFile, b: CloudreveFile) => number; +const sortMethodFuncs: Record = { + sizePos: (a: CloudreveFile, b: CloudreveFile) => { + return a.size - b.size; + }, + sizeRes: (a: CloudreveFile, b: CloudreveFile) => { + return b.size - a.size; + }, + namePos: (a: CloudreveFile, b: CloudreveFile) => { + return a.name.localeCompare( + b.name, + navigator.languages[0] || navigator.language, + { numeric: true, ignorePunctuation: true } + ); + }, + nameRev: (a: CloudreveFile, b: CloudreveFile) => { + return b.name.localeCompare( + a.name, + navigator.languages[0] || navigator.language, + { numeric: true, ignorePunctuation: true } + ); + }, + timePos: (a: CloudreveFile, b: CloudreveFile) => { + return Date.parse(a.date) - Date.parse(b.date); + }, + timeRev: (a: CloudreveFile, b: CloudreveFile) => { + return Date.parse(b.date) - Date.parse(a.date); + } +}; + +export const updateFileList = ( + list: CloudreveFile[] +): ThunkAction => { + return (dispatch, getState): void => { + const state = getState(); + // TODO: define state type + const { sortMethod } = state.viewUpdate; + const dirList = list.filter(x => { + return x.type === "dir"; + }); + const fileList = list.filter(x => { + return x.type === "file"; + }); + const sortFunc = sortMethodFuncs[sortMethod as SortMethod]; + dispatch(setDirList(dirList.sort(sortFunc))); + dispatch(setFileList(fileList.sort(sortFunc))); + }; +}; + +export const changeSortMethod = ( + method: SortMethod +): ThunkAction => { + return (dispatch, getState): void => { + const state = getState(); + const { fileList, dirList } = state.explorer; + const sortFunc = sortMethodFuncs[method]; + dispatch(setSortMethod(method)); + dispatch(setDirList(dirList.sort(sortFunc))); + dispatch(setFileList(fileList.sort(sortFunc))); + }; +};