From e28bf2648c1d36f96e6a3143a6336f6cb7aa7e1f Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 1 Apr 2022 10:54:26 +0200 Subject: [PATCH] WIP Settings --- src/Settings.res | 171 +++++++++++++++++++++++++++++------------------ 1 file changed, 107 insertions(+), 64 deletions(-) diff --git a/src/Settings.res b/src/Settings.res index 3a0d68e..d8f706f 100644 --- a/src/Settings.res +++ b/src/Settings.res @@ -1,116 +1,159 @@ @react.component let make = () => { - module Form = { - type proto = HTTP | HTTPS; - type urlStyle = VHOST | PATH; + module Proto = { + type t = HTTP | HTTPS + let default = HTTPS - type t = { - protocol: option, - endpoint: option, - url: option, - region: option, - keyId: option, - secretKey: option, + let make = str => switch(str) { + | "http" => HTTP + | _ => default + } + let make2 = mayStr => switch(mayStr) { + | Some(str) => make(str) + | None => default } - type e = - | UndefinedParameter - + let toStr = p => switch(p) { + | HTTP => "http" + | HTTPS => "https" + } + let isTls = p => p == HTTPS + } - let make = () => { protocol: None, endpoint: None, url: None, region: None, keyId: None, secretKey: None } + module UrlStyle = { + type t = VHOST | PATH + let default = PATH - let _build_unsafe = f => { - open Belt.Option + let make = str => switch(str) { + | "vhost" => VHOST + | _ => default + } + let make2 = mayStr => switch(mayStr) { + | Some(str) => make(str) + | None => default + } + let toStr = u => switch(u) { + | PATH => "path" + | VHOST => "vhost" + } + let isPathStyle = u => u == PATH + } + + module LocalKV = { + open Dom + let get = (key) => Storage2.localStorage -> Storage2.getItem(key) + let set = (key, value) => Storage2.localStorage -> Storage2.setItem(key, value) + } + + module Config = { + open Belt + + type t = { + protocol: Proto.t, + url: UrlStyle.t, + endpoint: string, + region: string, + keyId: string, + secretKey: string, + } + + let make = () => { + protocol: "conf_protocol" -> LocalKV.get -> Proto.make2, + url: "conf_url" -> LocalKV.get -> UrlStyle.make2, + endpoint: "conf_endpoint" -> LocalKV.get -> Option.getWithDefault("garage.deuxfleurs.fr"), + region: "conf_region" -> LocalKV.get -> Option.getWithDefault("garage"), + keyId: "conf_keyid" -> LocalKV.get -> Option.getWithDefault(""), + secretKey: "conf_secretkey" -> LocalKV.get -> Option.getWithDefault(""), + } + + let build = c => { { S3.endpoint: Some({ - protocol: switch(f.protocol -> getExn) { | HTTP => "http" | _ => "https" }, - hostname: f.endpoint -> getExn, + protocol: c.protocol -> Proto.toStr, + hostname: c.endpoint, path: "/" }), credentials: Some({ - accessKeyId: f.keyId -> getExn, - secretAccessKey: f.secretKey -> getExn, + accessKeyId: c.keyId, + secretAccessKey: c.secretKey, }), - tls: Some((f.protocol -> getExn) == HTTPS), - forcePathStyle: Some((f.url -> getExn) == PATH), - region: Some(f.region -> getExn), - } - } - - let build: t => Belt.Result.t = f => { - switch(f -> _build_unsafe) { - | config => Ok(config) - | exception Not_found => Error(UndefinedParameter) + tls: Some(c.protocol -> Proto.isTls), + forcePathStyle: Some(c.url -> UrlStyle.isPathStyle), + region: Some(c.region), } } } - let (form, updateForm) = React.useState(_ => Form.make()); + let (config, updateConfig) = React.useState(_ => Config.make()); let login = _ => { - Js.log(form -> Form.build) + Js.log(config -> Config.build) } let setProtocol = evt => { - let value = switch (ReactEvent.Form.target(evt)["value"]) { - | "http" => Form.HTTP - | _ => Form.HTTPS - } - updateForm(form => {...form, protocol: Some(value)}) + let value = ReactEvent.Form.target(evt)["value"] -> Proto.make + updateConfig(c => {...c, protocol: value}) } let setEndpoint = evt => { let value = (ReactEvent.Form.target(evt)["value"]) - updateForm(form => {...form, endpoint: Some(value) }) + updateConfig(c => {...c, endpoint: value }) } - let setRegion = _ => () + let setRegion = evt => { + let value = (ReactEvent.Form.target(evt)["value"]) + updateConfig(c => {...c, region: value }) + } let setUrlStyle = evt => { - let value = switch (ReactEvent.Form.target(evt)["value"]) { - | "path" => Form.PATH - | _ => Form.VHOST - } - updateForm(form => {...form, url: Some(value)}) + let value = ReactEvent.Form.target(evt)["value"] -> UrlStyle.make + updateConfig(c => {...c, url: value }) } - let setKeyId = _ => () - let setSecretKey = _ => () + let setKeyId = evt => { + let value = (ReactEvent.Form.target(evt)["value"]) + updateConfig(c => {...c, keyId: value }) + } + + let setSecretKey = evt => { + let value = (ReactEvent.Form.target(evt)["value"]) + updateConfig(c => {...c, secretKey: value }) + }
{ "Configuration"->React.string }
{ `Réseau` -> React.string } -
- Proto.toStr}> - -
-
- - -
+ + +
+ + +
{ "Authentification"->React.string } -
- -
-
- -
+
+ +
+
+ +
- +
}