Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Loading State and Optionally Disable Submit Button in MailingList Component #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/components/MailingList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,21 @@ const Button = styled.button`
&:active {
opacity: 0.7;
}

&:disabled {
background-color: #d3d3d3;
color: #a8a8a8;
border-color: #c0c0c0;
cursor: not-allowed;
}
`;

export default function MailingList() {
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [email, setEmail] = useState('');
const [success, setSuccess] = useState('');
const [loading, setLoading] = useState(false);

const api = 'https://api.dilanxd.com/wildhacks/subscribe';
const form = useRef();
Expand All @@ -128,6 +136,7 @@ export default function MailingList() {

const subscribe = (e) => {
e.preventDefault();
setLoading(true);

const requestOptions = {
method: 'POST',
Expand All @@ -145,10 +154,12 @@ export default function MailingList() {
.then((data) => {
console.log(data);
setSuccess(data.success);
setLoading(false);
})
.catch((error) => {
console.log(error);
setSuccess('error');
setLoading(false);
});
};

Expand Down Expand Up @@ -201,7 +212,7 @@ export default function MailingList() {
required
/>
</InputContainer>
<Button className="hide" type="submit" value="Send">
<Button className="hide" type="submit" value="Send" disabled={loading}>
Join
</Button>
</Form>
Expand Down