Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Stores: migrate Subscriber store to createReduxStore #74430

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/data-stores/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export * from './templates';
export * from './onboard/types';
export * from './domain-suggestions/types';
export * from './plans/types';
export * from './subscriber/types';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, those are already exported as Subscriber. types and consumed only that way 👍

export * from './launch/types';
export * from './user/types';

Expand Down
23 changes: 9 additions & 14 deletions packages/data-stores/src/subscriber/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { registerStore } from '@wordpress/data';
import { register, createReduxStore } from '@wordpress/data';
import { controls } from '../wpcom-request-controls';
import { createActions } from './actions';
import { STORE_KEY } from './constants';
Expand All @@ -7,16 +7,11 @@ import * as selectors from './selectors';
export * from './types';
export type { State };

let isRegistered = false;
export function register(): typeof STORE_KEY {
if ( ! isRegistered ) {
isRegistered = true;
registerStore( STORE_KEY, {
actions: createActions(),
controls,
reducer,
selectors,
} );
}
return STORE_KEY;
}
export const store = createReduxStore( STORE_KEY, {
actions: createActions(),
controls,
reducer,
selectors,
} );

register( store );
10 changes: 0 additions & 10 deletions packages/data-stores/src/subscriber/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import * as actions from './actions';
import * as selectors from './selectors';
import type { DispatchFromMap, SelectFromMap } from '../mapped-types';

export interface SubscriberState {
add?: {
inProgress: boolean;
Expand Down Expand Up @@ -48,9 +44,3 @@ export type ImportSubscribersResponse = {

export type GetSubscribersImportResponse = ImportJob;
export type GetSubscribersImportsResponse = ImportJob[];

export interface SubscriberDispatch {
dispatch: DispatchFromMap< typeof actions >;
}

export type SubscriberSelect = SelectFromMap< typeof selectors >;
7 changes: 3 additions & 4 deletions packages/subscriber/src/components/add-form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable wpcalypso/jsx-classname-namespace */
import { FormInputValidation } from '@automattic/components';
import { Subscriber } from '@automattic/data-stores';
import { localizeUrl } from '@automattic/i18n-utils';
import { Title, SubTitle, NextButton } from '@automattic/onboarding';
import { TextControl, FormFileUpload, Button } from '@wordpress/components';
Expand All @@ -20,9 +21,7 @@ import React, {
import { useActiveJobRecognition } from '../../hooks/use-active-job-recognition';
import { useInProgressState } from '../../hooks/use-in-progress-state';
import { RecordTrackEvents, useRecordAddFormEvents } from '../../hooks/use-record-add-form-events';
import { SUBSCRIBER_STORE } from '../../store';
import { tip } from './icon';
import type { SubscriberSelect } from '@automattic/data-stores';
import './style.scss';

interface Props {
Expand Down Expand Up @@ -69,7 +68,7 @@ export const AddSubscriberForm: FunctionComponent< Props > = ( props ) => {
importCsvSubscribers,
importCsvSubscribersUpdate,
getSubscribersImports,
} = useDispatch( SUBSCRIBER_STORE );
} = useDispatch( Subscriber.store );

/**
* ↓ Fields
Expand All @@ -92,7 +91,7 @@ export const AddSubscriberForm: FunctionComponent< Props > = ( props ) => {
const [ submitAttemptCount, setSubmitAttemptCount ] = useState( 0 );
const [ submitBtnReady, setIsSubmitBtnReady ] = useState( isSubmitButtonReady() );
const importSelector = useSelect(
( s ) => ( s( SUBSCRIBER_STORE ) as SubscriberSelect ).getImportSubscribersSelector(),
( select ) => select( Subscriber.store ).getImportSubscribersSelector(),
[]
);
const [ formFileUploadElement ] = useState(
Expand Down
9 changes: 2 additions & 7 deletions packages/subscriber/src/hooks/use-active-job-recognition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Subscriber } from '@automattic/data-stores';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect } from 'react';
import { SUBSCRIBER_STORE } from '../store';
import type { SubscriberSelect } from '@automattic/data-stores';

type ImportJob = Subscriber.ImportJob;
type ImportJobStatus = Subscriber.ImportJobStatus;
Expand All @@ -11,13 +9,10 @@ export function useActiveJobRecognition( siteId: number ) {
const INTERVAL_ACTIVE = 1000;
const INTERVAL_INACTIVE = 5000;
const ACTIVE_STATE: ImportJobStatus[] = [ 'pending', 'importing' ];
const { getSubscribersImports, importCsvSubscribersUpdate } = useDispatch( SUBSCRIBER_STORE );
const { getSubscribersImports, importCsvSubscribersUpdate } = useDispatch( Subscriber.store );

const imports =
useSelect(
( s ) => ( s( SUBSCRIBER_STORE ) as SubscriberSelect ).getImportJobsSelector(),
[]
) || [];
useSelect( ( select ) => select( Subscriber.store ).getImportJobsSelector(), [] ) || [];
const jobs = imports.filter( ( x: ImportJob ) => ACTIVE_STATE.includes( x.status ) );
const activeJob = jobs.length ? jobs[ 0 ] : undefined;

Expand Down
5 changes: 2 additions & 3 deletions packages/subscriber/src/hooks/use-in-progress-state.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Subscriber } from '@automattic/data-stores';
import { useSelect } from '@wordpress/data';
import { SUBSCRIBER_STORE } from '../store';
import type { SubscriberSelect } from '@automattic/data-stores';

export function useInProgressState() {
const { addSelector, importSelector } = useSelect( ( select ) => {
const subscriber: SubscriberSelect = select( SUBSCRIBER_STORE );
const subscriber = select( Subscriber.store );
return {
addSelector: subscriber.getAddSubscribersSelector(),
importSelector: subscriber.getImportSubscribersSelector(),
Expand Down
7 changes: 3 additions & 4 deletions packages/subscriber/src/hooks/use-record-add-form-events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Subscriber } from '@automattic/data-stores/src';
import { useSelect } from '@wordpress/data';
import { useEffect, useRef } from 'react';
import { SUBSCRIBER_STORE } from '../store';
import { useInProgressState } from './use-in-progress-state';
import type { SubscriberSelect } from '@automattic/data-stores';

export type RecordTrackEvents = (
eventName: string,
Expand All @@ -14,11 +13,11 @@ export function useRecordAddFormEvents( recordTracksEvent?: RecordTrackEvents, f
const inProgress = useInProgressState();
const prevInProgress = useRef( inProgress );
const addSelector = useSelect(
( s ) => ( s( SUBSCRIBER_STORE ) as SubscriberSelect ).getAddSubscribersSelector(),
( select ) => select( Subscriber.store ).getAddSubscribersSelector(),
[]
);
const importSelector = useSelect(
( s ) => ( s( SUBSCRIBER_STORE ) as SubscriberSelect ).getImportSubscribersSelector(),
( select ) => select( Subscriber.store ).getImportSubscribersSelector(),
[]
);

Expand Down
1 change: 0 additions & 1 deletion packages/subscriber/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './store';
export { AddSubscriberForm } from './components/add-form';
3 changes: 0 additions & 3 deletions packages/subscriber/src/store.ts

This file was deleted.