|
|
|
@ -49,6 +49,12 @@ export const createUiStore = () =>
|
|
|
|
|
export type UiStore = ReturnType<typeof createUiStore>;
|
|
|
|
|
export const UiStoreContext = createContext<UiStore>();
|
|
|
|
|
|
|
|
|
|
export const ADDRESS_LAYOUT_LEFT = "LEFT";
|
|
|
|
|
export const ADDRESS_LAYOUT_RIGHT = "RIGHT";
|
|
|
|
|
export type AddressLayout =
|
|
|
|
|
| typeof ADDRESS_LAYOUT_LEFT
|
|
|
|
|
| typeof ADDRESS_LAYOUT_RIGHT;
|
|
|
|
|
|
|
|
|
|
export const storeSchema = z.object({
|
|
|
|
|
version: z.number(),
|
|
|
|
|
project: z.object({
|
|
|
|
@ -80,9 +86,15 @@ export const storeSchema = z.object({
|
|
|
|
|
});
|
|
|
|
|
export type StoreObject = Infer<typeof storeSchema>;
|
|
|
|
|
|
|
|
|
|
export const CURRENT_VERSION = 1.3;
|
|
|
|
|
|
|
|
|
|
export const migrateState = (state: Record<string, any>) => {
|
|
|
|
|
state.version = CURRENT_VERSION;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const createStore = () =>
|
|
|
|
|
createStore_<StoreObject>({
|
|
|
|
|
version: 1,
|
|
|
|
|
version: CURRENT_VERSION,
|
|
|
|
|
project: {
|
|
|
|
|
orderNumber: "",
|
|
|
|
|
projectNumber: "",
|
|
|
|
@ -120,6 +132,7 @@ export const localStoreSchema = z.object({
|
|
|
|
|
paymentTerms: z.string().optional(),
|
|
|
|
|
iban: z.string(),
|
|
|
|
|
creditor: addressSchema,
|
|
|
|
|
addressLayout: z.literals(ADDRESS_LAYOUT_LEFT, ADDRESS_LAYOUT_RIGHT),
|
|
|
|
|
customAddress: addressSchema,
|
|
|
|
|
contact: z.object({
|
|
|
|
|
name: z.string(),
|
|
|
|
@ -140,15 +153,31 @@ export const localStoreSchema = z.object({
|
|
|
|
|
});
|
|
|
|
|
export type LocalStoreObject = Infer<typeof localStoreSchema>;
|
|
|
|
|
|
|
|
|
|
export const migrateLocalState = (state: Record<string, any>) => {
|
|
|
|
|
if (state.version === 1) {
|
|
|
|
|
state.addressLayout = ADDRESS_LAYOUT_LEFT;
|
|
|
|
|
|
|
|
|
|
state.version = 1.3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.version = CURRENT_VERSION;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const migrateInfoLog = (oldVersion: number, newVersion: number) =>
|
|
|
|
|
console.info(
|
|
|
|
|
`Migrated document schema from version ${oldVersion} to ${newVersion}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const createLocalStore = () =>
|
|
|
|
|
createLocalStore_<LocalStoreObject>(
|
|
|
|
|
{
|
|
|
|
|
version: 1,
|
|
|
|
|
version: CURRENT_VERSION,
|
|
|
|
|
showWelcome: true,
|
|
|
|
|
vatNumber: "",
|
|
|
|
|
vatRate: 0.0,
|
|
|
|
|
paymentTerms: undefined,
|
|
|
|
|
creditor: createAddress(),
|
|
|
|
|
addressLayout: ADDRESS_LAYOUT_RIGHT,
|
|
|
|
|
customAddress: createAddress(),
|
|
|
|
|
contact: {
|
|
|
|
|
name: "",
|
|
|
|
@ -160,7 +189,24 @@ export const createLocalStore = () =>
|
|
|
|
|
useCustomAddress: false,
|
|
|
|
|
iban: "",
|
|
|
|
|
},
|
|
|
|
|
{ prefix: "invoice-app" }
|
|
|
|
|
{
|
|
|
|
|
prefix: "invoice-app",
|
|
|
|
|
migrate: (getState) => {
|
|
|
|
|
const version = JSON.parse(
|
|
|
|
|
localStorage.getItem("invoice-app-version") || "-1"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (version == -1) return false;
|
|
|
|
|
|
|
|
|
|
if (version !== CURRENT_VERSION) {
|
|
|
|
|
migrateLocalState(getState());
|
|
|
|
|
migrateInfoLog(version, CURRENT_VERSION);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type LocalStore = ReturnType<typeof createLocalStore>;
|
|
|
|
|