Skip to content

Instantly share code, notes, and snippets.

@svengiebel
Last active May 21, 2018 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svengiebel/42698019d8348ca1659e3249624724c9 to your computer and use it in GitHub Desktop.
Save svengiebel/42698019d8348ca1659e3249624724c9 to your computer and use it in GitHub Desktop.
working with dom input file data in react-reason problem
open Belt;
[@bs.deriving jsConverter]
type file = {
lastModified: int,
lastModifiedDate: string,
name: string,
size: int,
type_: string,
webkitRelativePath: string,
};
type fileList = array(file);
type state = {
noFile: bool,
files: fileList,
};
type action =
| HandleFileChange(fileList)
| UploadFile;
let component = ReasonReact.reducerComponent("Uploader");
let make = _children => {
...component,
initialState: _state => {noFile: true, files: [||]},
reducer: (action, _state) =>
switch (action) {
| HandleFileChange(fileList) =>
ReasonReact.Update({noFile: false, files: fileList})
| UploadFile =>
Js.Promise.(
Axios.postData(
"/test",
/* fileToJs creates an Object like this:
{
lastModified: undefined,
lastModifiedDate: undefined,
...
}
}
*/
fileToJs(Belt.Array.getExn(_state.files, 0)),
)
|> then_(response => resolve(Js.log(response##data)))
|> catch(error => resolve(Js.log(error)))
);
ReasonReact.NoUpdate;
},
render: _self =>
<div>
<input
onChange=(
_event =>
_self.send(
HandleFileChange(
ReactDOMRe.domElementToObj(ReactEventRe.Form.target(_event))##files,
),
)
)
_type="file"
name="importing_file"
/>
<div>
<button _type="button" onClick=((_) => _self.send(UploadFile))>
(ReasonReact.string("Importieren"))
</button>
</div>
</div>,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment