Fallback pour localStorage et config simplifiée de l'adresse publique de l'app client
This commit is contained in:
parent
47020f7beb
commit
e0b59de77c
|
@ -3458,6 +3458,21 @@
|
||||||
"json5": "^2.1.2"
|
"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": {
|
"locate-path": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
"bulma": "^0.8.2",
|
"bulma": "^0.8.2",
|
||||||
"bulma-switch": "^2.0.0",
|
"bulma-switch": "^2.0.0",
|
||||||
"json-merge-patch": "^0.2.3",
|
"json-merge-patch": "^0.2.3",
|
||||||
|
"local-storage-fallback": "^4.1.1",
|
||||||
"preact": "^10.4.1",
|
"preact": "^10.4.1",
|
||||||
"preact-markup": "^1.6.0",
|
"preact-markup": "^1.6.0",
|
||||||
"preact-render-to-string": "^5.1.6",
|
"preact-render-to-string": "^5.1.6",
|
||||||
|
|
|
@ -1,12 +1,20 @@
|
||||||
import { useState } from "preact/hooks";
|
import { useState } from "preact/hooks";
|
||||||
|
import storage from 'local-storage-fallback'
|
||||||
|
|
||||||
export function useLocalStorage<T>(key: string, initialValue: T) {
|
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
|
// State to store our value
|
||||||
// Pass initial state function to useState so logic is only executed once
|
// Pass initial state function to useState so logic is only executed once
|
||||||
const [storedValue, setStoredValue] = useState(() => {
|
const [storedValue, setStoredValue] = useState(() => {
|
||||||
try {
|
try {
|
||||||
// Get from local storage by key
|
// 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
|
// Parse stored json or if none return initialValue
|
||||||
return item ? JSON.parse(item) : initialValue;
|
return item ? JSON.parse(item) : initialValue;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -26,7 +34,7 @@ export function useLocalStorage<T>(key: string, initialValue: T) {
|
||||||
// Save state
|
// Save state
|
||||||
setStoredValue(valueToStore);
|
setStoredValue(valueToStore);
|
||||||
// Save to local storage
|
// Save to local storage
|
||||||
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
localStorage.setItem(key, JSON.stringify(valueToStore));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// A more advanced implementation would handle the error case
|
// A more advanced implementation would handle the error case
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
@ -19,15 +19,13 @@ type Config struct {
|
||||||
|
|
||||||
// HTTPConfig is the configuration part which defines HTTP related params
|
// HTTPConfig is the configuration part which defines HTTP related params
|
||||||
type HTTPConfig struct {
|
type HTTPConfig struct {
|
||||||
BaseURL string `yaml:"baseurl" env:"GUESSTIMATE_BASE_URL"`
|
|
||||||
Address string `yaml:"address" env:"GUESSTIMATE_HTTP_ADDRESS"`
|
Address string `yaml:"address" env:"GUESSTIMATE_HTTP_ADDRESS"`
|
||||||
PublicDir string `yaml:"publicDir" env:"GUESSTIMATE_PUBLIC_DIR"`
|
PublicDir string `yaml:"publicDir" env:"GUESSTIMATE_PUBLIC_DIR"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientConfig is the configuration part which defines the client app related params
|
// ClientConfig is the configuration part which defines the client app related params
|
||||||
type ClientConfig struct {
|
type ClientConfig struct {
|
||||||
BaseURL string `yaml:"baseurl" env:"GUESSTIMATE_CLIENT_BASE_URL"`
|
PublicBaseURL string `yaml:"publicbaseurl" env:"GUESSTIMATE_CLIENT_PUBLIC_BASE_URL"`
|
||||||
Address string `yaml:"address" env:"GUESSTIMATE_CLIENT_HTTP_ADDRESS"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DataConfig is the configuration part which defines data related params
|
// DataConfig is the configuration part which defines data related params
|
||||||
|
@ -70,13 +68,11 @@ func NewDumpDefault() *Config {
|
||||||
func NewDefault() *Config {
|
func NewDefault() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
HTTP: HTTPConfig{
|
HTTP: HTTPConfig{
|
||||||
BaseURL: "localhost",
|
|
||||||
Address: ":8081",
|
Address: ":8081",
|
||||||
PublicDir: "public",
|
PublicDir: "public",
|
||||||
},
|
},
|
||||||
Client: ClientConfig{
|
Client: ClientConfig{
|
||||||
BaseURL: "localhost",
|
PublicBaseURL: "http://localhost:8080",
|
||||||
Address: ":8080",
|
|
||||||
},
|
},
|
||||||
Data: DataConfig{
|
Data: DataConfig{
|
||||||
Path: "guesstimate.db",
|
Path: "guesstimate.db",
|
||||||
|
|
|
@ -88,8 +88,7 @@ func handleExportProject(w http.ResponseWriter, r *http.Request) {
|
||||||
url string
|
url string
|
||||||
)
|
)
|
||||||
|
|
||||||
url = "http://" + string(cfg.Client.BaseURL) + string(cfg.Client.Address) + "/pdf/" + string(projectID)
|
url = string(cfg.Client.PublicBaseURL) + "/pdf/" + string(projectID)
|
||||||
fmt.Println(url)
|
|
||||||
|
|
||||||
// Create new PDF generator
|
// Create new PDF generator
|
||||||
pdfg, err := wkhtmltopdf.NewPDFGenerator()
|
pdfg, err := wkhtmltopdf.NewPDFGenerator()
|
||||||
|
|
Loading…
Reference in New Issue