1- import { Issue , ReactionID , Reactions , reactionTypes , toggleReaction } from './github' ;
1+ import {
2+ createIssue as createGitHubIssue ,
3+ Issue , loadIssueByNumber ,
4+ ReactionID ,
5+ Reactions ,
6+ reactionTypes ,
7+ toggleReaction
8+ } from './github' ;
29import { EmptyReactions , reactionEmoji , reactionNames } from './reactions' ;
10+ import { pageAttributes as page } from './page-attributes' ;
311
412export class PostReactionComponent {
513 public readonly element : HTMLElement ;
@@ -11,7 +19,7 @@ export class PostReactionComponent {
1119
1220 constructor (
1321 private issue : Issue | null ,
14- private createIssue : ( ) => Promise < null >
22+ private createIssueCallback : ( issue : Issue ) => Promise < null >
1523 ) {
1624 this . element = document . createElement ( 'section' ) ;
1725 this . element . classList . add ( 'post-reactions' ) ;
@@ -41,22 +49,33 @@ export class PostReactionComponent {
4149 const handler = async ( event : Event ) => {
4250 event . preventDefault ( ) ;
4351
52+ const button = event . target as HTMLButtonElement ;
53+ button . disabled = true ;
54+ const id = button . value as ReactionID ;
4455 const issueExists = ! ! this . issue ;
56+
4557 if ( ! issueExists ) {
46- await this . createIssue ( ) ;
58+ const newIssue = await createGitHubIssue (
59+ page . issueTerm as string ,
60+ page . url ,
61+ page . title ,
62+ page . description || '' ,
63+ page . label
64+ ) ;
65+ const issue = await loadIssueByNumber ( newIssue . number ) ;
66+ this . issue = issue ;
67+ this . reactions = issue . reactions ;
68+ await this . createIssueCallback ( issue ) ;
4769 }
4870
49- const button = event . target as HTMLButtonElement ;
50- button . disabled = true ;
51- const url = button . formAction ;
52- const id = button . value as ReactionID ;
71+ const url = this . reactions . url ;
5372 const { deleted} = await toggleReaction ( url , id ) ;
5473 const delta = deleted ? - 1 : 1 ;
5574 this . reactions [ id ] += delta ;
5675 this . reactions . total_count += delta ;
5776 this . issue ! . reactions = this . reactions ;
58- this . setIssue ( this . issue ) ;
5977 button . disabled = false ;
78+ this . setIssue ( this . issue ) ;
6079 }
6180
6281 const buttons = this . element . querySelectorAll ( 'button[post-reaction]' ) ;
0 commit comments