Skip to content

Commit dc4c0c6

Browse files
committed
Add setup form for Last.FM API key and username in TopCharts block
1 parent 7702fe8 commit dc4c0c6

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

packages/block-library/top-charts/edit.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ import { useEntityProp } from '@wordpress/core-data';
1111
import { useEffect, useState } from '@wordpress/element';
1212
import apiFetch from '@wordpress/api-fetch';
1313
import { addQueryArgs } from '@wordpress/url';
14+
import {
15+
Button,
16+
ExternalLink,
17+
Placeholder,
18+
__experimentalHStack as HStack,
19+
__experimentalInputControl as InputControl,
20+
} from '@wordpress/components';
1421

1522
const Edit = ( {
1623
attributes: { collection },
1724
} ) => {
1825
const [ apiKey, setApiKey ] = useEntityProp( 'root', 'site', 'profile_blocks_lastfm_api_key' );
1926
const [ defaultProfile, setDefaultProfile ] = useEntityProp( 'root', 'site', 'profile_blocks_lastfm_profile' );
2027

28+
const [ showSetup, setShowSetup ] = useState( !! apiKey );
29+
2130
const [ items, setItems ] = useState( [] );
2231

2332
useEffect( () => {
@@ -48,6 +57,53 @@ const Edit = ( {
4857
const blockProps = useBlockProps();
4958
const innerBlockProps = useInnerBlocksProps( blockProps );
5059

60+
if ( showSetup ) {
61+
const onSubmit = ( event ) => {
62+
event.preventDefault();
63+
setShowSetup( false );
64+
};
65+
66+
return (
67+
<div { ...blockProps }>
68+
<Placeholder
69+
label={ __( 'Profile Blocks for Last.FM', 'profile-blocks-lastfm' ) }
70+
icon={ null }
71+
instructions={ __( 'Enter your Last.FM API key and default username. You can override the username for each block.', 'profile-blocks-lastfm' ) }
72+
>
73+
<form onSubmit={ onSubmit }>
74+
<HStack align="end">
75+
<InputControl
76+
__next40pxDefaultSize
77+
value={ apiKey || '' }
78+
className="wp-block-profile-blocks-lastfm-top-charts__placeholder-input"
79+
label={ __( 'API Key', 'profile-blocks-lastfm' ) }
80+
placeholder={ __( 'Enter your Last.fm API key', 'profile-blocks-lastfm' ) }
81+
onChange={ ( value ) => setApiKey( value ) }
82+
type="password"
83+
required
84+
/>
85+
<InputControl
86+
__next40pxDefaultSize
87+
value={ defaultProfile || '' }
88+
label={ __( 'Username', 'profile-blocks-lastfm' ) }
89+
placeholder={ __( 'Enter your Last.fm username', 'profile-blocks-lastfm' ) }
90+
onChange={ ( value ) => setDefaultProfile( value ) }
91+
required
92+
/>
93+
<Button __next40pxDefaultSize variant="primary" type="submit">
94+
{ __( 'Apply', 'profile-blocks-lastfm' ) }
95+
</Button>
96+
</HStack>
97+
</form>
98+
99+
<ExternalLink href="https://www.last.fm/api/authentication" rel="noreferrer noopener">
100+
{ __( 'Get your Last.fm API key', 'profile-blocks-lastfm' ) }
101+
</ExternalLink>
102+
</Placeholder>
103+
</div>
104+
);
105+
}
106+
51107
return (
52108
<BlockContextProvider value={ { collection: items } }>
53109
<div { ...innerBlockProps } />
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
.wp-block-profile-blocks-lastfm-top-charts {}
1+
.wp-block-profile-blocks-lastfm-top-charts {
2+
.wp-block-profile-blocks-lastfm-top-charts__placeholder-input {
3+
flex: 1 1 auto;
4+
}
5+
}

0 commit comments

Comments
 (0)