@monygroupcorp/micro-web3 1.2.4 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,9 @@
1
- import { Component } from '@monygroupcorp/microact';
1
+ import { Component, h } from '@monygroupcorp/microact';
2
2
 
3
3
  export class TransactionOptions extends Component {
4
4
  constructor(props) {
5
5
  super(props);
6
- this.eventBus = props.eventBus;
7
-
6
+
8
7
  this.state = {
9
8
  nftMintingEnabled: props.nftMintingEnabled || false,
10
9
  message: props.message || '',
@@ -15,79 +14,30 @@ export class TransactionOptions extends Component {
15
14
  isPhase2: props.isPhase2 === null ? null : props.isPhase2
16
15
  };
17
16
 
18
- this.handleMessageInput = this.handleMessageInput.bind(this);
19
- this.handleNFTToggle = this.handleNFTToggle.bind(this);
20
- this.handleStoreUpdate = this.handleStoreUpdate.bind(this);
21
-
22
17
  this.updateTimer = null;
23
18
  }
24
19
 
25
- onMount() {
26
- this.contractDataUnsubscribe = this.eventBus.on('contractData:updated', (data) => {
27
- this.updateProps({ isPhase2: data.liquidityPool && data.liquidityPool !== '0x0000000000000000000000000000000000000000' });
20
+ didMount() {
21
+ this.subscribe('contractData:updated', (data) => {
22
+ const isPhase2 = data.liquidityPool && data.liquidityPool !== '0x0000000000000000000000000000000000000000';
23
+ if (this.state.isPhase2 !== isPhase2) {
24
+ this.setState({ isPhase2 });
25
+ }
28
26
  });
29
-
30
- this.addEventListeners();
31
- this.updateElements();
32
- }
33
-
34
- onUnmount() {
35
- if (this.contractDataUnsubscribe) this.contractDataUnsubscribe();
36
- this.removeEventListeners();
37
- }
38
-
39
- updateProps(newProps) {
40
- const newState = { ...this.state, ...newProps };
41
- const shouldUpdate = this.state.isPhase2 !== newState.isPhase2 ||
42
- this.state.swapDirection !== newState.swapDirection ||
43
- this.state.nftBalance !== newState.nftBalance;
44
-
45
- this.setState(newState);
46
-
47
- if (shouldUpdate) {
48
- this.validateTransaction();
49
- this.update(); // Re-render if phase or other critical props change
50
- } else {
51
- this.updateElements(); // Just update dynamic parts
52
- }
53
- }
54
-
55
- addEventListeners() {
56
- const messageInput = this.element.querySelector('#messageInput');
57
- const nftToggle = this.element.querySelector('#nftToggle');
58
-
59
- if (messageInput) {
60
- this._messageInput = messageInput;
61
- messageInput.addEventListener('input', this.handleMessageInput);
62
- }
63
- if (nftToggle) {
64
- this._nftToggle = nftToggle;
65
- nftToggle.addEventListener('change', this.handleNFTToggle);
66
- }
67
- }
68
-
69
- removeEventListeners() {
70
-
71
- if (this._messageInput) {
72
- this._messageInput.removeEventListener('input', this.handleMessageInput);
73
- }
74
-
75
- if (this._nftToggle) {
76
- this._nftToggle.removeEventListener('change', this.handleNFTToggle);
77
- }
78
27
  }
79
28
 
80
29
  validateTransaction() {
81
30
  const isMessageValid = this.state.message.length <= 140;
82
31
  const isNFTValid = !this.state.nftMintingEnabled || this.state.nftBalance < 10;
83
32
  const newIsValid = isMessageValid && isNFTValid;
84
-
85
- // Only emit if validation state has changed
33
+
86
34
  if (this.state.lastValidationState !== newIsValid) {
87
- this.state.lastValidationState = newIsValid;
88
- this.state.isValid = newIsValid;
89
-
90
- this.eventBus.emit('transactionValidation', {
35
+ this.setState({
36
+ lastValidationState: newIsValid,
37
+ isValid: newIsValid
38
+ });
39
+
40
+ this.props.eventBus?.emit('transactionValidation', {
91
41
  isValid: newIsValid,
92
42
  errors: this.getValidationErrors()
93
43
  });
@@ -96,11 +46,11 @@ export class TransactionOptions extends Component {
96
46
 
97
47
  getValidationErrors() {
98
48
  const errors = [];
99
-
49
+
100
50
  if (this.state.message.length > 140) {
101
51
  errors.push('Message must be 140 characters or less');
102
52
  }
103
-
53
+
104
54
  if (this.state.nftMintingEnabled && this.state.nftBalance >= 10) {
105
55
  errors.push('Cannot mint more than 10 NFTs');
106
56
  }
@@ -108,131 +58,72 @@ export class TransactionOptions extends Component {
108
58
  return errors;
109
59
  }
110
60
 
111
- updateElements() {
112
-
113
- // Update message input if it's not focused
114
- const messageInput = this.element.querySelector('#messageInput');
115
- const nftToggle = this.element.querySelector('#nftToggle');
116
-
117
- if (messageInput && !messageInput.matches(':focus')) {
118
- messageInput.value = this.state.message;
119
- }
120
-
121
- if (nftToggle) {
122
- nftToggle.checked = this.state.nftMintingEnabled;
123
- }
124
-
125
- // Re-attach event listeners after updating elements
126
- this.removeEventListeners();
127
- this.addEventListeners();
128
-
129
- // Update character count
130
- const characterCount = this.element.querySelector('.character-count');
131
- if (characterCount) {
132
- characterCount.textContent = `${this.state.message.length}/140`;
133
- characterCount.className = `character-count ${this.state.message.length > 140 ? 'error' : ''}`;
134
- }
135
-
136
- // Update validation status
137
- const validationStatus = this.element.querySelector('.validation-status');
138
- if (validationStatus) {
139
- validationStatus.className = `validation-status ${this.state.isValid ? 'valid' : 'invalid'}`;
140
- validationStatus.innerHTML = this.getValidationErrors()
141
- .map(error => `<p class="error">${error}</p>`)
142
- .join('');
143
- }
144
- }
145
-
146
- handleMessageInput(event) {
147
- const newMessage = event.target.value || '';
148
-
149
- // Update state without triggering a re-render
150
- this.state = {
151
- ...this.state,
152
- message: newMessage
153
- };
154
-
155
- // Update elements and ensure listeners
156
- this.updateElements();
157
-
61
+ handleMessageInput(e) {
62
+ const newMessage = e.target.value || '';
63
+ this.setState({ message: newMessage });
64
+ this.validateTransaction();
158
65
  this.emitStateUpdate();
159
66
  }
160
67
 
161
- handleNFTToggle(event) {
162
- const isEnabled = event.target.checked;
163
-
164
- // Update state without triggering a re-render
165
- this.state = {
166
- ...this.state,
167
- nftMintingEnabled: isEnabled
168
- };
169
-
170
- // Update elements and ensure listeners
171
- this.updateElements();
172
-
68
+ handleNFTToggle(e) {
69
+ const isEnabled = e.target.checked;
70
+ this.setState({ nftMintingEnabled: isEnabled });
71
+ this.validateTransaction();
173
72
  this.emitStateUpdate();
174
73
  }
175
74
 
176
- // New method to emit state updates
177
75
  emitStateUpdate() {
178
76
  const updatePayload = {
179
77
  message: this.state.message,
180
78
  nftMintingEnabled: this.state.nftMintingEnabled
181
79
  };
182
- this.eventBus.emit('transactionOptions:update', updatePayload);
183
- }
184
-
185
- checkPhase2Status() {
186
- const { isPhase2 } = this.state;
187
- if (isPhase2 === null) {
188
- // Phase not yet determined
189
- return;
190
- }
191
- if (this.state.isPhase2 !== isPhase2) {
192
- this.setState({ isPhase2 });
193
- }
80
+ this.props.eventBus?.emit('transactionOptions:update', updatePayload);
194
81
  }
195
82
 
196
- template() {
83
+ render() {
197
84
  const { nftMintingEnabled, message, isValid, isPhase2, swapDirection } = this.state;
198
85
 
199
- if (isPhase2 === null) {
200
- return '';
201
- }
202
- if (isPhase2) {
203
- return ''; // No options in Phase 2
204
- }
205
-
206
- return `
207
- <div class="transaction-options">
208
- <div class="option-group ${swapDirection === 'sell' ? 'hidden' : ''}">
209
- <label class="nft-toggle">
210
- <input type="checkbox"
211
- ${nftMintingEnabled ? 'checked' : ''}
212
- id="nftToggle">
213
- Mint NFT with transaction
214
- </label>
215
- </div>
216
-
217
- <div class="option-group">
218
- <label for="messageInput">Transaction Message</label>
219
- <textarea id="messageInput"
220
- maxlength="140"
221
- placeholder="Enter optional message..."
222
- >${message}</textarea>
223
- <span class="character-count ${message.length > 140 ? 'error' : ''}">
224
- ${message.length}/140
225
- </span>
226
- </div>
227
-
228
- <div class="validation-status ${isValid ? 'valid' : 'invalid'}">
229
- ${this.getValidationErrors().map(error => `<p class="error">${error}</p>`).join('')}
230
- </div>
231
- </div>
232
- `;
233
- }
234
-
235
- render() {
236
- return this.template();
86
+ if (isPhase2 === null || isPhase2) {
87
+ return h('div', { className: 'transaction-options empty' });
88
+ }
89
+
90
+ const errors = this.getValidationErrors();
91
+
92
+ return h('div', { className: 'transaction-options' },
93
+ h('div', {
94
+ className: `option-group ${swapDirection === 'sell' ? 'hidden' : ''}`
95
+ },
96
+ h('label', { className: 'nft-toggle' },
97
+ h('input', {
98
+ type: 'checkbox',
99
+ checked: nftMintingEnabled,
100
+ onChange: this.bind(this.handleNFTToggle)
101
+ }),
102
+ 'Mint NFT with transaction'
103
+ )
104
+ ),
105
+
106
+ h('div', { className: 'option-group' },
107
+ h('label', { htmlFor: 'messageInput' }, 'Transaction Message'),
108
+ h('textarea', {
109
+ id: 'messageInput',
110
+ maxLength: 140,
111
+ placeholder: 'Enter optional message...',
112
+ value: message,
113
+ onInput: this.bind(this.handleMessageInput)
114
+ }),
115
+ h('span', {
116
+ className: `character-count ${message.length > 140 ? 'error' : ''}`
117
+ }, `${message.length}/140`)
118
+ ),
119
+
120
+ h('div', {
121
+ className: `validation-status ${isValid ? 'valid' : 'invalid'}`
122
+ },
123
+ errors.map((error, index) =>
124
+ h('p', { key: index, className: 'error' }, error)
125
+ )
126
+ )
127
+ );
237
128
  }
238
- }
129
+ }