Fallback pour localStorage et config simplifiée de l'adresse publique de l'app client

This commit is contained in:
Teddy Cornaut 2020-06-16 18:02:28 -04:00
parent 47020f7beb
commit e0b59de77c
5 changed files with 29 additions and 10 deletions

View File

@ -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",

View File

@ -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",

View File

@ -1,12 +1,20 @@
import { useState } from "preact/hooks";
import storage from 'local-storage-fallback'
export function useLocalStorage<T>(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<T>(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);

View File

@ -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",

View File

@ -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()