chore: add src

This commit is contained in:
Jesús Pérez Lorenzo 2021-10-14 15:42:52 +01:00
parent d3e880d1d4
commit 49e9edae14
65 changed files with 3855 additions and 0 deletions

61
src/store/index.ts Normal file
View file

@ -0,0 +1,61 @@
import { InjectionKey } from 'vue'
import {
createStore,
createLogger,
Store as VuexStore,
useStore as baseUseStore,
} from 'vuex'
import {
app_data,
AppDataState,
app_check,
AppCheckState,
app_defs,
AppDefsState,
app_profile,
AppProfileState,
AppLangState,
app_lang,
} from './modules'
import { Store as AppDataStore } from './modules/app/data'
import { Store as AppCheckStore } from './modules/app/check'
import { Store as AppDefsStore } from './modules/app/defs'
import { Store as AppProfileStore } from './modules/app/profile'
import { Store as AppLangStore } from './modules/app/lang'
export interface RootState {
data: AppDataState
check: AppCheckState
defs: AppDefsState
profile: AppProfileState
lang: AppLangState
}
export type RootStore = AppDataStore<Pick<RootState, 'data'>>
& AppCheckStore<Pick<RootState, 'check'>>
& AppDefsStore<Pick<RootState, 'defs'>>
& AppProfileStore<Pick<RootState, 'profile'>>
& AppLangStore<Pick<RootState, 'lang'>>
// eslint-disable-next-line symbol-description
export const key: InjectionKey<VuexStore<RootState>> = Symbol()
const debug = process.env.NODE_ENV !== 'production'
export default createStore<RootState>({
modules: {
app_data,
app_check,
app_defs,
app_profile,
app_lang,
},
strict: debug,
plugins: debug ? [createLogger()] : [],
})
// export const useStore = (): RootStore => {
// return baseUseStore(key)
// }

View file

@ -0,0 +1,44 @@
import { ActionContext, ActionTree } from 'vuex'
import { Mutations, MutationTypes } from './mutations'
import { State } from './index'
import { RootState } from '~/store/index'
export enum ActionTypes {
addCheck = 'addCheck',
removeCheck = 'removeCheck',
editCheck = 'editCheck'
}
type AppCheckActionContext = {
commit<K extends keyof Mutations>(
key: K,
payload: Parameters<Mutations[K]>[1]
): ReturnType<Mutations[K]>
} & Omit<ActionContext<State, RootState>, 'commit'>
export interface Actions {
[ActionTypes.addCheck](
{ commit }: AppCheckActionContext,
payload: any
): void
[ActionTypes.removeCheck](
{ commit }: AppCheckActionContext,
payload: any
): void
[ActionTypes.editCheck](
{ commit }: AppCheckActionContext,
payload: any
): void
}
export const actions: ActionTree<State, RootState> & Actions = {
[ActionTypes.addCheck]({ commit }, payload) {
commit(MutationTypes.addCheck, payload)
},
[ActionTypes.removeCheck]({ commit }, payload) {
commit(MutationTypes.removeCheck, payload)
},
[ActionTypes.editCheck]({ commit }, payload) {
commit(MutationTypes.editCheck, payload)
},
}

View file

@ -0,0 +1,13 @@
import { GetterTree } from 'vuex'
import { State } from './index'
import { RootState } from '~/store/index'
export type Getters<S = State> = {
check(state: S, key: string): any
}
export const getters: GetterTree<State, RootState> & Getters = {
check: (state, key: string) => {
return state.check.get(key)
},
}

View file

@ -0,0 +1,49 @@
import {
Store as VuexStore,
Module,
CommitOptions,
DispatchOptions,
} from 'vuex'
import { getters, Getters } from './getters'
import { mutations, Mutations, MutationTypes } from './mutations'
import { actions, Actions, ActionTypes } from './actions'
import { RootState } from '~/store/index'
interface State {
check: Map<string, any>
}
const state: State = {
check: new Map(),
}
const data_module: Module<State, RootState> = {
state,
mutations,
actions,
getters,
}
export default data_module
type Store<S = State> = Omit<VuexStore<S>, 'commit' | 'getters' | 'dispatch' > & {
commit<K extends keyof Mutations, P extends Parameters<Mutations[K]>[1]>(
key: K,
payload: P,
options?: CommitOptions
): ReturnType<Mutations[K]>
} & {
getters: {
[K in keyof Getters]: ReturnType<Getters[K]>
}
} & {
dispatch<K extends keyof Actions>(
key: K,
payload: Parameters<Actions[K]>[1],
options?: DispatchOptions
): ReturnType<Actions[K]>
}
export { State, ActionTypes, MutationTypes, Store }

View file

@ -0,0 +1,48 @@
import { MutationTree } from 'vuex'
import { State } from './index'
import { AppCheck } from '~/store/types'
export enum MutationTypes {
addCheck = 'addCheck',
removeCheck = 'removeCheck',
editCheck = 'editCheck'
}
interface CheckMap {
key: string
check: any
}
export type Mutations<S = State> = {
[MutationTypes.addCheck](state: S, check_map: CheckMap): void
[MutationTypes.removeCheck](state: S, check_map: CheckMap): void
[MutationTypes.editCheck](state: S, check_map: CheckMap, key: string): void
}
export const mutations: MutationTree<State> & Mutations = {
[MutationTypes.addCheck](state: State, check_map: CheckMap) {
if (check_map.key && check_map.check)
state.check.set(check_map.key, check_map.check)
},
[MutationTypes.removeCheck](state: State, check_map: CheckMap) {
if (check_map.key && check_map.check) {
const check = state.check.get(check_map.key)
check.splice(check.indexOf(check_map.check), 1)
state.check.set(check_map.key, check)
}
},
[MutationTypes.editCheck](state: State, check_map: CheckMap) {
if (check_map.key && check_map.check) {
const new_check = [] as unknown as any
const check = state.check.get(check_map.key)
const key = check_map.check.id
check.forEach((it: any) => {
if (it.id === key)
new_check.push(check)
else
new_check.push(it)
})
state.check.set(check_map.key, new_check)
}
},
}

View file

@ -0,0 +1,44 @@
import { ActionContext, ActionTree } from 'vuex'
import { Mutations, MutationTypes } from './mutations'
import { State } from './index'
import { RootState } from '~/store/index'
export enum ActionTypes {
addData = 'addData',
removeData = 'removeData',
editData = 'editData'
}
type AppDataActionContext = {
commit<K extends keyof Mutations>(
key: K,
payload: Parameters<Mutations[K]>[1]
): ReturnType<Mutations[K]>
} & Omit<ActionContext<State, RootState>, 'commit'>
export interface Actions {
[ActionTypes.addData](
{ commit }: AppDataActionContext,
payload: any
): void
[ActionTypes.removeData](
{ commit }: AppDataActionContext,
payload: any
): void
[ActionTypes.editData](
{ commit }: AppDataActionContext,
payload: any
): void
}
export const actions: ActionTree<State, RootState> & Actions = {
[ActionTypes.addData]({ commit }, payload) {
commit(MutationTypes.addData, payload)
},
[ActionTypes.removeData]({ commit }, payload) {
commit(MutationTypes.removeData, payload)
},
[ActionTypes.editData]({ commit }, payload) {
commit(MutationTypes.editData, payload)
},
}

View file

@ -0,0 +1,13 @@
import { GetterTree } from 'vuex'
import { State } from './index'
import { RootState } from '~/store/index'
export type Getters<S = State> = {
data(state: S, key: string): any
}
export const getters: GetterTree<State, RootState> & Getters = {
data: (state, key: string) => {
return state.data.get(key)
},
}

View file

@ -0,0 +1,49 @@
import {
Store as VuexStore,
Module,
CommitOptions,
DispatchOptions,
} from 'vuex'
import { getters, Getters } from './getters'
import { mutations, Mutations, MutationTypes } from './mutations'
import { actions, Actions, ActionTypes } from './actions'
import { RootState } from '~/store/index'
interface State {
data: Map<string, any>
}
const state: State = {
data: new Map(),
}
const data_module: Module<State, RootState> = {
state,
mutations,
actions,
getters,
}
export default data_module
type Store<S = State> = Omit<VuexStore<S>, 'commit' | 'getters' | 'dispatch' > & {
commit<K extends keyof Mutations, P extends Parameters<Mutations[K]>[1]>(
key: K,
payload: P,
options?: CommitOptions
): ReturnType<Mutations[K]>
} & {
getters: {
[K in keyof Getters]: ReturnType<Getters[K]>
}
} & {
dispatch<K extends keyof Actions>(
key: K,
payload: Parameters<Actions[K]>[1],
options?: DispatchOptions
): ReturnType<Actions[K]>
}
export { State, ActionTypes, MutationTypes, Store }

View file

@ -0,0 +1,48 @@
import { MutationTree } from 'vuex'
import { State } from './index'
import { AppData } from '~/store/types'
export enum MutationTypes {
addData = 'addData',
removeData = 'removeData',
editData = 'editData'
}
interface DataMap {
key: string
data: any
}
export type Mutations<S = State> = {
[MutationTypes.addData](state: S, data_map: DataMap): void
[MutationTypes.removeData](state: S, data_map: DataMap): void
[MutationTypes.editData](state: S, data_map: DataMap, key: string): void
}
export const mutations: MutationTree<State> & Mutations = {
[MutationTypes.addData](state: State, data_map: DataMap) {
if (data_map.key && data_map.data)
state.data.set(data_map.key, data_map.data)
},
[MutationTypes.removeData](state: State, data_map: DataMap) {
if (data_map.key && data_map.data) {
const data = state.data.get(data_map.key)
data.splice(data.indexOf(data_map.data), 1)
state.data.set(data_map.key, data)
}
},
[MutationTypes.editData](state: State, data_map: DataMap) {
if (data_map.key && data_map.data) {
const new_data = [] as unknown as any
const data = state.data.get(data_map.key)
const key = data_map.data.id
data.forEach((it: any) => {
if (it.id === key)
new_data.push(data)
else
new_data.push(it)
})
state.data.set(data_map.key, new_data)
}
},
}

View file

@ -0,0 +1,44 @@
import { ActionContext, ActionTree } from 'vuex'
import { Mutations, MutationTypes } from './mutations'
import { State } from './index'
import { RootState } from '~/store/index'
export enum ActionTypes {
addDefs = 'addDefs',
removeDefs = 'removeDefs',
editDefs = 'editDefs'
}
type AppDefsActionContext = {
commit<K extends keyof Mutations>(
key: K,
payload: Parameters<Mutations[K]>[1]
): ReturnType<Mutations[K]>
} & Omit<ActionContext<State, RootState>, 'commit'>
export interface Actions {
[ActionTypes.addDefs](
{ commit }: AppDefsActionContext,
payload: any
): void
[ActionTypes.removeDefs](
{ commit }: AppDefsActionContext,
payload: any
): void
[ActionTypes.editDefs](
{ commit }: AppDefsActionContext,
payload: any
): void
}
export const actions: ActionTree<State, RootState> & Actions = {
[ActionTypes.addDefs]({ commit }, payload) {
commit(MutationTypes.addDefs, payload)
},
[ActionTypes.removeDefs]({ commit }, payload) {
commit(MutationTypes.removeDefs, payload)
},
[ActionTypes.editDefs]({ commit }, payload) {
commit(MutationTypes.editDefs, payload)
},
}

View file

@ -0,0 +1,13 @@
import { GetterTree } from 'vuex'
import { State } from './index'
import { RootState } from '~/store/index'
export type Getters<S = State> = {
defs(state: S, key: string): any
}
export const getters: GetterTree<State, RootState> & Getters = {
defs: (state, key: string) => {
return state.main.get(key)
},
}

View file

@ -0,0 +1,49 @@
import {
Store as VuexStore,
Module,
CommitOptions,
DispatchOptions,
} from 'vuex'
import { getters, Getters } from './getters'
import { mutations, Mutations, MutationTypes } from './mutations'
import { actions, Actions, ActionTypes } from './actions'
import { RootState } from '~/store/index'
interface State {
main: Map<string, any>
}
const state: State = {
main: new Map(),
}
const data_module: Module<State, RootState> = {
state,
mutations,
actions,
getters,
}
export default data_module
type Store<S = State> = Omit<VuexStore<S>, 'commit' | 'getters' | 'dispatch' > & {
commit<K extends keyof Mutations, P extends Parameters<Mutations[K]>[1]>(
key: K,
payload: P,
options?: CommitOptions
): ReturnType<Mutations[K]>
} & {
getters: {
[K in keyof Getters]: ReturnType<Getters[K]>
}
} & {
dispatch<K extends keyof Actions>(
key: K,
payload: Parameters<Actions[K]>[1],
options?: DispatchOptions
): ReturnType<Actions[K]>
}
export { State, ActionTypes, MutationTypes, Store }

View file

@ -0,0 +1,47 @@
import { MutationTree } from 'vuex'
import { State } from './index'
export enum MutationTypes {
addDefs = 'addDefs',
removeDefs = 'removeDefs',
editDefs = 'editDefs'
}
interface DefsMap {
key: string
defs: any
}
export type Mutations<S = State> = {
[MutationTypes.addDefs](state: S, defs_map: DefsMap): void
[MutationTypes.removeDefs](state: S, defs_map: DefsMap): void
[MutationTypes.editDefs](state: S, defs_map: DefsMap, key: string): void
}
export const mutations: MutationTree<State> & Mutations = {
[MutationTypes.addDefs](state: State, defs_map: DefsMap) {
if (defs_map.key && defs_map.defs)
state.main.set(defs_map.key, defs_map.defs)
},
[MutationTypes.removeDefs](state: State, defs_map: DefsMap) {
if (defs_map.key && defs_map.defs) {
const defs = state.main.get(defs_map.key)
defs.splice(defs.indexOf(defs_map.defs), 1)
state.main.set(defs_map.key, defs)
}
},
[MutationTypes.editDefs](state: State, defs_map: DefsMap) {
if (defs_map.key && defs_map.defs) {
const new_defs = [] as unknown as any
const defs = state.main.get(defs_map.key)
const key = defs_map.defs.id
defs.forEach((it: any) => {
if (it.id === key)
new_defs.push(defs)
else
new_defs.push(it)
})
state.main.set(defs_map.key, new_defs)
}
},
}

View file

@ -0,0 +1,35 @@
import data_module, { State as DataState, MutationTypes as DataMutationTypes, ActionTypes as DataActionTypes, Store as DataStore } from './data'
import check_module, { State as CheckState, MutationTypes as CheckMutationTypes, ActionTypes as CheckActionTypes, Store as CheckStore } from './check'
import defs_module, { State as DefsState, MutationTypes as DefsMutationTypes, ActionTypes as DefsActionTypes, Store as DefsStore } from './defs'
import profile_module, { State as ProfileState, MutationTypes as ProfileMutationTypes, ActionTypes as ProfileActionTypes, Store as ProfileStore } from './profile'
import lang_module, { State as LangState, MutationTypes as LangMutationTypes, ActionTypes as LangActionTypes, Store as LangStore } from './lang'
export const AppDataMutationTypes = DataMutationTypes
export const AppCheckMutationTypes = CheckMutationTypes
export const AppDefsMutationTypes = DefsMutationTypes
export const AppProfileMutationTypes = ProfileMutationTypes
export const AppLangMutationTypes = LangMutationTypes
export const AppDataActionTypes = DataActionTypes
export const AppCheckActionTypes = CheckActionTypes
export const AppDefsActionTypes = DefsActionTypes
export const AppProfileActionTypes = ProfileActionTypes
export const AppLangActionTypes = LangActionTypes
export type AppDataState= DataState
export type AppCheckState= CheckState
export type AppDefsState= DefsState
export type AppProfileState= ProfileState
export type AppLangState= LangState
export type AppDataStore = DataStore
export type AppCheckStore = CheckStore
export type AppDefsStore = DefsStore
export type AppProfileStore = ProfileStore
export type AppLangStore = LangStore
export const app_data = data_module
export const app_check = check_module
export const app_defs = defs_module
export const app_profile = profile_module
export const app_lang = lang_module

View file

@ -0,0 +1,68 @@
import { ActionContext, ActionTree } from 'vuex'
import { Mutations, MutationTypes } from './mutations'
import { State } from './index'
import { RootState } from '~/store/index'
export enum ActionTypes {
addLang = 'addLang',
addLangMain = 'addLangMain',
addLangData = 'addLangData',
addLangForms = 'addLangForms',
removeLang = 'removeLang',
editLang = 'editLang'
}
type AppLangActionContext = {
commit<K extends keyof Mutations>(
key: K,
payload: Parameters<Mutations[K]>[1]
): ReturnType<Mutations[K]>
} & Omit<ActionContext<State, RootState>, 'commit'>
export interface Actions {
[ActionTypes.addLang](
{ commit }: AppLangActionContext,
payload: any
): void
[ActionTypes.addLangMain](
{ commit }: AppLangActionContext,
payload: any
): void
[ActionTypes.addLangData](
{ commit }: AppLangActionContext,
payload: any
): void
[ActionTypes.addLangForms](
{ commit }: AppLangActionContext,
payload: any
): void
[ActionTypes.removeLang](
{ commit }: AppLangActionContext,
payload: any
): void
[ActionTypes.editLang](
{ commit }: AppLangActionContext,
payload: any
): void
}
export const actions: ActionTree<State, RootState> & Actions = {
[ActionTypes.addLang]({ commit }, payload) {
commit(MutationTypes.addLang, payload)
},
[ActionTypes.addLangMain]({ commit }, payload) {
commit(MutationTypes.addLangMain, payload)
},
[ActionTypes.addLangData]({ commit }, payload) {
commit(MutationTypes.addLangData, payload)
},
[ActionTypes.addLangForms]({ commit }, payload) {
commit(MutationTypes.addLangForms, payload)
},
[ActionTypes.removeLang]({ commit }, payload) {
commit(MutationTypes.removeLang, payload)
},
[ActionTypes.editLang]({ commit }, payload) {
commit(MutationTypes.editLang, payload)
},
}

View file

@ -0,0 +1,25 @@
import { GetterTree } from 'vuex'
import { State } from './index'
import { RootState } from '~/store/index'
export type Getters<S = State> = {
lang(state: S, key: string): any
langMain(state: S, key: string): any
langData(state: S, key: string): any
langForms(state: S, key: string): any
}
export const getters: GetterTree<State, RootState> & Getters = {
lang: (state: State, key: string) => {
return state.lang.get(key)
},
langMain: (state: State, key: string) => {
return state.lang.get(key)?.main || null
},
langData: (state: State, key: string) => {
return state.lang.get(key)?.data || null
},
langForms: (state: State, key: string) => {
return state.lang.get(key)?.forms || null
},
}

View file

@ -0,0 +1,51 @@
import {
Store as VuexStore,
Module,
CommitOptions,
DispatchOptions,
} from 'vuex'
import { getters, Getters } from './getters'
import { mutations, Mutations, MutationTypes } from './mutations'
import { actions, Actions, ActionTypes } from './actions'
import { RootState } from '~/store/index'
import { AppLang } from '~/store/types'
interface State {
lang: Map<string, AppLang>
}
const state: State = {
lang: new Map(),
}
const data_module: Module<State, RootState> = {
state,
mutations,
actions,
getters,
}
export default data_module
type Store<S = State> = Omit<VuexStore<S>, 'commit' | 'getters' | 'dispatch' > & {
commit<K extends keyof Mutations, P extends Parameters<Mutations[K]>[1]>(
key: K,
payload: P,
options?: CommitOptions
): ReturnType<Mutations[K]>
} & {
getters: {
[K in keyof Getters]: ReturnType<Getters[K]>
}
} & {
dispatch<K extends keyof Actions>(
key: K,
payload: Parameters<Actions[K]>[1],
options?: DispatchOptions
): ReturnType<Actions[K]>
}
export { State, ActionTypes, MutationTypes, Store }

View file

@ -0,0 +1,108 @@
import { MutationTree } from 'vuex'
import { State } from './index'
import { AppLang, AppCtxLang, LangMain, LangData, LangForms } from '~/store/types'
export enum MutationTypes {
addLang = 'addLang',
addLangMain = 'addLangMain',
addLangData = 'addLangData',
addLangForms = 'addLangForms',
removeLang = 'removeLang',
editLang = 'editLang'
}
interface LangMap {
key: string
lang: AppLang
}
interface LangSection {
key: string
section: any
}
interface CtxLangMap {
key: string
lang: AppCtxLang
}
export type Mutations<S = State> = {
[MutationTypes.addLang](state: S, lang_map: LangMap): void
[MutationTypes.addLangMain](state: S, lang_map: LangSection): void
[MutationTypes.addLangData](state: S, lang_map: LangSection): void
[MutationTypes.addLangForms](state: S, lang_map: LangSection): void
[MutationTypes.removeLang](state: S, lang_map: CtxLangMap): void
[MutationTypes.editLang](state: S, lang_map: CtxLangMap): void
}
const get_lang_map = (state: State, mapkey: string): AppLang => {
const lng = state.lang.get(mapkey)
return lng || { main: {}, data: {}, forms: {} }
}
export const mutations: MutationTree<State> & Mutations = {
[MutationTypes.addLang](state: State, lang_map: LangMap) {
state.lang.set(lang_map.key, lang_map.lang)
},
[MutationTypes.addLangMain](state, lang_map: LangSection) {
const lng = get_lang_map(state, lang_map.key)
lng.main = lang_map.section
state.lang.set(lang_map.key, lng)
},
[MutationTypes.addLangData](state, lang_map: LangSection) {
const lng = get_lang_map(state, lang_map.key)
lng.data = lang_map.section
state.lang.set(lang_map.key, lng)
},
[MutationTypes.addLangForms](state, lang_map: LangSection) {
const lng = get_lang_map(state, lang_map.key)
lng.data = lang_map.section
state.lang.set(lang_map.key, lng)
},
[MutationTypes.removeLang](state: State, lang_map: CtxLangMap) {
const lang = get_lang_map(state, lang_map.key)
const key = lang_map.lang.lng.key
switch (lang_map.lang.ctx) {
case 'main':
if (lang.main[key]) {
delete lang.main[key]
state.lang.set(lang_map.key, lang)
}
break
case 'data':
if (lang.data[key]) {
delete lang.data[key]
state.lang.set(lang_map.key, lang)
}
break
case 'forms':
if (lang.forms[key]) {
delete lang.forms[key]
state.lang.set(lang_map.key, lang)
}
break
}
},
[MutationTypes.editLang](state: State, lang_map: CtxLangMap) {
const lang = get_lang_map(state, lang_map.key)
const key = lang_map.lang.lng.key
const val = lang_map.lang.lng.text
switch (lang_map.lang.ctx) {
case 'main':
if (lang.main[key]) {
lang.main[key] = val
state.lang.set(lang_map.key, lang)
}
break
case 'data':
if (lang.data[key]) {
lang.data[key] = val
state.lang.set(lang_map.key, lang)
}
break
case 'forms':
if (lang.forms[key]) {
lang.forms[key] = val
state.lang.set(lang_map.key, lang)
}
break
}
},
}

View file

@ -0,0 +1,44 @@
import { ActionContext, ActionTree } from 'vuex'
import { Mutations, MutationTypes } from './mutations'
import { State } from './index'
import { RootState } from '~/store/index'
export enum ActionTypes {
addProfile = 'addProfile',
removeProfile = 'removeProfile',
editProfile = 'editProfile'
}
type AppProfileActionContext = {
commit<K extends keyof Mutations>(
key: K,
payload: Parameters<Mutations[K]>[1]
): ReturnType<Mutations[K]>
} & Omit<ActionContext<State, RootState>, 'commit'>
export interface Actions {
[ActionTypes.addProfile](
{ commit }: AppProfileActionContext,
payload: any
): void
[ActionTypes.removeProfile](
{ commit }: AppProfileActionContext,
payload: any
): void
[ActionTypes.editProfile](
{ commit }: AppProfileActionContext,
payload: any
): void
}
export const actions: ActionTree<State, RootState> & Actions = {
[ActionTypes.addProfile]({ commit }, payload) {
commit(MutationTypes.addProfile, payload)
},
[ActionTypes.removeProfile]({ commit }, payload) {
commit(MutationTypes.removeProfile, payload)
},
[ActionTypes.editProfile]({ commit }, payload) {
commit(MutationTypes.editProfile, payload)
},
}

View file

@ -0,0 +1,13 @@
import { GetterTree } from 'vuex'
import { State } from './index'
import { RootState } from '~/store/index'
export type Getters<S = State> = {
profile(state: S): any
}
export const getters: GetterTree<State, RootState> & Getters = {
profile: (state: State) => {
return state.profile
},
}

View file

@ -0,0 +1,49 @@
import {
Store as VuexStore,
Module,
CommitOptions,
DispatchOptions,
} from 'vuex'
import { getters, Getters } from './getters'
import { mutations, Mutations, MutationTypes } from './mutations'
import { actions, Actions, ActionTypes } from './actions'
import { RootState } from '~/store/index'
interface State {
profile: any
}
const state: State = {
profile: {},
}
const data_module: Module<State, RootState> = {
state,
mutations,
actions,
getters,
}
export default data_module
type Store<S = State> = Omit<VuexStore<S>, 'commit' | 'getters' | 'dispatch' > & {
commit<K extends keyof Mutations, P extends Parameters<Mutations[K]>[1]>(
key: K,
payload: P,
options?: CommitOptions
): ReturnType<Mutations[K]>
} & {
getters: {
[K in keyof Getters]: ReturnType<Getters[K]>
}
} & {
dispatch<K extends keyof Actions>(
key: K,
payload: Parameters<Actions[K]>[1],
options?: DispatchOptions
): ReturnType<Actions[K]>
}
export { State, ActionTypes, MutationTypes, Store }

View file

@ -0,0 +1,37 @@
import { MutationTree } from 'vuex'
import { State } from './index'
export enum MutationTypes {
addProfile = 'addProfile',
removeProfile = 'removeProfile',
editProfile = 'editProfile'
}
export type Mutations<S = State> = {
[MutationTypes.addProfile](state: S, profile: any): void
[MutationTypes.removeProfile](state: S, target: any): void
[MutationTypes.editProfile](state: S, target: any, key: string): void
}
export const mutations: MutationTree<State> & Mutations = {
[MutationTypes.addProfile](state: State, profile: any) {
state.profile = profile
},
[MutationTypes.removeProfile](state: State, target: any) {
const profile = state.profile as any
const key = target.key
if (profile[key]) {
delete profile[key]
state.profile = profile
}
},
[MutationTypes.editProfile](state: State, target: any) {
const profile = state.profile as any
const key = target.key
const data = target.data
if (profile[key]) {
profile[key] = data
state.profile = profile
}
},
}

View file

@ -0,0 +1 @@
export * from './app'

97
src/store/store_module.ts Normal file
View file

@ -0,0 +1,97 @@
import { useStore, Store } from 'vuex'
interface InternalModule<S, A, M, G> {
state: S
actions: A
mutations: M
getters: G
}
/**
* This function allows us to access the internal vuex properties and
* maps them in a way which removes the module prefix.
*/
function getFromStoreByType<T>(
moduleName: string,
type: unknown,
isNamespaced: boolean,
) {
if (isNamespaced) {
return Object.keys(type)
.filter(t => t.startsWith(`${moduleName}/`))
.reduce((acc, curr) => {
const typeName = curr.split('/').pop()
const typeValue = type[curr][0]
return { [typeName]: typeValue, ...acc }
}, {}) as T
}
return Object.keys(type).reduce((acc, curr) => {
const typeValue = type[curr][0]
return { [curr]: typeValue, ...acc }
}, {}) as T
}
/*
* We have to wrap the getters in a Proxy because we only want to
* "access" the getter if it actually being accessed.
*
* We could technically use the getFromStoreByType function but
* the getter would be invoked multiple types on store instantiation.
*
* This is just a little cheeky workaround. Proxy <3
*/
function wrapGettersInProxy<G>(
moduleName: string,
getters: G,
isNamespaced: boolean,
) {
return new Proxy(getters as Object, {
get(_, prop: string) {
if (isNamespaced)
return getters[`${moduleName}/${prop}`]
return getters[prop]
},
}) as G
}
function isModuleNamespaced<S>(moduleName: string, store: Store<S>): boolean {
// @ts-ignore internal Vuex object that isn't typed.
return Boolean(store._modulesNamespaceMap[`${moduleName}/`])
}
export default function useStoreModule<S = any, A = any, M = any, G = any>(
moduleName: string,
storeName?: string,
): InternalModule<S, A, M, G> {
// @ts-ignore useStore doesn't yet accept a key as arg
const store = storeName ? useStore(storeName) : useStore()
const state = store.state[moduleName]
const isNamespaced = isModuleNamespaced(moduleName, store)
const actions = getFromStoreByType<A>(
moduleName,
// @ts-ignore internal Vuex object that isn't typed.
store._actions,
isNamespaced,
)
const mutations = getFromStoreByType<M>(
moduleName,
// @ts-ignore internal Vuex object that isn't typed.
store._mutations,
isNamespaced,
)
const getters = wrapGettersInProxy<G>(moduleName, store.getters, isNamespaced)
return {
actions,
mutations,
state,
getters,
}
}

78
src/store/types.ts Normal file
View file

@ -0,0 +1,78 @@
import {
AppDataMutationTypes,
AppDataActionTypes,
AppCheckMutationTypes,
AppCheckActionTypes,
AppDefsMutationTypes,
AppDefsActionTypes,
AppProfileMutationTypes,
AppProfileActionTypes,
AppLangMutationTypes,
AppLangActionTypes,
} from './modules/app/index'
export const AppDataMutation = AppDataMutationTypes
export const AppDataAction = AppDataActionTypes
export const AppCheckMutation = AppCheckMutationTypes
export const AppCheckAction = AppCheckActionTypes
export const AppDefsMutation = AppDefsMutationTypes
export const AppDefsAction = AppDefsActionTypes
export const AppProfileMutation = AppProfileMutationTypes
export const AppProfileAction = AppProfileActionTypes
export const AppLangMutation = AppLangMutationTypes
export const AppLangAction = AppLangActionTypes
export interface LangCtxKey {
ctx: string
key: string
}
export interface LangItem {
key: string
text: string
}
export interface LangMain {
[key: string]: string
}
export interface LangData {
[key: string]: string
}
export interface LangForms {
[key: string]: string
}
export interface AppLang {
main: LangMain
data: LangData
forms: LangForms
}
export interface AppCtxLang {
ctx: string
lng: LangItem
}
export interface AppDefs {
[key: string]: string
}
export interface AppProfile {
[key: string]: string
}
export interface AppData {
[key: string]: string
}
export interface AppCheck {
[key: string]: string
}
/*
export interface AppStore {
defs: AppDefs
profile: AppProfile
data: [AppData]
lang: AppLang
}
*/