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

Patterns: fix bug with pattern categories not saving sometimes #54676

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 18 additions & 7 deletions packages/patterns/src/components/category-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const DEFAULT_QUERY = {
};
const slug = 'wp_pattern_category';

export default function CategorySelector( { onCategorySelection } ) {
export default function CategorySelector( {
onCategorySelection,
setCategorySaving,
} ) {
const [ values, setValues ] = useState( [] );
const [ search, setSearch ] = useState( '' );
const debouncedSearch = useDebounce( setSearch, 500 );
Expand Down Expand Up @@ -92,13 +95,21 @@ export default function CategorySelector( { onCategorySelection } ) {

setValues( uniqueTerms );

Promise.all(
uniqueTerms.map( ( termName ) =>
findOrCreateTerm( { name: termName } )
)
).then( ( newTerms ) => {
onCategorySelection( newTerms );
// If the user clicks the create pattern modal button directly after entering
// a category we need to return a promise so the pattern doesn't save before
// the save of the categories is completed.
const categorySaving = new Promise( function ( resolve ) {
Promise.all(
uniqueTerms.map( ( termName ) =>
findOrCreateTerm( { name: termName } )
)
).then( ( newTerms ) => {
setCategorySaving();
onCategorySelection( newTerms );
resolve( newTerms );
} );
glendaviesnz marked this conversation as resolved.
Show resolved Hide resolved
} );
setCategorySaving( categorySaving );
}

return (
Expand Down
22 changes: 20 additions & 2 deletions packages/patterns/src/components/create-pattern-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ export default function CreatePatternModal( {
const [ syncType, setSyncType ] = useState( PATTERN_SYNC_TYPES.full );
const [ categories, setCategories ] = useState( [] );
const [ title, setTitle ] = useState( '' );
const [ categorySaving, setCategorySaving ] = useState();
glendaviesnz marked this conversation as resolved.
Show resolved Hide resolved
const { createPattern } = unlock( useDispatch( patternsStore ) );

const { createErrorNotice } = useDispatch( noticesStore );
async function onCreate( patternTitle, sync ) {

async function addPattern( patternTitle, sync, patternCategories ) {
try {
const newPattern = await createPattern(
patternTitle,
sync,
typeof content === 'function' ? content() : content,
categories
patternCategories
);
onSuccess( {
pattern: newPattern,
Expand All @@ -60,6 +62,21 @@ export default function CreatePatternModal( {
}
}

async function onCreate( patternTitle, sync ) {
// Check that any onBlur save of the categories is completed
// before creating the pattern and closing the modal.
if ( categorySaving ) {
const newTerms = await categorySaving;
addPattern(
patternTitle,
sync,
newTerms.map( ( cat ) => cat.id )
);
return;
}
addPattern( patternTitle, sync, categories );
}

const handleCategorySelection = ( selectedCategories ) => {
setCategories( selectedCategories.map( ( cat ) => cat.id ) );
};
Expand Down Expand Up @@ -91,6 +108,7 @@ export default function CreatePatternModal( {
/>
<CategorySelector
onCategorySelection={ handleCategorySelection }
setCategorySaving={ setCategorySaving }
/>
<ToggleControl
label={ __( 'Synced' ) }
Expand Down
Loading