From e0b59de77c7b0ab866b27793150cebcdd4f2a441 Mon Sep 17 00:00:00 2001 From: Teddy Cornaut Date: Tue, 16 Jun 2020 18:02:28 -0400 Subject: [PATCH] =?UTF-8?q?Fallback=20pour=20localStorage=20et=20config=20?= =?UTF-8?q?simplifi=C3=A9e=20de=20l'adresse=20publique=20de=20l'app=20clie?= =?UTF-8?q?nt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/package-lock.json | 15 +++++++++++++++ client/package.json | 1 + client/src/hooks/use-local-storage.ts | 12 ++++++++++-- server/internal/config/config.go | 8 ++------ server/internal/route/project.go | 3 +-- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index 1d8d1ea..ea2dfce 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -3458,6 +3458,21 @@ "json5": "^2.1.2" } }, + "local-storage-fallback": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/local-storage-fallback/-/local-storage-fallback-4.1.1.tgz", + "integrity": "sha512-Ka4rem1FOgvIaPMokxXTP8DgW8gjfAQSdJC8TGrplDug7vh3b0PZnY/9euG3O3cIGAvJLp33mMU6MJ13x6S+Pw==", + "requires": { + "cookie": "^0.3.1" + }, + "dependencies": { + "cookie": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", + "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" + } + } + }, "locate-path": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", diff --git a/client/package.json b/client/package.json index d5b1421..262fbb4 100644 --- a/client/package.json +++ b/client/package.json @@ -10,6 +10,7 @@ "bulma": "^0.8.2", "bulma-switch": "^2.0.0", "json-merge-patch": "^0.2.3", + "local-storage-fallback": "^4.1.1", "preact": "^10.4.1", "preact-markup": "^1.6.0", "preact-render-to-string": "^5.1.6", diff --git a/client/src/hooks/use-local-storage.ts b/client/src/hooks/use-local-storage.ts index 10d86ba..53ae844 100644 --- a/client/src/hooks/use-local-storage.ts +++ b/client/src/hooks/use-local-storage.ts @@ -1,12 +1,20 @@ import { useState } from "preact/hooks"; +import storage from 'local-storage-fallback' export function useLocalStorage(key: string, initialValue: T) { + //Define storage to localStorage and fallback if not avalaible (in wkhtmltopdf for exemple) + let localStorage: any; + localStorage = window.localStorage + if (null === localStorage) { + localStorage = storage; + } + // State to store our value // Pass initial state function to useState so logic is only executed once const [storedValue, setStoredValue] = useState(() => { try { // Get from local storage by key - const item = window.localStorage.getItem(key); + const item = localStorage.getItem(key); // Parse stored json or if none return initialValue return item ? JSON.parse(item) : initialValue; } catch (error) { @@ -26,7 +34,7 @@ export function useLocalStorage(key: string, initialValue: T) { // Save state setStoredValue(valueToStore); // Save to local storage - window.localStorage.setItem(key, JSON.stringify(valueToStore)); + localStorage.setItem(key, JSON.stringify(valueToStore)); } catch (error) { // A more advanced implementation would handle the error case console.error(error); diff --git a/server/internal/config/config.go b/server/internal/config/config.go index 65caa51..309f51d 100644 --- a/server/internal/config/config.go +++ b/server/internal/config/config.go @@ -19,15 +19,13 @@ type Config struct { // HTTPConfig is the configuration part which defines HTTP related params type HTTPConfig struct { - BaseURL string `yaml:"baseurl" env:"GUESSTIMATE_BASE_URL"` Address string `yaml:"address" env:"GUESSTIMATE_HTTP_ADDRESS"` PublicDir string `yaml:"publicDir" env:"GUESSTIMATE_PUBLIC_DIR"` } // ClientConfig is the configuration part which defines the client app related params type ClientConfig struct { - BaseURL string `yaml:"baseurl" env:"GUESSTIMATE_CLIENT_BASE_URL"` - Address string `yaml:"address" env:"GUESSTIMATE_CLIENT_HTTP_ADDRESS"` + PublicBaseURL string `yaml:"publicbaseurl" env:"GUESSTIMATE_CLIENT_PUBLIC_BASE_URL"` } // DataConfig is the configuration part which defines data related params @@ -70,13 +68,11 @@ func NewDumpDefault() *Config { func NewDefault() *Config { return &Config{ HTTP: HTTPConfig{ - BaseURL: "localhost", Address: ":8081", PublicDir: "public", }, Client: ClientConfig{ - BaseURL: "localhost", - Address: ":8080", + PublicBaseURL: "http://localhost:8080", }, Data: DataConfig{ Path: "guesstimate.db", diff --git a/server/internal/route/project.go b/server/internal/route/project.go index c9929fb..b7e97f4 100644 --- a/server/internal/route/project.go +++ b/server/internal/route/project.go @@ -88,8 +88,7 @@ func handleExportProject(w http.ResponseWriter, r *http.Request) { url string ) - url = "http://" + string(cfg.Client.BaseURL) + string(cfg.Client.Address) + "/pdf/" + string(projectID) - fmt.Println(url) + url = string(cfg.Client.PublicBaseURL) + "/pdf/" + string(projectID) // Create new PDF generator pdfg, err := wkhtmltopdf.NewPDFGenerator()