feat setup: Setup Redux, Immutable.js and React
feat FileList: Implement File List + changedir action
This commit is contained in:
8
src/js/actions/changedir.js
Normal file
8
src/js/actions/changedir.js
Normal file
@ -0,0 +1,8 @@
|
||||
import { CHANGE_DIRECTORY } from 'actions/types';
|
||||
|
||||
export default function changedir(dir) {
|
||||
return {
|
||||
type: CHANGE_DIRECTORY,
|
||||
dir
|
||||
};
|
||||
}
|
8
src/js/actions/list-files.js
Normal file
8
src/js/actions/list-files.js
Normal file
@ -0,0 +1,8 @@
|
||||
import { LIST_FILES } from 'actions/types';
|
||||
|
||||
export default function listFiles(files) {
|
||||
return {
|
||||
type: LIST_FILES,
|
||||
files
|
||||
};
|
||||
}
|
9
src/js/actions/types.js
Normal file
9
src/js/actions/types.js
Normal file
@ -0,0 +1,9 @@
|
||||
const TYPES = {
|
||||
CHANGE_DIRECTORY: Symbol(),
|
||||
LIST_FILES: Symbol(),
|
||||
SORT: Symbol(),
|
||||
SEARCH: Symbol(),
|
||||
REFRESH: Symbol()
|
||||
};
|
||||
|
||||
export default TYPES;
|
13
src/js/api/files.js
Normal file
13
src/js/api/files.js
Normal file
@ -0,0 +1,13 @@
|
||||
export async function directory(dir = '/') {
|
||||
let storage = navigator.getDeviceStorage('sdcard');
|
||||
let root = await storage.getRoot();
|
||||
|
||||
if (dir === '/' || !dir) return root;
|
||||
|
||||
return await root.get(dir);
|
||||
}
|
||||
|
||||
export async function children(dir) {
|
||||
let parent = await directory(dir);
|
||||
return await parent.getFilesAndDirectories();
|
||||
}
|
38
src/js/components/file-list.js
Normal file
38
src/js/components/file-list.js
Normal file
@ -0,0 +1,38 @@
|
||||
import React, { Component } from 'react';
|
||||
import { connect } from 'react-redux';
|
||||
import File from './file';
|
||||
|
||||
@connect(props)
|
||||
export default class FileList extends Component {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
render() {
|
||||
let { cwd, files } = this.props;
|
||||
|
||||
let els = files.map((file, index) => {
|
||||
return <File key={index} index={index} name={file.name} />;
|
||||
});
|
||||
|
||||
return (
|
||||
<div><strong>cwd: {cwd}</strong>
|
||||
{els}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function props(state) {
|
||||
return {
|
||||
cwd: state.get('cwd'),
|
||||
files: state.get('files')
|
||||
}
|
||||
}
|
||||
|
||||
async function getFiles(dir) {
|
||||
let storage = navigator.getDeviceStorage('sdcard');
|
||||
let root = await storage.get(dir);
|
||||
|
||||
return await root.getFilesAndDirectories();
|
||||
}
|
20
src/js/components/file.js
Normal file
20
src/js/components/file.js
Normal file
@ -0,0 +1,20 @@
|
||||
import React, { Component } from 'react';
|
||||
import store from 'store';
|
||||
import changedir from 'actions/changedir';
|
||||
|
||||
export default class File extends Component {
|
||||
render() {
|
||||
return (
|
||||
<div onClick={this.peekInside.bind(this)}>
|
||||
<p>{this.props.index}. {this.props.name}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
peekInside() {
|
||||
let file = store.getState().get('files')[this.props.index];
|
||||
|
||||
console.log(file);
|
||||
store.dispatch(changedir(file.path.slice(1) + file.name));
|
||||
}
|
||||
}
|
@ -1,5 +1,18 @@
|
||||
export default React.createClass({
|
||||
import React, { Component } from 'react'
|
||||
import FileList from 'components/file-list';
|
||||
import changedir from 'actions/changedir';
|
||||
import store from 'store';
|
||||
|
||||
window.store = store;
|
||||
window.changedir = changedir;
|
||||
|
||||
export default class Root extends Component {
|
||||
render() {
|
||||
return <div></div>;
|
||||
return (
|
||||
<div>
|
||||
Hawk!
|
||||
<FileList />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,2 +1,7 @@
|
||||
import Root from 'components/root'
|
||||
let x = "I'm just testing";
|
||||
import React from 'react';
|
||||
import Root from 'components/root';
|
||||
import store from 'store';
|
||||
import { Provider } from 'react-redux';
|
||||
|
||||
let wrapper = document.getElementById('wrapper');
|
||||
React.render(<Provider store={store}>{() => <Root />}</Provider>, wrapper);
|
||||
|
10
src/js/reducers/all.js
Normal file
10
src/js/reducers/all.js
Normal file
@ -0,0 +1,10 @@
|
||||
import Immutable from 'immutable';
|
||||
import cwd from './cwd';
|
||||
import files from './files';
|
||||
|
||||
export default function(state = new Immutable.Map(), action) {
|
||||
return new Immutable.Map({
|
||||
cwd: cwd(state.get('cwd'), action),
|
||||
files: files(state.get('files'), action)
|
||||
});
|
||||
}
|
16
src/js/reducers/cwd.js
Normal file
16
src/js/reducers/cwd.js
Normal file
@ -0,0 +1,16 @@
|
||||
import { CHANGE_DIRECTORY } from 'actions/types';
|
||||
import listFiles from 'actions/list-files';
|
||||
import { children } from 'api/files';
|
||||
import store from 'store';
|
||||
|
||||
export default function(state = '/', action) {
|
||||
switch (action.type) {
|
||||
case CHANGE_DIRECTORY:
|
||||
children(action.dir).then(files => {
|
||||
store.dispatch(listFiles(files));
|
||||
});
|
||||
return action.dir;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
10
src/js/reducers/files.js
Normal file
10
src/js/reducers/files.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { LIST_FILES } from 'actions/types';
|
||||
|
||||
export default function(state = [], action) {
|
||||
switch (action.type) {
|
||||
case LIST_FILES:
|
||||
return action.files;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
14
src/js/store.js
Normal file
14
src/js/store.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { createStore } from 'redux';
|
||||
import reducers from 'reducers/all';
|
||||
import changedir from 'actions/changedir';
|
||||
import Immutable from 'immutable';
|
||||
|
||||
const DEFAULT = new Immutable.Map({
|
||||
dir: '/',
|
||||
files: []
|
||||
});
|
||||
|
||||
let store = createStore(reducers, DEFAULT);
|
||||
store.dispatch(changedir(DEFAULT.dir));
|
||||
|
||||
export default store;
|
Reference in New Issue
Block a user