Skip to content

Commit

Permalink
file-uploader example: minor fixes suggested by @paldepind
Browse files Browse the repository at this point in the history
  • Loading branch information
ericgj committed Sep 3, 2015
1 parent 971edb8 commit 6e99ca5
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 303 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -25,5 +25,6 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
jspm_packages

*.swp
47 changes: 27 additions & 20 deletions examples/file-uploader/js/app.js
Expand Up @@ -13,6 +13,12 @@ const uploadList = require('./list');
const uploader = require('./uploader');


// app constants

const UPLOAD_URL = '/upload'
const UPLOAD_HEADERS = {}


// action

const listUpdate = (listAction,model) => {
Expand All @@ -28,9 +34,9 @@ const Action = Type({
});

const update = Action.caseOn({
Create: (up,files,model) => {
return listUpdate( uploadList.Action.Create(up,files), model );
},
Create: (up,files,model) => (
listUpdate( uploadList.Action.Create(up,files), model )
),

Route: listUpdate
});
Expand All @@ -42,31 +48,32 @@ const init = () => { return { uploads: uploadList.init() }; }

// view

const view = curry( ({url, headers, action$}, model) => {

const up = uploader.upload(headers, url);

const form = (
h('form', {on: {submit: preventDefault} }, [
h('input',
{ props: {type: 'file', multiple: true},
on: {
change: compose(action$, Action.Create(up), getTarget('files'))
}
}
)
]
)
);
const view = curry( ({action$}, model) => {

const up = uploader.upload(UPLOAD_HEADERS, UPLOAD_URL);

return (
h('div.uploading', {}, [
form,
form(action$, up),
uploadList.view(model.uploads)
])
);
});

const form = (action$, up) => (
h('form', {on: {submit: preventDefault} }, [
h('input',
{ props: {type: 'file', multiple: true},
on: {
change: compose(action$, Action.Create(up), getTarget('files'))
}
}
)
]
)
);


const getTarget = curry( (key,e) => e.target[key] );
const preventDefault = invoker(0, 'preventDefault');

Expand Down
92 changes: 50 additions & 42 deletions examples/file-uploader/js/build.js

Large diffs are not rendered by default.

40 changes: 21 additions & 19 deletions examples/file-uploader/js/list.js
Expand Up @@ -9,11 +9,11 @@ const h = require('snabbdom/h');
const upload = require('./upload');
const uploader = require('./uploader');

const sync = (s) => [s, []];
const noFx = (s) => [s, []];

// note: prefer to check if iterable,
// but FileList.prototype doesn't seem to have Symbol.iterator cross-browser?
const isFileList = (x) => !(undefined === x.length)
const isFileList = (x) => x.length !== undefined

// action

Expand All @@ -36,7 +36,7 @@ const update = Action.caseOn({
const finish = (type) => () => {
return adjust(upload.update(upload.Action[type]()), i, model);
};
return sync(
return noFx(
uploader.Result.case({
OK: finish('Uploaded'),
NotFound: finish('Error'),
Expand All @@ -61,27 +61,29 @@ const nextIndex = (model) => model.length;

const view = (model) => {

const style = {'list-style': 'none',
'-webkit-margin-before': 0,
'-webkit-margin-after': 0,
'-webkit-padding-start': 0
};

const listItemView = (item, i) => {
const substyle = { };
const subview = upload.view(
{ progress: { height: 20, width: 200 } },
item
);
return h('li', {style: substyle}, [subview]);
}

return (
h('ul', {style}, model.map( listItemView ) )
h('ul', {style: style.ul}, model.map( listItemView ) )
);

};

const listItemView = (item, i) => {
return (
h('li', {style: style.li}, [
upload.view(
{ progress: { height: 20, width: 200 } },
item
)
])
);
}


const style = {
ul: {'list-style': 'none'},
li: { }
}


module.exports = { init, update, Action, view }

2 changes: 1 addition & 1 deletion examples/file-uploader/js/main.js
Expand Up @@ -14,7 +14,7 @@ const app = require('./app');
let state = app.init(), asyncActions, vnode

const render = () => {
vnode = patch(vnode, app.view({action$: update, url: '/upload'}, state));
vnode = patch(vnode, app.view({action$: update}, state));
};

const update = (action) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/file-uploader/js/test.js
Expand Up @@ -17,7 +17,7 @@ const noop = function(){}

/******************************************************************************/
const progressUpload = (total, loaded, data) => {
return upload.update( upload.Action.Progress({total,loaded},noop), data );
return upload.update( upload.Action.Progress(noop, {total,loaded}), data );
}

const finishUpload = upload.update(upload.Action.Uploaded());
Expand Down Expand Up @@ -75,7 +75,7 @@ const dummyUploader = curry( (total, steps, abort, final, files) => {
const progress = (loaded, delay) => {
delay = delay + ( Math.random() * 2000 );
setTimeout(
() => res(uploader.Result.Progress({total,loaded},abort)),
() => res(uploader.Result.Progress(abort, {total,loaded})),
delay
);
return delay;
Expand Down

0 comments on commit 6e99ca5

Please sign in to comment.