-
-
Save svengiebel/42698019d8348ca1659e3249624724c9 to your computer and use it in GitHub Desktop.
working with dom input file data in react-reason problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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