@etsoo/react
Version:
TypeScript ReactJs UI Independent Framework
47 lines (46 loc) • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CultureState = void 0;
const State_1 = require("./State");
// Calls
const calls = {
/**
* Key value
* @param key Item key
*/
get(key) {
const resources = this.state.resources;
const value = typeof resources === "object" ? resources[key] : undefined;
if (value == null)
return undefined;
return value;
}
};
/**
* Culture state
* Creator for culture context and provider globally, not inside a component to avoid problem:
* Cannot update a component (`provider`) while rendering a different component
*/
class CultureState {
/**
* Constructor
*/
constructor(item) {
// Default
const defaultItem = item ?? {};
// Load resources
if (item != null && typeof item.resources !== "object")
item.resources().then((result) => (item.resources = result));
// Act
const { context, provider } = State_1.State.create((state, action) => {
// Language reducer
if (state.name !== action.name) {
return { ...action };
}
return state;
}, defaultItem, calls);
this.context = context;
this.provider = provider;
}
}
exports.CultureState = CultureState;