Hide the task list and store the state in the store

[MAILPOET-4826]
This commit is contained in:
Rostislav Wolny
2022-12-15 14:21:00 +01:00
committed by Aschepikov
parent be592dbf9b
commit b0e8263e2f
8 changed files with 62 additions and 16 deletions

View File

@@ -1,9 +1,17 @@
import { MailPoet } from 'mailpoet'; import { MailPoet } from 'mailpoet';
import { useSelect, useDispatch } from '@wordpress/data';
import { DropdownMenu } from '@wordpress/components'; import { DropdownMenu } from '@wordpress/components';
import { moreVertical } from '@wordpress/icons'; import { moreVertical } from '@wordpress/icons';
import { storeName } from 'homepage/store/store';
export function TaskList(): JSX.Element { export function TaskList(): JSX.Element {
return ( const isTaskListHidden = useSelect(
(select) => select(storeName).getIsTaskListHidden(),
[],
);
const { hideTaskList } = useDispatch(storeName);
return isTaskListHidden ? null : (
<> <>
<h1>{MailPoet.I18n.t('welcomeToMailPoet')}</h1> <h1>{MailPoet.I18n.t('welcomeToMailPoet')}</h1>
<h2>{MailPoet.I18n.t('beginByCompletingSetup')}</h2> <h2>{MailPoet.I18n.t('beginByCompletingSetup')}</h2>
@@ -13,7 +21,7 @@ export function TaskList(): JSX.Element {
controls={[ controls={[
{ {
title: MailPoet.I18n.t('hideList'), title: MailPoet.I18n.t('hideList'),
onClick: () => {}, onClick: hideTaskList,
icon: null, icon: null,
}, },
]} ]}

View File

@@ -1,5 +1,5 @@
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import { useEffect } from 'react'; import { useEffect, useState } from 'react';
import { GlobalContext, useGlobalContextValue } from 'context/index.jsx'; import { GlobalContext, useGlobalContextValue } from 'context/index.jsx';
import { TopBarWithBeamer } from 'common/top_bar/top_bar'; import { TopBarWithBeamer } from 'common/top_bar/top_bar';
import { HomepageNotices } from 'homepage/notices'; import { HomepageNotices } from 'homepage/notices';
@@ -7,20 +7,21 @@ import { TaskList } from './components/task-list';
import { createStore } from './store/store'; import { createStore } from './store/store';
function App(): JSX.Element { function App(): JSX.Element {
const [isStoreInitialized, setIsStoreInitialized] = useState(false);
useEffect(() => { useEffect(() => {
createStore(); createStore();
setIsStoreInitialized(true);
}, []); }, []);
return ( return (
<GlobalContext.Provider value={useGlobalContextValue(window)}> <GlobalContext.Provider value={useGlobalContextValue(window)}>
<TopBarWithBeamer /> <TopBarWithBeamer />
<HomepageNotices /> <HomepageNotices />
<TaskList /> {isStoreInitialized ? <TaskList /> : null}
</GlobalContext.Provider> </GlobalContext.Provider>
); );
} }
const container = document.getElementById('mailpoet_homepage_container'); const container = document.getElementById('mailpoet_homepage_container');
if (container) { if (container) {
ReactDOM.render(<App />, container); ReactDOM.render(<App />, container);
} }

View File

@@ -0,0 +1,3 @@
export const hideTaskList = () => ({
type: 'SET_TASK_LIST_HIDDEN',
});

View File

@@ -0,0 +1,9 @@
import { State } from './types';
export function getInitialState(): State {
return {
taskList: {
isTaskListHidden: false,
},
};
}

View File

@@ -0,0 +1,17 @@
import { Action } from '@wordpress/data';
import { State } from './types';
export function reducer(state: State, action: Action): State {
switch (action.type) {
case 'SET_TASK_LIST_HIDDEN':
return {
...state,
taskList: {
...state.taskList,
isTaskListHidden: true,
},
};
default:
return state;
}
}

View File

@@ -0,0 +1,5 @@
import { State } from './types';
export function getIsTaskListHidden(state: State): boolean {
return state.taskList.isTaskListHidden;
}

View File

@@ -5,31 +5,27 @@ import {
StoreDescriptor, StoreDescriptor,
} from '@wordpress/data'; } from '@wordpress/data';
import { OmitFirstArgs } from 'types'; import { OmitFirstArgs } from 'types';
import { getInitialState } from './initial-state';
import * as actions from './actions';
import * as selectors from './selectors';
import { reducer } from './reducer';
import { State } from './types';
const storeName = 'mailpoet/homepage'; export const storeName = 'mailpoet/homepage';
const actions = {};
const selectors = {};
const controls = {}; const controls = {};
const reducer = (state) => state;
const initialState = {};
type StoreType = Omit<StoreDescriptor, 'name'> & { type StoreType = Omit<StoreDescriptor, 'name'> & {
name: typeof storeName; name: typeof storeName;
}; };
type State = {
[K in string]: never;
};
type EditorStoreConfigType = StoreConfig<State>; type EditorStoreConfigType = StoreConfig<State>;
export const createStore = (): StoreType => { export const createStore = (): StoreType => {
const storeConfig = { const storeConfig = {
actions, actions,
controls, controls,
selectors, selectors,
reducer, reducer,
initialState, initialState: getInitialState(),
} as EditorStoreConfigType; } as EditorStoreConfigType;
const store = createReduxStore<State>(storeName, storeConfig) as StoreType; const store = createReduxStore<State>(storeName, storeConfig) as StoreType;
register(store); register(store);
return store; return store;

View File

@@ -0,0 +1,7 @@
export type TaskListState = {
isTaskListHidden: boolean;
};
export type State = {
taskList: TaskListState;
};