Skip to content

Commit

Permalink
update slices creator
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Nov 29, 2024
1 parent bf45ca1 commit 4b8c1cc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/store/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const useStatusStoreBase = create<StatusState>()(
'statusMessages',
[] as StatusEntry[],
STORE_NAME,
true
)(...args),
})),
);
79 changes: 49 additions & 30 deletions src/store/utils/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,71 @@ export type SliceState<K extends string, V, T = V> = {
[P in `set${Capitalize<K>}`]: (value: V) => void;
} & {
[P in `reset${Capitalize<K>}`]: () => void;
} & {
[P in `add${Capitalize<K>}`]: (value: T) => void;
} & Partial<{
[P in `add${Capitalize<K>}`]: (value: T) => void;
}>;

const capitalize = (value: string) => {
return value.charAt(0).toUpperCase() + value.slice(1);
};

const updateValue = <V, T>(value: V, updatedValue: T): V => {
if (Array.isArray(value)) {
return [...value, updatedValue] as any;
} else if (value instanceof Map) {
const newMap = new Map(value);

if (updatedValue instanceof Map) {
updatedValue.forEach((value, key) => newMap.set(key, value));
} else if (typeof updatedValue === 'object' && updatedValue !== null) {
Object.entries(updatedValue).forEach(([k, v]) => newMap.set(k, v));
}

return newMap as any;
} else if (typeof value === 'object' && value !== null) {
if (typeof updatedValue === 'object' && updatedValue !== null) {
return { ...value, ...updatedValue } as any;
}

return updatedValue as any;
} else if (value instanceof Set) {
return new Set([...value, updatedValue]) as any;
} else if (typeof value === 'string') {
return `${value}${updatedValue}` as any;
}

return updatedValue as any;
};

export const createBaseSlice = <K extends string, V, T = V>(
valueTitle: K,
initialValue: V,
sliceTitle: string,
canAddSingleItem = false,
): StateCreator<SliceState<K, V, T>, [['zustand/devtools', never]], [], SliceState<K, V, T>> => {
return set => {
const capitalizedTitle = capitalize(valueTitle);

return {
const baseSlice = {
[valueTitle]: initialValue,
[`set${capitalizedTitle}`]: (updatedValue: V) =>
set({ [valueTitle]: updatedValue } as any, undefined, `${sliceTitle}/set${capitalizedTitle}`),
set({ [valueTitle]: updatedValue } as any, false, `${sliceTitle}/set${capitalizedTitle}`),
[`reset${capitalizedTitle}`]: () =>
set({ [valueTitle]: initialValue } as any, undefined, `${sliceTitle}/set${capitalizedTitle}`),
[`add${capitalizedTitle}`]: (updatedValue: T) =>
set({ [valueTitle]: initialValue } as any, false, `${sliceTitle}/reset${capitalizedTitle}`),
} as SliceState<K, V, T>;

if (canAddSingleItem) {
(baseSlice as any)[`add${capitalizedTitle}`] = (updatedValue: T) =>
set(
state => {
const value = state[valueTitle] as any;

if (Array.isArray(value)) {
return { [valueTitle]: [...value, updatedValue] } as any;
} else if (typeof value === 'object' && value !== null) {
if (typeof updatedValue === 'object' && updatedValue !== null) {
return { [valueTitle]: { ...value, ...updatedValue } } as any;
}

return { [valueTitle]: updatedValue } as any;
} else if (value instanceof Set) {
return { [valueTitle]: new Set([...value, updatedValue]) } as any;
} else if (typeof value === 'string') {
return { [valueTitle]: `${value}${updatedValue}` } as any;
}

return { [valueTitle]: updatedValue } as any;
return { [valueTitle]: updateValue(value, updatedValue) } as any;
},
undefined,
`${sliceTitle}/set${capitalizedTitle}`,
),
} as SliceState<K, V, T>;
};
};
false,
`${sliceTitle}/add${capitalizedTitle}`,
);
}

const capitalize = (value: string) => {
return value.charAt(0).toUpperCase() + value.slice(1);
return baseSlice;
};
};

0 comments on commit 4b8c1cc

Please sign in to comment.