File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed
Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,16 @@ export default class PostsTags {
6666 post ! : Post ;
6767
6868 static async syncPostTags ( postId : string , tags : Tag [ ] ) {
69+ const uniqueTags = tags . reduce < Tag [ ] > ( ( acc , current ) => {
70+ if ( ! acc . find ( tag => tag . id === current . id ) ) {
71+ acc . push ( current ) ;
72+ return acc ;
73+ }
74+ return acc ;
75+ } , [ ] ) ;
76+
77+ console . log ( uniqueTags ) ;
78+
6979 const repo = getRepository ( PostsTags ) ;
7080
7181 // get current post tags
@@ -77,15 +87,15 @@ export default class PostsTags {
7787
7888 const normalized = {
7989 prev : normalize ( prevPostTags , postTag => postTag . fk_tag_id ) ,
80- current : normalize ( tags )
90+ current : normalize ( uniqueTags )
8191 } ;
8292
8393 // removes tags that are missing
8494 const missing = prevPostTags . filter ( postTag => ! normalized . current [ postTag . fk_tag_id ] ) ;
8595 missing . forEach ( tag => repo . remove ( tag ) ) ;
8696
8797 // adds tags that are new
88- const tagsToAdd = tags . filter ( tag => ! normalized . prev [ tag . id ] ) ;
98+ const tagsToAdd = uniqueTags . filter ( tag => ! normalized . prev [ tag . id ] ) ;
8999 const postTags = tagsToAdd . map ( tag => {
90100 const postTag = new PostsTags ( ) ;
91101 postTag . fk_post_id = postId ;
You can’t perform that action at this time.
0 commit comments