|
| 1 | +import { createSlice } from '@reduxjs/toolkit'; |
| 2 | +import { AppState, AppThunk } from '@app/store'; |
| 3 | + |
| 4 | +export interface MenuList { |
| 5 | + editMetadata: boolean; |
| 6 | + editContent: boolean; |
| 7 | + // testCrosswalk: boolean; |
| 8 | + publish: boolean; |
| 9 | + invalidate: boolean; |
| 10 | + deprecate: boolean; |
| 11 | + remove: boolean; |
| 12 | + version: boolean; |
| 13 | + mscrCopy: boolean; |
| 14 | + deleteDraft: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +const initialMenuList: MenuList = { |
| 18 | + deleteDraft: false, |
| 19 | + deprecate: false, |
| 20 | + editContent: false, |
| 21 | + editMetadata: false, |
| 22 | + invalidate: false, |
| 23 | + mscrCopy: false, |
| 24 | + publish: false, |
| 25 | + remove: false, |
| 26 | + version: false, |
| 27 | +}; |
| 28 | + |
| 29 | +export interface ModalList { |
| 30 | + form: FormState; |
| 31 | + confirm: ConfirmState; |
| 32 | +} |
| 33 | + |
| 34 | +export interface FormState { |
| 35 | + mscrCopy: boolean; |
| 36 | + version: boolean; |
| 37 | +} |
| 38 | + |
| 39 | +const initialFormState: FormState = { |
| 40 | + mscrCopy: false, |
| 41 | + version: false |
| 42 | +}; |
| 43 | + |
| 44 | +export interface ConfirmState { |
| 45 | + deleteDraft: boolean; |
| 46 | + deprecate: boolean; |
| 47 | + invalidate: boolean; |
| 48 | + publish: boolean; |
| 49 | + remove: boolean; |
| 50 | + saveMetadata: boolean; |
| 51 | +} |
| 52 | + |
| 53 | +const initialConfirmState: ConfirmState = { |
| 54 | + deleteDraft: false, |
| 55 | + deprecate: false, |
| 56 | + invalidate: false, |
| 57 | + publish: false, |
| 58 | + remove: false, |
| 59 | + saveMetadata: false, |
| 60 | +}; |
| 61 | + |
| 62 | +const initialModalList: ModalList = { |
| 63 | + confirm: initialConfirmState, |
| 64 | + form: initialFormState, |
| 65 | +}; |
| 66 | + |
| 67 | +const initialState = { |
| 68 | + isLatest: false, |
| 69 | + isPublished: false, |
| 70 | + isCrosswalk: false, |
| 71 | + menuList: initialMenuList, |
| 72 | + modal: initialModalList, |
| 73 | +}; |
| 74 | + |
| 75 | +export const actionmenuSlice = createSlice({ |
| 76 | + name: 'actionmenu', |
| 77 | + initialState: initialState, |
| 78 | + reducers: { |
| 79 | + setIsLatest(state, action) { |
| 80 | + return { |
| 81 | + ...state, |
| 82 | + isLatest: action.payload |
| 83 | + }; |
| 84 | + }, |
| 85 | + setIsPublished(state, action) { |
| 86 | + return { |
| 87 | + ...state, |
| 88 | + isPublished: action.payload |
| 89 | + }; |
| 90 | + }, |
| 91 | + setIsCrosswalk(state, action) { |
| 92 | + return { |
| 93 | + ...state, |
| 94 | + isCrosswalk: action.payload |
| 95 | + }; |
| 96 | + }, |
| 97 | + setMenuList(state, action) { |
| 98 | + const menuListAddition: Partial<MenuList> = {}; |
| 99 | + action.payload.forEach((key: keyof MenuList) => menuListAddition[key] = true); |
| 100 | + return { |
| 101 | + ...state, |
| 102 | + menuList: { |
| 103 | + ...state.menuList, |
| 104 | + ...menuListAddition |
| 105 | + } |
| 106 | + }; |
| 107 | + }, |
| 108 | + setMenuListFull(state, action) { |
| 109 | + return { |
| 110 | + ...state, |
| 111 | + menuList: action.payload |
| 112 | + }; |
| 113 | + }, |
| 114 | + setFormState(state, action) { |
| 115 | + return { |
| 116 | + ...state, |
| 117 | + modal: { |
| 118 | + ...initialModalList, |
| 119 | + form: { |
| 120 | + ...initialFormState, |
| 121 | + [action.payload.key]: action.payload.value |
| 122 | + } |
| 123 | + } |
| 124 | + }; |
| 125 | + }, |
| 126 | + setConfirmState(state, action) { |
| 127 | + return { |
| 128 | + ...state, |
| 129 | + modal: { |
| 130 | + ...initialModalList, |
| 131 | + confirm: { |
| 132 | + ...initialConfirmState, |
| 133 | + [action.payload.key]: action.payload.value |
| 134 | + } |
| 135 | + } |
| 136 | + }; |
| 137 | + }, |
| 138 | + }, |
| 139 | +}); |
| 140 | + |
| 141 | +export function selectIsLatest() { |
| 142 | + return (state: AppState) => state.actionmenu.isLatest; |
| 143 | +} |
| 144 | + |
| 145 | +export function setIsLatest(isLatest?: boolean): AppThunk { |
| 146 | + return (dispatch) => |
| 147 | + dispatch(actionmenuSlice.actions.setIsLatest(isLatest ?? false)); |
| 148 | +} |
| 149 | + |
| 150 | +export function selectIsPublished() { |
| 151 | + return (state: AppState) => state.actionmenu.isPublished; |
| 152 | +} |
| 153 | + |
| 154 | +export function setIsPublished(isPublished?: boolean): AppThunk { |
| 155 | + return (dispatch) => |
| 156 | + dispatch(actionmenuSlice.actions.setIsPublished(isPublished ?? false)); |
| 157 | +} |
| 158 | + |
| 159 | +export function selectIsCrosswalk() { |
| 160 | + return (state: AppState) => state.actionmenu.isCrosswalk; |
| 161 | +} |
| 162 | + |
| 163 | +export function setIsCrosswalk(isCrosswalk?: boolean): AppThunk { |
| 164 | + return (dispatch) => |
| 165 | + dispatch(actionmenuSlice.actions.setIsCrosswalk(isCrosswalk ?? false)); |
| 166 | +} |
| 167 | + |
| 168 | +export function selectMenuList() { |
| 169 | + return (state: AppState) => state.actionmenu.menuList; |
| 170 | +} |
| 171 | + |
| 172 | +export function setMenuList(menuList: Array<keyof MenuList>): AppThunk { |
| 173 | + return (dispatch) => dispatch(actionmenuSlice.actions.setMenuList(menuList)); |
| 174 | +} |
| 175 | + |
| 176 | +export function resetMenuList(): AppThunk { |
| 177 | + return (dispatch) => dispatch(actionmenuSlice.actions.setMenuListFull(initialMenuList)); |
| 178 | +} |
| 179 | + |
| 180 | +export function selectModal() { |
| 181 | + return (state: AppState) => state.actionmenu.modal; |
| 182 | +} |
| 183 | + |
| 184 | +export function selectFormModalState() { |
| 185 | + return (state: AppState) => state.actionmenu.modal.form; |
| 186 | +} |
| 187 | + |
| 188 | +export function setFormModalState(formType: {key: keyof FormState; value: boolean}): AppThunk { |
| 189 | + return (dispatch) => dispatch(actionmenuSlice.actions.setFormState(formType)); |
| 190 | +} |
| 191 | + |
| 192 | +export function selectConfirmModalState() { |
| 193 | + return (state: AppState) => state.actionmenu.modal.confirm; |
| 194 | +} |
| 195 | + |
| 196 | +export function setConfirmModalState(confirmType: {key: keyof ConfirmState; value: boolean}): AppThunk { |
| 197 | + return (dispatch) => dispatch(actionmenuSlice.actions.setConfirmState(confirmType)); |
| 198 | +} |
0 commit comments