-
-
Save svengiebel/d9d76d579ff908492dc60f49cd2b262e to your computer and use it in GitHub Desktop.
somewhat fixed test.re case
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 abstract] | |
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; | |
module Encode = { | |
let toJson = file => | |
Json.Encode.( | |
object_([ | |
("lastModified", file##lastModified |> int), | |
("lastModifiedDate", file##lastModifiedDate |> string), | |
("name", file##name |> string), | |
("size", file##size |> int), | |
("type", file##type_ |> string), | |
("webkitRelativePath", file##webkitRelativePath |> string), | |
]) | |
); | |
}; | |
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 => | |
let payload = Belt.Array.getExn(_state.files, 0); | |
Js.Promise.( | |
Fetch.fetchWithInit( | |
"/api/hello", | |
Fetch.RequestInit.make( | |
~method_=Post, | |
~body= | |
Fetch.BodyInit.make( | |
Js.Json.stringify(Encode.toJson(payload)), | |
), | |
~headers= | |
Fetch.HeadersInit.make({"Content-Type": "application/json"}), | |
(), | |
), | |
) | |
|> then_(Fetch.Response.json) | |
|> ignore | |
); | |
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