Added opt-in option for promotional emails

This commit is contained in:
2021-02-11 16:01:39 +11:00
parent 7759a1cd40
commit 615b686890
19 changed files with 848 additions and 83 deletions
+43 -2
View File
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useEffect } from 'react';
import { Redirect } from 'react-router-dom';
import { useCookies } from 'react-cookie';
@@ -12,12 +12,53 @@ const Account = props => {
return <Redirect to='/' />;
}
//refs
let contactElement;
//once before render
useEffect(() => {
fetch('/api/accounts')
.then(blob => blob.json())
.then(json => {
contactElement.checked = json.contact;
})
.catch(e => console.error(e))
;
}, []);
return (
<div className='page'>
<h1 className='centered'>Account</h1>
<DeleteAccount />
<form className='constricted' onSubmit={async evt => {
evt.preventDefault();
await update(contactElement.checked);
}}>
<div>
<label htmlFor='contact'>Allow Promotional Emails:</label>
<input type='checkbox' name='contact' ref={e => contactElement = e} />
</div>
<button type='submit'>Update Information</button>
</form>
<DeleteAccount className='constricted' />
</div>
);
};
const update = async (contact) => {
//generate a new formdata payload
let formData = new FormData();
formData.append('contact', contact);
const result = await fetch('/api/accounts', { method: 'PATCH', body: formData });
if (result.ok) {
alert(await result.text());
} else {
alert(await result.text());
}
}
export default Account;