Skip to content

Commit

Permalink
Merge pull request stakwork#754 from stakwork/update/connection_code
Browse files Browse the repository at this point in the history
Update connection code
  • Loading branch information
humansinstitute authored Dec 13, 2024
2 parents 0064230 + aa26f68 commit a6b6805
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 20 deletions.
108 changes: 90 additions & 18 deletions src/pages/superadmin/header/InviteModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react';
import { nonWidgetConfigs } from 'people/utils/Constants';
import { useIsMobile } from 'hooks/uiHooks';
import { InvoiceForm, InvoiceInput, InvoiceLabel } from 'people/utils/style';
import { InvoiceForm, InvoiceInput, InvoiceInputContainer, InvoiceLabel } from 'people/utils/style';
import styled from 'styled-components';
import { BudgetButton } from 'people/widgetViews/workspace/style';
import { useStores } from 'store';
Expand Down Expand Up @@ -32,15 +32,48 @@ const InviteModal = (props: InviteProps) => {
const { open, close, addToast } = props;
const [loading, setLoading] = useState(false);
const [inviteNumber, setInviteNumber] = useState(1);
const [satAmount, setSatAmount] = useState(1);
const [pubkey, setPubkey] = useState('');
const config = nonWidgetConfigs['workspaceusers'];

const parsePubkey = (pubkey: string): { parsedPubkey: string; routeHint: string } => {
if (pubkey) {
const result = pubkey.split('_');
if (result.length === 3) {
return { parsedPubkey: result[0], routeHint: `${result[1]}_${result[2]}` };
}
}
return { parsedPubkey: '', routeHint: '' };
};

const createConnectionCode = async () => {
setLoading(true);
const status = await main.createConnectionCodes(inviteNumber);
let extractedPubkey = '';
let parsedRouteHint = '';

if (pubkey) {
const { parsedPubkey, routeHint } = parsePubkey(pubkey);
if (!parsedPubkey && !routeHint) {
setLoading(false);
if (addToast) addToast('Invalid Pubkey', 'error');
return;
}
extractedPubkey = parsedPubkey;
parsedRouteHint = routeHint;
}

const status = await main.createConnectionCodes({
users_number: inviteNumber,
sats_amount: satAmount,
...(extractedPubkey &&
parsedRouteHint && { pubkey: extractedPubkey, route_hint: parsedRouteHint })
});

if (status === 200) {
if (addToast) addToast('Users invite code created successfully', 'success');
setInviteNumber(1);
setPubkey('');
setSatAmount(1);
close();
} else {
if (addToast) addToast('Could not create users invite code', 'error');
Expand Down Expand Up @@ -80,22 +113,61 @@ const InviteModal = (props: InviteProps) => {
<Wrapper>
<WithdrawModalTitle className="withdraw-title">Invite Users</WithdrawModalTitle>
<InvoiceForm>
<InvoiceLabel
style={{
display: 'block'
}}
>
Number of users
</InvoiceLabel>
<InvoiceInput
data-testid="withdrawInvoiceInput"
type="text"
style={{
width: '100%'
}}
value={inviteNumber}
onChange={(e: any) => setInviteNumber(Number(e.target.value))}
/>
<InvoiceInputContainer>
<InvoiceLabel
style={{
display: 'block'
}}
>
Number of users
</InvoiceLabel>
<InvoiceInput
data-testid="withdrawInvoiceInput"
type="text"
style={{
width: '100%'
}}
value={inviteNumber}
onChange={(e: any) => setInviteNumber(Number(e.target.value))}
/>
</InvoiceInputContainer>

<InvoiceInputContainer>
<InvoiceLabel
style={{
display: 'block'
}}
>
Pubkey
</InvoiceLabel>
<InvoiceInput
data-testid="withdrawInvoiceInput"
type="text"
style={{
width: '100%'
}}
value={pubkey}
onChange={(e: any) => setPubkey(e.target.value)}
/>
</InvoiceInputContainer>
<InvoiceInputContainer>
<InvoiceLabel
style={{
display: 'block'
}}
>
Sats Amount
</InvoiceLabel>
<InvoiceInput
data-testid="withdrawInvoiceInput"
type="number"
style={{
width: '100%'
}}
value={satAmount}
onChange={(e: any) => setSatAmount(Number(e.target.value))}
/>
</InvoiceInputContainer>
</InvoiceForm>
<BudgetButton
disabled={false}
Expand Down
5 changes: 5 additions & 0 deletions src/people/utils/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export const InvoiceInput = styled.input`
border-radius: 8px;
border: 0.5px solid black;
`;
export const InvoiceInputContainer = styled.div`
display: flex;
flex-direction: column;
margin-bottom: 1rem;
`;
export const WorkspaceWrap = styled.div`
display: flex;
justify-content: space-between;
Expand Down
17 changes: 15 additions & 2 deletions src/store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3766,13 +3766,26 @@ export class MainStore {
}
}

async createConnectionCodes(users_number: number): Promise<number> {
async createConnectionCodes({
users_number,
sats_amount,
pubkey,
route_hint
}: {
users_number: number;
sats_amount?: number;
pubkey?: string;
route_hint?: string;
}): Promise<number> {
try {
if (!uiStore.meInfo) return 406;
const info = uiStore.meInfo;

const data = {
number: users_number
number: users_number,
pubkey: pubkey,
route_hint,
sats_amount
};

const body = JSON.stringify(data);
Expand Down

0 comments on commit a6b6805

Please sign in to comment.