Skip to content

Commit f361613

Browse files
committed
Fix the issue with promises
1 parent 1f440c2 commit f361613

2 files changed

Lines changed: 55 additions & 40 deletions

File tree

.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module.exports = {
44
'plugin:eslint-comments/recommended',
55
],
66
parserOptions: {
7-
ecmaVersion: "latest",
7+
ecmaVersion: 'latest',
88
},
99
rules: {
10-
"no-console": "off",
10+
'no-console': 'off',
1111
},
1212
};

assets/js/suggested-task-terms.js

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
* Populate prplSuggestedTasksTerms with the terms for the taxonomies we use.
44
*
5-
* Dependencies: wp-api, progress-planner/document-ready
5+
* Dependencies: wp-api-fetch, progress-planner/document-ready
66
*/
77

88
const prplSuggestedTasksTerms = {};
@@ -40,48 +40,63 @@ const prplTerms = {
4040
`Terms already fetched for taxonomy: ${ taxonomy }`
4141
);
4242
resolve( prplSuggestedTasksTerms[ taxonomy ] );
43+
return;
4344
}
44-
wp.api.loadPromise.done( () => {
45-
console.info( `Fetching terms for taxonomy: ${ taxonomy }...` );
4645

47-
const typeName = taxonomy.replace( 'prpl_', 'Prpl_' );
48-
prplSuggestedTasksTerms[ taxonomy ] =
49-
prplSuggestedTasksTerms[ taxonomy ] || {};
50-
const TermsCollection = new wp.api.collections[ typeName ]();
51-
TermsCollection.fetch( { data: { per_page: 100 } } ).done(
52-
( data ) => {
53-
let userTermFound = false;
54-
// 100 is the maximum number of terms that can be fetched in one request.
55-
data.forEach( ( term ) => {
56-
prplSuggestedTasksTerms[ taxonomy ][ term.slug ] =
57-
term;
58-
if ( 'user' === term.slug ) {
59-
userTermFound = true;
60-
}
61-
} );
46+
console.info( `Fetching terms for taxonomy: ${ taxonomy }...` );
6247

63-
if ( userTermFound ) {
64-
resolve( prplSuggestedTasksTerms[ taxonomy ] );
65-
} else {
66-
// If the `user` term doesn't exist, create it.
67-
const newTermModel = new wp.api.models[ typeName ](
68-
{
69-
slug: 'user',
70-
name: 'user',
71-
}
72-
);
73-
newTermModel
74-
.save()
75-
.then( ( response ) => {
76-
prplSuggestedTasksTerms[ taxonomy ].user =
77-
response;
78-
return prplSuggestedTasksTerms[ taxonomy ];
79-
} )
80-
.then( resolve ); // Resolve the promise after all requests are complete.
48+
prplSuggestedTasksTerms[ taxonomy ] =
49+
prplSuggestedTasksTerms[ taxonomy ] || {};
50+
51+
// Fetch terms using wp.apiFetch.
52+
wp.apiFetch( {
53+
path: `/wp/v2/${ taxonomy }?per_page=100`,
54+
} )
55+
.then( ( data ) => {
56+
let userTermFound = false;
57+
// 100 is the maximum number of terms that can be fetched in one request.
58+
data.forEach( ( term ) => {
59+
prplSuggestedTasksTerms[ taxonomy ][ term.slug ] = term;
60+
if ( 'user' === term.slug ) {
61+
userTermFound = true;
8162
}
63+
} );
64+
65+
if ( userTermFound ) {
66+
resolve( prplSuggestedTasksTerms[ taxonomy ] );
67+
} else {
68+
// If the `user` term doesn't exist, create it.
69+
wp.apiFetch( {
70+
path: `/wp/v2/${ taxonomy }`,
71+
method: 'POST',
72+
data: {
73+
slug: 'user',
74+
name: 'user',
75+
},
76+
} )
77+
.then( ( response ) => {
78+
prplSuggestedTasksTerms[ taxonomy ].user =
79+
response;
80+
resolve( prplSuggestedTasksTerms[ taxonomy ] );
81+
} )
82+
.catch( ( error ) => {
83+
console.error(
84+
`Error creating user term for taxonomy: ${ taxonomy }`,
85+
error
86+
);
87+
// Resolve anyway even if creation fails.
88+
resolve( prplSuggestedTasksTerms[ taxonomy ] );
89+
} );
8290
}
83-
);
84-
} );
91+
} )
92+
.catch( ( error ) => {
93+
console.error(
94+
`Error fetching terms for taxonomy: ${ taxonomy }`,
95+
error
96+
);
97+
// Resolve with empty object on error.
98+
resolve( prplSuggestedTasksTerms[ taxonomy ] );
99+
} );
85100
} );
86101
},
87102

0 commit comments

Comments
 (0)