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' ;
2
9
import { EmptyReactions , reactionEmoji , reactionNames } from './reactions' ;
10
+ import { pageAttributes as page } from './page-attributes' ;
3
11
4
12
export class PostReactionComponent {
5
13
public readonly element : HTMLElement ;
@@ -11,7 +19,7 @@ export class PostReactionComponent {
11
19
12
20
constructor (
13
21
private issue : Issue | null ,
14
- private createIssue : ( ) => Promise < null >
22
+ private createIssueCallback : ( issue : Issue ) => Promise < null >
15
23
) {
16
24
this . element = document . createElement ( 'section' ) ;
17
25
this . element . classList . add ( 'post-reactions' ) ;
@@ -41,22 +49,33 @@ export class PostReactionComponent {
41
49
const handler = async ( event : Event ) => {
42
50
event . preventDefault ( ) ;
43
51
52
+ const button = event . target as HTMLButtonElement ;
53
+ button . disabled = true ;
54
+ const id = button . value as ReactionID ;
44
55
const issueExists = ! ! this . issue ;
56
+
45
57
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 ) ;
47
69
}
48
70
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 ;
53
72
const { deleted} = await toggleReaction ( url , id ) ;
54
73
const delta = deleted ? - 1 : 1 ;
55
74
this . reactions [ id ] += delta ;
56
75
this . reactions . total_count += delta ;
57
76
this . issue ! . reactions = this . reactions ;
58
- this . setIssue ( this . issue ) ;
59
77
button . disabled = false ;
78
+ this . setIssue ( this . issue ) ;
60
79
}
61
80
62
81
const buttons = this . element . querySelectorAll ( 'button[post-reaction]' ) ;
0 commit comments