Skip to content

Commit 8c49bf8

Browse files
committed
add loading state for create button and export csv
1 parent 0f1bbc3 commit 8c49bf8

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

components/Paybutton/PaybuttonForm.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import style from './paybutton.module.css'
77
import Plus from 'assets/plus.png'
88

99
interface IProps {
10-
onSubmit: Function
10+
onSubmit: (data: PaybuttonPOSTParameters) => Promise<void>
1111
paybuttons: []
1212
wallets: WalletWithAddressesWithPaybuttons[]
1313
error: String
@@ -16,6 +16,7 @@ interface IProps {
1616
export default function PaybuttonForm ({ onSubmit, paybuttons, wallets, error }: IProps): ReactElement {
1717
const { register, handleSubmit, reset } = useForm<PaybuttonPOSTParameters>()
1818
const [modal, setModal] = useState(false)
19+
const [loading, setLoading] = useState(false)
1920

2021
useEffect(() => {
2122
setModal(false)
@@ -28,6 +29,16 @@ export default function PaybuttonForm ({ onSubmit, paybuttons, wallets, error }:
2829
label: wallet.name
2930
}
3031
})
32+
33+
const handleFormSubmit = async (data: PaybuttonPOSTParameters): Promise<void> => {
34+
setLoading(true)
35+
try {
36+
await onSubmit(data)
37+
} finally {
38+
setLoading(false)
39+
}
40+
}
41+
3142
return (
3243
<>
3344
<div className={style.create_button_ctn}>
@@ -42,7 +53,13 @@ export default function PaybuttonForm ({ onSubmit, paybuttons, wallets, error }:
4253
<div className={style.form_ctn_inner}>
4354
<h4>Create Button</h4>
4455
<div className={style.form_ctn}>
45-
<form onSubmit={(e) => { void handleSubmit(onSubmit)(e) }} method='post'>
56+
<form
57+
onSubmit={(e) => {
58+
e.preventDefault()
59+
void handleSubmit(handleFormSubmit)()
60+
}}
61+
method="post"
62+
>
4663
<label htmlFor='name'>Name*</label>
4764
<input {...register('name')} type='text' id='name' name='name' placeholder="The unique name of your button" required />
4865
<label htmlFor='wallet'>Wallet*</label>
@@ -69,7 +86,7 @@ export default function PaybuttonForm ({ onSubmit, paybuttons, wallets, error }:
6986
<textarea {...register('description')} id='description' name='description' placeholder="More information about your button (optional)"/>
7087
<div className={style.btn_row}>
7188
{error !== '' && <div className={style.error_message}>{error}</div>}
72-
<button type='submit' className='button_main'>Submit</button>
89+
<button type='submit' className='button_main' disabled={loading}>{loading ? 'Loading...' : 'Submit'}</button>
7390
<button onClick={() => { setModal(false); reset() }} className='button_outline'>Cancel</button>
7491
</div>
7592
</form>

pages/button/[id].tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default function Button (props: PaybuttonProps): React.ReactElement {
5959
const userProfile = props.userProfile
6060
const timezone = userProfile?.preferredTimezone === '' ? moment.tz.guess() : userProfile.preferredTimezone
6161
const router = useRouter()
62+
const [loading, setLoading] = useState(false)
6263

6364
const updateIsSyncing = (addressStringList: string[]): void => {
6465
const newIsSyncing = { ...isSyncing }
@@ -118,6 +119,7 @@ export default function Button (props: PaybuttonProps): React.ReactElement {
118119

119120
const downloadCSV = async (paybutton: { id: string, name: string }, currency: string): Promise<void> => {
120121
try {
122+
setLoading(true)
121123
const preferredCurrencyId = userProfile?.preferredCurrencyId ?? ''
122124
let url = `/api/paybutton/download/transactions/${paybutton.id}?currency=${preferredCurrencyId}`
123125
const isCurrencyEmptyOrUndefined = (value: string): boolean => (value === '' || value === undefined)
@@ -148,6 +150,7 @@ export default function Button (props: PaybuttonProps): React.ReactElement {
148150
} catch (error) {
149151
console.error('An error occurred while downloading the CSV:', error)
150152
} finally {
153+
setLoading(false)
151154
setSelectedCurrency('')
152155
}
153156
}
@@ -173,30 +176,32 @@ export default function Button (props: PaybuttonProps): React.ReactElement {
173176
id='export-btn'
174177
value={selectedCurrency}
175178
onChange={handleExport}
179+
disabled={loading}
176180
className="button_outline button_small"
177181
style={{ marginBottom: '0', cursor: 'pointer' }}
178182
>
179-
<option value='' disabled> Export as CSV</option>
183+
<option value='' disabled>{loading ? 'Downloading...' : 'Export as CSV'}</option>
180184
<option key="all" value="all">
181-
All Currencies
185+
{loading ? 'Downloading...' : 'All Currencies'}
182186
</option>
183187
{Object.entries(NETWORK_TICKERS_FROM_ID)
184188
.filter(([id]) => paybuttonNetworks.includes(Number(id)))
185189
.map(([id, ticker]) => (
186190
<option key={id} value={ticker}>
187-
{ticker.toUpperCase()}
191+
{loading ? 'Downloading...' : ticker.toUpperCase()}
188192
</option>
189193
))}
190194
</select>
191195
)
192196
: (
193-
<div
197+
<button
194198
onClick={handleExport}
199+
disabled={loading}
195200
className="button_outline button_small"
196201
style={{ marginBottom: '0', cursor: 'pointer' }}
197202
>
198-
Export as CSV
199-
</div>
203+
{loading ? 'Downloading...' : 'Export as CSV'}
204+
</button>
200205
)}
201206
</div>
202207
</div>

styles/global.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,14 @@ body[data-theme='dark'] .button_main {
388388
color: #231f20 !important;
389389
}
390390

391+
.button_outline:disabled {
392+
background-color: var(--secondary-bg-color) !important;
393+
color: var(--primary-text-color) !important;
394+
border: none !important;
395+
opacity: 0.6;
396+
cursor: not-allowed !important;
397+
}
398+
391399
body[data-theme='dark'] .button_outline:hover {
392400
border-color: #000;
393401
}

0 commit comments

Comments
 (0)