Skip to content

Commit

Permalink
store types refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 5, 2024
1 parent 9eaf316 commit 4d11ced
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 17 deletions.
5 changes: 2 additions & 3 deletions src/store/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { StateCreator } from 'zustand';
import { createBaseSlice, SliceState } from './utils/slice';
import { generateStore } from './utils/storeCreator';
import { generateStore, type StateCreatorTyped } from './utils/storeCreator';

export type RecordState = RecordEntry | null;
export type SelectedRecordBlocksState = SelectedRecordBlocks | undefined;
Expand All @@ -14,7 +13,7 @@ export type InputsState = SliceState<'userValues', UserValues> &

const STORE_NAME = 'Inputs';

const inputsStore: StateCreator<InputsState, [['zustand/devtools', never]], []> = (...args) => ({
const inputsStore: StateCreatorTyped<InputsState> = (...args) => ({
...createBaseSlice({ basic: 'userValues' }, {} as UserValues, true)(...args),
...createBaseSlice({ basic: 'previewContent' }, [] as PreviewContent[])(...args),
...createBaseSlice({ basic: 'record' }, null as RecordState)(...args),
Expand Down
5 changes: 2 additions & 3 deletions src/store/loadingState.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { StateCreator } from 'zustand';
import { createBaseSlice, SliceState } from './utils/slice';
import { generateStore } from './utils/storeCreator';
import { generateStore, type StateCreatorTyped } from './utils/storeCreator';

export type LoadingState = SliceState<'isLoading', boolean>;

const STORE_NAME = 'Loading';

const loadingStateStore: StateCreator<LoadingState, [['zustand/devtools', never]], []> = (...args) => ({
const loadingStateStore: StateCreatorTyped<LoadingState> = (...args) => ({
...createBaseSlice({ basic: 'isLoading' }, false)(...args),
});

Expand Down
5 changes: 2 additions & 3 deletions src/store/marcPreview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { StateCreator } from 'zustand';
import { createBaseSlice, SliceState } from './utils/slice';
import { generateStore } from './utils/storeCreator';
import { generateStore, type StateCreatorTyped } from './utils/storeCreator';

type MarcPreviewData = MarcDTO | null;
type MarcPreviewMetaData = MarcPreviewMetadata | null;
Expand All @@ -11,7 +10,7 @@ export type MarcPreviewState = SliceState<'basicValue', any> &

const STORE_NAME = 'MarcPreview';

const marcPreviewStore: StateCreator<MarcPreviewState, [['zustand/devtools', never]], []> = (...args) => ({
const marcPreviewStore: StateCreatorTyped<MarcPreviewState> = (...args) => ({
...createBaseSlice({ basic: 'basicValue' }, null)(...args),
...createBaseSlice({ basic: 'complexValue' }, null as MarcPreviewData)(...args),
...createBaseSlice({ basic: 'metaData' }, null as MarcPreviewMetaData)(...args),
Expand Down
5 changes: 2 additions & 3 deletions src/store/profile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { StateCreator } from 'zustand';
import { createBaseSlice, SliceState } from './utils/slice';
import { generateStore } from './utils/storeCreator';
import { generateStore, type StateCreatorTyped } from './utils/storeCreator';

type SelectedProfileType = ProfileEntry | null;
type PreparedFieldsType = ResourceTemplates | null;
Expand All @@ -14,7 +13,7 @@ export type ProfileState = SliceState<'profiles', ProfileEntry[]> &

const STORE_NAME = 'Profile';

const profileStore: StateCreator<ProfileState, [['zustand/devtools', never]], []> = (...args) => ({
const profileStore: StateCreatorTyped<ProfileState> = (...args) => ({
...createBaseSlice({ basic: 'profiles' }, [] as ProfileEntry[])(...args),
...createBaseSlice({ basic: 'selectedProfile' }, null as SelectedProfileType)(...args),
...createBaseSlice({ basic: 'preparedFields' }, null as PreparedFieldsType)(...args),
Expand Down
5 changes: 2 additions & 3 deletions src/store/status.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { StateCreator } from 'zustand';
import { createBaseSlice, SliceState } from './utils/slice';
import { generateStore } from './utils/storeCreator';
import { generateStore, type StateCreatorTyped } from './utils/storeCreator';

type LastSavedRecordId = string | null;

Expand All @@ -11,7 +10,7 @@ export type StatusState = SliceState<'lastSavedRecordId', LastSavedRecordId> &

const STORE_NAME = 'Status';

const statusStore: StateCreator<StatusState, [['zustand/devtools', never]], []> = (...args) => ({
const statusStore: StateCreatorTyped<StatusState> = (...args) => ({
...createBaseSlice({ basic: 'lastSavedRecordId' }, null as LastSavedRecordId)(...args),
...createBaseSlice({ basic: 'isEditedRecord' }, false)(...args),
...createBaseSlice({ basic: 'recordStatus' }, { type: undefined } as RecordStatus)(...args),
Expand Down
6 changes: 4 additions & 2 deletions src/store/utils/storeCreator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { create, StateCreator } from 'zustand';
import { create, type StateCreator } from 'zustand';
import { devtools } from 'zustand/middleware';
import { IS_PROD_MODE } from '@common/constants/bundle.constants';

const STORE_NAME = 'Linked Data Editor';

export const generateStore = <T>(store: StateCreator<T, [['zustand/devtools', never]], []>, name: string) =>
export type StateCreatorTyped<T> = StateCreator<T, [['zustand/devtools', never]], []>;

export const generateStore = <T>(store: StateCreatorTyped<T>, name: string) =>
create<T>()(devtools(store, { name: STORE_NAME, store: name, enabled: !IS_PROD_MODE }));

0 comments on commit 4d11ced

Please sign in to comment.