From 78f9743fc5fed266a3d06c3a6f2a540c0f72b64c Mon Sep 17 00:00:00 2001 From: Chika Date: Sun, 8 May 2022 13:30:07 +0300 Subject: [PATCH] types: organize store types --- pages/index.tsx | 15 ++++++++------- stores/calculation/options/index.ts | 9 ++------- stores/calculation/options/types.ts | 8 ++++++++ stores/calculation/statuses/index.ts | 7 ++----- stores/calculation/statuses/types.ts | 5 +++++ stores/calculation/values/index.ts | 15 ++++++++------- stores/calculation/values/types.ts | 2 ++ stores/index.js | 6 ++---- types/page.ts | 15 +++++++++++++++ 9 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 stores/calculation/statuses/types.ts create mode 100644 types/page.ts diff --git a/pages/index.tsx b/pages/index.tsx index 10c1033..3b0dea5 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -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 = () => { return
Home
; @@ -22,7 +18,12 @@ export const getServerSideProps: GetServerSideProps = async ctx => { const apolloClient = initializeApollo(); - return { props: { user, initialApolloState: apolloClient.cache.extract() } }; + return { + props: { + user, + initialApolloState: apolloClient.cache.extract(), + }, + }; }; export default Home; diff --git a/stores/calculation/options/index.ts b/stores/calculation/options/index.ts index cf3e387..2ed3f15 100644 --- a/stores/calculation/options/index.ts +++ b/stores/calculation/options/index.ts @@ -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; -type Filter = (options: Options) => Options; -type OptionsFilters = Record; - export class OptionsStore { root: RootStore; #options: Partial = {}; diff --git a/stores/calculation/options/types.ts b/stores/calculation/options/types.ts index b60366f..59c60f5 100644 --- a/stores/calculation/options/types.ts +++ b/stores/calculation/options/types.ts @@ -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; + +export type Filter = (options: Options) => Options; +export type OptionsFilters = Record; diff --git a/stores/calculation/statuses/index.ts b/stores/calculation/statuses/index.ts index f0ae7b1..c825bb7 100644 --- a/stores/calculation/statuses/index.ts +++ b/stores/calculation/statuses/index.ts @@ -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>; +import type { CalculationStatuses, Status } from './types'; export class StatusStore { root: RootStore; diff --git a/stores/calculation/statuses/types.ts b/stores/calculation/statuses/types.ts new file mode 100644 index 0000000..45538fa --- /dev/null +++ b/stores/calculation/statuses/types.ts @@ -0,0 +1,5 @@ +import type { Elements } from 'Components/Calculation/types/elements'; + +export type Status = 'Default' | 'Disabled' | 'Loading' | 'Hidden'; + +export type CalculationStatuses = Partial>; diff --git a/stores/calculation/values/index.ts b/stores/calculation/values/index.ts index e8402cf..03d679c 100644 --- a/stores/calculation/values/index.ts +++ b/stores/calculation/values/index.ts @@ -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 = {}; + #values: CalculationValues = {}; constructor(rootStore: RootStore) { makeAutoObservable(this); this.root = rootStore; } - hydrate = (initialValues: Partial) => { + 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, typeof valuesNames[number]>); + }, {} as Pick); } setValue(valueName: V, value: ValuesTypes[V]) { @@ -45,7 +46,7 @@ export class ValuesStore { this.setValue(valueName, value); } - setValues(values: Partial, settings: { replace?: boolean }) { + setValues(values: CalculationValues, settings: { replace?: boolean }) { if (settings?.replace) this.#values = values; this.#values = Object.assign(this.#values, values); } diff --git a/stores/calculation/values/types.ts b/stores/calculation/values/types.ts index 5a8fec5..27f01e1 100644 --- a/stores/calculation/values/types.ts +++ b/stores/calculation/values/types.ts @@ -256,4 +256,6 @@ export type ValuesTypes = { [Key in keyof Types]: Types[Key] | null; }; +export type CalculationValues = Partial; + export type Values = keyof ValuesTypes; diff --git a/stores/index.js b/stores/index.js index 3e042e9..23f77b0 100644 --- a/stores/index.js +++ b/stores/index.js @@ -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); diff --git a/types/page.ts b/types/page.ts new file mode 100644 index 0000000..f4cf353 --- /dev/null +++ b/types/page.ts @@ -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; + }; +}