types: organize store types
This commit is contained in:
parent
85036ff164
commit
78f9743fc5
@ -1,13 +1,9 @@
|
||||
import { NormalizedCacheObject } from '@apollo/client';
|
||||
import { initializeApollo } from 'apollo/client';
|
||||
import type { GetServerSideProps, NextPage } from 'next';
|
||||
import { fetchUser } from 'services/user';
|
||||
import type { User } from 'services/user/types';
|
||||
import type { BasePageProps } from 'types/page';
|
||||
|
||||
interface PageProps {
|
||||
user: User;
|
||||
initialApolloState: NormalizedCacheObject;
|
||||
}
|
||||
interface PageProps extends BasePageProps {}
|
||||
|
||||
const Home: NextPage<PageProps> = () => {
|
||||
return <div>Home</div>;
|
||||
@ -22,7 +18,12 @@ export const getServerSideProps: GetServerSideProps<PageProps> = async ctx => {
|
||||
|
||||
const apolloClient = initializeApollo();
|
||||
|
||||
return { props: { user, initialApolloState: apolloClient.cache.extract() } };
|
||||
return {
|
||||
props: {
|
||||
user,
|
||||
initialApolloState: apolloClient.cache.extract(),
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default Home;
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
import { Elements } from 'Components/Calculation/types/elements';
|
||||
import type { Elements } from 'Components/Calculation/types/elements';
|
||||
import { mergeWith } from 'lodash';
|
||||
import { makeAutoObservable } from 'mobx';
|
||||
import { RootStore } from 'stores/root';
|
||||
import { BaseOption } from './types';
|
||||
import { CalculationOptions, Filter, Options, OptionsFilters } from './types';
|
||||
|
||||
const _EXCLUDE_RESET_ELEMENTS: Elements[] = [
|
||||
'selectTechnicalCard',
|
||||
@ -23,11 +23,6 @@ const _AUTO_SET_VALUE_ELEMENTS: Elements[] = [
|
||||
'selectTownRegistration',
|
||||
];
|
||||
|
||||
type Options = BaseOption[];
|
||||
type CalculationOptions = Record<Elements, Options>;
|
||||
type Filter = (options: Options) => Options;
|
||||
type OptionsFilters = Record<Elements, Filter>;
|
||||
|
||||
export class OptionsStore {
|
||||
root: RootStore;
|
||||
#options: Partial<CalculationOptions> = {};
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
import type { Elements } from 'Components/Calculation/types/elements';
|
||||
|
||||
export type BaseOption = {
|
||||
name: string;
|
||||
value: any;
|
||||
};
|
||||
|
||||
export type Options = BaseOption[];
|
||||
export type CalculationOptions = Record<Elements, Options>;
|
||||
|
||||
export type Filter = (options: Options) => Options;
|
||||
export type OptionsFilters = Record<Elements, Filter>;
|
||||
|
||||
@ -1,10 +1,7 @@
|
||||
import { Elements } from 'Components/Calculation/types/elements';
|
||||
import type { Elements } from 'Components/Calculation/types/elements';
|
||||
import { makeAutoObservable } from 'mobx';
|
||||
import { RootStore } from 'stores/root';
|
||||
|
||||
type Status = 'Default' | 'Disabled' | 'Loading' | 'Hidden';
|
||||
|
||||
export type CalculationStatuses = Partial<Record<Elements, Status>>;
|
||||
import type { CalculationStatuses, Status } from './types';
|
||||
|
||||
export class StatusStore {
|
||||
root: RootStore;
|
||||
|
||||
5
stores/calculation/statuses/types.ts
Normal file
5
stores/calculation/statuses/types.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import type { Elements } from 'Components/Calculation/types/elements';
|
||||
|
||||
export type Status = 'Default' | 'Disabled' | 'Loading' | 'Hidden';
|
||||
|
||||
export type CalculationStatuses = Partial<Record<Elements, Status>>;
|
||||
@ -1,19 +1,20 @@
|
||||
import { ElementsTypes, getValueName } from 'Components/Calculation/config/map';
|
||||
import { Elements } from 'Components/Calculation/types/elements';
|
||||
import type { ElementsTypes } from 'Components/Calculation/config/map';
|
||||
import { getValueName } from 'Components/Calculation/config/map';
|
||||
import type { Elements } from 'Components/Calculation/types/elements';
|
||||
import { makeAutoObservable } from 'mobx';
|
||||
import { RootStore } from '../../root';
|
||||
import { Values, ValuesTypes } from './types';
|
||||
import type { CalculationValues, Values, ValuesTypes } from './types';
|
||||
|
||||
export class ValuesStore {
|
||||
root: RootStore;
|
||||
#values: Partial<ValuesTypes> = {};
|
||||
#values: CalculationValues = {};
|
||||
|
||||
constructor(rootStore: RootStore) {
|
||||
makeAutoObservable(this);
|
||||
this.root = rootStore;
|
||||
}
|
||||
|
||||
hydrate = (initialValues: Partial<ValuesTypes>) => {
|
||||
hydrate = (initialValues: CalculationValues) => {
|
||||
this.#values = initialValues;
|
||||
};
|
||||
|
||||
@ -30,7 +31,7 @@ export class ValuesStore {
|
||||
return valuesNames.reduce((values, valueName) => {
|
||||
values[valueName] = this.getValue(valueName);
|
||||
return values;
|
||||
}, {} as Pick<Partial<ValuesTypes>, typeof valuesNames[number]>);
|
||||
}, {} as Pick<CalculationValues, typeof valuesNames[number]>);
|
||||
}
|
||||
|
||||
setValue<V extends Values>(valueName: V, value: ValuesTypes[V]) {
|
||||
@ -45,7 +46,7 @@ export class ValuesStore {
|
||||
this.setValue(valueName, value);
|
||||
}
|
||||
|
||||
setValues(values: Partial<ValuesTypes>, settings: { replace?: boolean }) {
|
||||
setValues(values: CalculationValues, settings: { replace?: boolean }) {
|
||||
if (settings?.replace) this.#values = values;
|
||||
this.#values = Object.assign(this.#values, values);
|
||||
}
|
||||
|
||||
@ -256,4 +256,6 @@ export type ValuesTypes = {
|
||||
[Key in keyof Types]: Types[Key] | null;
|
||||
};
|
||||
|
||||
export type CalculationValues = Partial<ValuesTypes>;
|
||||
|
||||
export type Values = keyof ValuesTypes;
|
||||
|
||||
@ -9,10 +9,8 @@ export function initializeStore(initialData) {
|
||||
const _store = store ?? new RootStore();
|
||||
|
||||
if (initialData) {
|
||||
const {
|
||||
user = null,
|
||||
calculation: { values = {}, statuses = {}, options = {} },
|
||||
} = initialData;
|
||||
const { user = null, calculation = {} } = initialData;
|
||||
const { values = {}, statuses = {}, options = {} } = calculation;
|
||||
|
||||
_store.$user.hydrate(user);
|
||||
_store.$calculation.$values.hydrate(values);
|
||||
|
||||
15
types/page.ts
Normal file
15
types/page.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import type { NormalizedCacheObject } from '@apollo/client';
|
||||
import type { User } from 'services/user/types';
|
||||
import type { CalculationOptions } from 'stores/calculation/options/types';
|
||||
import type { CalculationStatuses } from 'stores/calculation/statuses/types';
|
||||
import type { CalculationValues } from 'stores/calculation/values/types';
|
||||
|
||||
export interface BasePageProps {
|
||||
user: User;
|
||||
initialApolloState: NormalizedCacheObject;
|
||||
calculation?: {
|
||||
values: CalculationValues;
|
||||
statuses?: CalculationStatuses;
|
||||
options?: CalculationOptions;
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user