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

Small Invite Flow Bug Fixes #1213

Merged
merged 4 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
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
93 changes: 67 additions & 26 deletions client/scripts/views/modals/create_invite_modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import 'modals/create_invite_modal.scss';
import m from 'mithril';
import $ from 'jquery';
import mixpanel from 'mixpanel-browser';
import { Button, Input, Form, FormGroup, FormLabel, Select } from 'construct-ui';
import { Button, Input, Form, FormGroup, FormLabel, Select, RadioGroup } from 'construct-ui';

import app from 'state';
import { CommunityInfo, ChainInfo, RoleInfo } from 'models';
import { CommunityInfo, ChainInfo, RoleInfo, ChainBase } from 'models';
import { CompactModalExitButton } from 'views/modal';
import { checkAddress } from '@polkadot/util-crypto';
import Web3 from 'web3';

interface IInviteButtonAttrs {
selection: string,
Expand All @@ -18,24 +20,30 @@ interface IInviteButtonAttrs {
invitedAddressChain?: string,
community?: CommunityInfo,
chain?: ChainInfo,
disabled?: boolean
}

const InviteButton: m.Component<IInviteButtonAttrs, { disabled: boolean, }> = {
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}

const InviteButton: m.Component<IInviteButtonAttrs, { loading: boolean, }> = {
oninit: (vnode) => {
vnode.state.disabled = false;
vnode.state.loading = false;
},
view: (vnode) => {
const { selection, successCallback, failureCallback,
invitedAddress, invitedEmail, invitedAddressChain, community, chain } = vnode.attrs;
invitedAddress, invitedEmail, invitedAddressChain, community, chain, disabled } = vnode.attrs;
return m(Button, {
class: 'create-invite-button',
intent: 'primary',
name: selection,
loading: vnode.state.disabled,
loading: vnode.state.loading,
disabled,
rounded: true,
type: 'submit',
label: selection === 'address'
? 'Invite Commonwealth user' : selection === 'email' ? 'Invite email' : 'Add',
label: selection === 'address' || selection === 'email' ? 'Send Invite' : 'Add',
onclick: (e) => {
e.preventDefault();
const address = invitedAddress;
Expand All @@ -46,7 +54,7 @@ const InviteButton: m.Component<IInviteButtonAttrs, { disabled: boolean, }> = {
if (selection === 'address' && (address === '' || address === null)) return;
if (selection === 'email' && (emailAddress === '' || emailAddress === null)) return;

vnode.state.disabled = true;
vnode.state.loading = true;
successCallback(false);
failureCallback(false);

Expand Down Expand Up @@ -75,7 +83,7 @@ const InviteButton: m.Component<IInviteButtonAttrs, { disabled: boolean, }> = {
auth: true,
jwt: app.user.jwt,
}).then((response) => {
vnode.state.disabled = false;
vnode.state.loading = false;
if (response.status === 'Success') {
successCallback(true);
if (postType === '/addMember') {
Expand All @@ -101,7 +109,7 @@ const InviteButton: m.Component<IInviteButtonAttrs, { disabled: boolean, }> = {
});
}, (err) => {
failureCallback(true, err.responseJSON.error);
vnode.state.disabled = false;
vnode.state.loading = false;
m.redraw();
});
}
Expand Down Expand Up @@ -129,36 +137,34 @@ const CreateInviteLink: m.Component<{
? { chain: chain.id }
: { community: community.id };
return m(Form, { class: 'CreateInviteLink' }, [
m(FormGroup, { span: 12 }, [
m('h2.invite-link-title', 'Generate Invite Link'),
]),
m(FormGroup, { span: 4 }, [
m(FormLabel, { for: 'uses', }, 'Generate invite link'),
m(Select, {
m(FormLabel, { for: 'uses', }, 'Number of Uses'),
m(RadioGroup, {
name: 'uses',
defaultValue: vnode.state.inviteUses,
options: [
{ value: 'none', label: 'Unlimited uses' },
{ value: '1', label: 'One time use' },
// { value: '2', label: 'Twice' },
],
onchange: (e) => {
vnode.state.inviteUses = (e.target as any).value;
},
value: vnode.state.inviteUses,
onchange: (e: Event) => { vnode.state.inviteUses = (e.target as any).value; },
}),
]),
m(FormGroup, { span: 4 }, [
m(FormLabel, { for: 'time' }, 'Expires after'),
m(Select, {
m(RadioGroup, {
name: 'time',
defaultValue: vnode.state.inviteTime,
options: [
{ value: 'none', label: 'Never expires' },
{ value: '24h', label: '24 hours' },
{ value: '48h', label: '48 hours' },
{ value: '1w', label: '1 week' },
{ value: '30d', label: '30 days' },
],
onchange: (e) => {
vnode.state.inviteTime = (e.target as any).value;
},
value: vnode.state.inviteTime,
onchange: (e: Event) => { vnode.state.inviteTime = (e.target as any).value; },
}),
]),
m(FormGroup, { span: 4 }),
Expand Down Expand Up @@ -187,14 +193,27 @@ const CreateInviteLink: m.Component<{
label: 'Get invite link'
}),
]),
m(FormGroup, { span: 8 }, [
m(FormGroup, { span: 8, class: 'copy-link-line' }, [
m(Input, {
id: 'invite-link-pastebin',
class: 'invite-link-pastebin',
fluid: true,
readonly: true,
placeholder: 'Click to generate a link',
value: `${vnode.state.link}`,
}),
m('img', {
src: 'static/img/copy_default.svg',
alt: '',
class: 'mx-auto',
onclick: (e) => {
const copyText = document.getElementById('invite-link-pastebin') as HTMLInputElement;
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */

document.execCommand('copy');
}
})
]),
]);
}
Expand Down Expand Up @@ -225,6 +244,21 @@ const CreateInviteModal: m.Component<{
: communityInfo ? { community: communityInfo }
: null;
if (!chainOrCommunityObj) return;

const selectedChainId = vnode.state.invitedAddressChain || (chainInfo ? chainInfo.id : app.config.chains.getAll()[0].id);
const selectedChain = app.config.chains.getById(selectedChainId);
let isAddressValid = true;

if (selectedChain?.base === ChainBase.Substrate) {
[isAddressValid] = checkAddress(vnode.state.invitedAddress, parseInt(selectedChain.ss58Prefix, 10));
} else if (chainInfo?.base === ChainBase.Ethereum) {
isAddressValid = Web3.utils.checkAddressChecksum(vnode.state.invitedAddress);
} else {
// TODO: check Cosmos & Near?
}

const isEmailValid = validateEmail(vnode.state.invitedEmail);

return m('.CreateInviteModal', [
m('.compact-modal-title', [
m('h3', 'Invite members'),
Expand Down Expand Up @@ -258,16 +292,19 @@ const CreateInviteModal: m.Component<{
fluid: true,
name: 'address',
autocomplete: 'off',
placeholder: 'Address',
placeholder: 'Paste address',
class: !vnode.state.invitedAddress?.length ? '' : isAddressValid ? 'valid' : 'invalid',
oninput: (e) => {
vnode.state.invitedAddress = (e.target as any).value;
}
}),
]),
m(InviteButton, {
selection: 'address',
disabled: !isAddressValid,
successCallback: (v: boolean) => {
vnode.state.success = v;
vnode.state.invitedAddress = '';
m.redraw();
},
failureCallback: (v: boolean, err?: string,) => {
Expand All @@ -287,16 +324,19 @@ const CreateInviteModal: m.Component<{
fluid: true,
name: 'emailAddress',
autocomplete: 'off',
placeholder: 'satoshi@protonmail.com',
placeholder: 'Enter email',
class: !vnode.state.invitedEmail?.length ? '' : isEmailValid ? 'valid' : 'invalid',
oninput: (e) => {
vnode.state.invitedEmail = (e.target as any).value;
}
}),
]),
m(InviteButton, {
selection: 'email',
disabled: !isEmailValid,
successCallback: (v: boolean) => {
vnode.state.success = v;
vnode.state.invitedEmail = '';
m.redraw();
},
failureCallback: (v: boolean, err?: string,) => {
Expand All @@ -308,6 +348,7 @@ const CreateInviteModal: m.Component<{
...chainOrCommunityObj
}),
]),
m('div.divider'),
m(CreateInviteLink, { ...chainOrCommunityObj }),
vnode.state.success && m('.success-message', [
'Success! Your invite was sent',
Expand Down
96 changes: 76 additions & 20 deletions client/styles/modals/create_invite_modal.scss
Original file line number Diff line number Diff line change
@@ -1,28 +1,84 @@
@import 'client/styles/shared';
@import "client/styles/shared";

.CreateInviteModal {
form {
margin: 0;
max-width: 560px;
h4 {
margin-top: 0;
margin-bottom: 16px;
}
form {
margin: 0;
max-width: 560px;
h4 {
margin-top: 0;
margin-bottom: 16px;
}
.chainSelectLabel {
display: block;
}
.chainSelectLabel {
display: block;
}
form + form {
margin-top: 40px;
}
.valid input {
border: solid 1px green;
}
.invalid input {
border: solid 1px red;
}
.error-message {
margin: 10px 0;
font-weight: 500;
color: $negative-bg-color;
}
.success-message {
margin: 10px 0;
font-weight: 500;
color: $positive-bg-color;
}
.copy-link-line {
display: flex;
align-items: center;
height: 40px;

img {
width: 30px;
margin-left: 10px;
content: url("/static/img/copy_default.svg");
cursor: pointer;

&:hover {
content: url("/static/img/copy_hover.svg");
}
}
form + form {
margin-top: 40px;
}

.divider {
width: calc(100% + 56px);
height: 1px;
background: #ddd;
margin-top: 20px;
margin-left: -28px;
}

.CreateInviteLink {
margin-top: 30px;
.invite-link-title {
font-size: 24px;
line-height: 28.8px;
font-weight: 500;
}
.error-message {
margin: 10px 0;
font-weight: 500;
color: $negative-bg-color;
.cui-form-label {
font-size: 18px;
line-height: 21.6px;
font-weight: 500;
}
.success-message {
margin: 10px 0;
font-weight: 500;
color: $positive-bg-color;

.cui-radio {
font-size: 18px;
line-height: 21.6px;
font-weight: 400;
padding-left: 24px;

.cui-control-indicator {
width: 16px;
height: 16px;
}
}
}
}
4 changes: 4 additions & 0 deletions static/img/copy_default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions static/img/copy_hover.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.