-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from bitcoin-sv/feat-finalize-contacts
feat(SPV-679): finalize contacts
- Loading branch information
Showing
31 changed files
with
572 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,50 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { timeoutPromise } from '@/utils/timeoutPromise'; | ||
import { PaginationParams } from '../types'; | ||
import { Contact } from '../types/contact'; | ||
import { Contact, ContactMetadata } from '../types/contact'; | ||
import axios from 'axios'; | ||
|
||
export const searchContacts = async (_pagination?: PaginationParams) => { | ||
await timeoutPromise(500); | ||
return contacts; | ||
const { data: response } = await axios.get(`/contact/search`); | ||
return response; | ||
}; | ||
|
||
export const addContact = async (paymail: string, name: string) => { | ||
await timeoutPromise(1000); | ||
contacts = [ | ||
...contacts, | ||
{ | ||
paymail, | ||
name, | ||
status: 'awaiting-acceptance', | ||
}, | ||
]; | ||
export const upsertContact = async (paymail: string, fullName: string, metadata?: ContactMetadata) => { | ||
await axios.put(`/contact/${encodeURIPaymail(paymail)}`, { | ||
fullName, | ||
metadata, | ||
}); | ||
}; | ||
|
||
export const rejectContact = async (paymail: string) => { | ||
console.log('reject'); | ||
await timeoutPromise(1000); | ||
contacts = contacts.filter((contact) => contact.paymail !== paymail); | ||
await axios.patch(`/contact/rejected/${encodeURIPaymail(paymail)}`); | ||
}; | ||
|
||
export const acceptContact = async (paymail: string) => { | ||
await timeoutPromise(1000); | ||
contacts = contacts.map((contact) => { | ||
if (contact.paymail === paymail) { | ||
contact.status = 'not-confirmed'; | ||
} | ||
return { ...contact }; | ||
}); | ||
await axios.patch(`/contact/accepted/${encodeURIPaymail(paymail)}`); | ||
}; | ||
|
||
export const getTOTP = async (_paymail: string) => { | ||
await timeoutPromise(1000); | ||
return Math.floor(10 + Math.random() * 89); | ||
export const getTOTP = async (contact: Contact) => { | ||
const res = await axios.post(`/contact/totp`, contact); | ||
return res.data.passcode; | ||
}; | ||
|
||
export const confirmContactWithTOTP = async (paymail: string, _totp: number) => { | ||
await timeoutPromise(1000); | ||
contacts = contacts.map((contact) => { | ||
if (contact.paymail === paymail) { | ||
contact.status = 'confirmed'; | ||
} | ||
return { ...contact }; | ||
export const confirmContactWithTOTP = async (contact: Contact, totp: string) => { | ||
await axios.patch(`/contact/confirmed`, { | ||
passcode: totp, | ||
contact, | ||
}); | ||
}; | ||
|
||
/// Mocked contacts | ||
|
||
let contacts: Contact[] = [ | ||
{ | ||
paymail: '[email protected]', | ||
name: 'Bob', | ||
status: 'confirmed', | ||
}, | ||
{ | ||
paymail: '[email protected]', | ||
name: 'Tester', | ||
status: 'awaiting-acceptance', | ||
}, | ||
{ | ||
paymail: '[email protected]', | ||
name: 'The Guy', | ||
status: 'not-confirmed', | ||
}, | ||
]; | ||
const encodeURIPaymail = (paymail: string) => { | ||
// Remove control characters from the paymail | ||
function* iterator() { | ||
for (let i = 0; i < paymail.length; i++) { | ||
const code = paymail.charCodeAt(i); | ||
if (code > 32 && code !== 127) { | ||
yield code; | ||
} | ||
} | ||
} | ||
const sanitizedPaymail = String.fromCharCode(...iterator()); | ||
return encodeURIComponent(sanitizedPaymail); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
export const ContactAwaitingAcceptance = 'awaiting-acceptance'; | ||
export const ContactNotConfirmed = 'not-confirmed'; | ||
export const ContactAwaitingAcceptance = 'awaiting'; | ||
export const ContactNotConfirmed = 'unconfirmed'; | ||
export const ContactConfirmed = 'confirmed'; | ||
export const ContactRejected = 'rejected'; | ||
|
||
export type ContactStatus = typeof ContactAwaitingAcceptance | typeof ContactNotConfirmed | typeof ContactConfirmed; | ||
export type ContactStatus = | ||
| typeof ContactAwaitingAcceptance | ||
| typeof ContactNotConfirmed | ||
| typeof ContactConfirmed | ||
| typeof ContactRejected; | ||
|
||
export type Contact = { | ||
created_at: string; | ||
updated_at: string; | ||
deleted_at: string; | ||
metadata?: ContactMetadata; | ||
id: string; | ||
pubKey: string; | ||
paymail: string; | ||
name: string; | ||
status: ContactStatus; | ||
fullName: string; | ||
}; | ||
|
||
//TODO: Add more fields (...metadata) | ||
export type ContactMetadata = { | ||
phoneNumber?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 25 additions & 6 deletions
31
src/components/ContactsList/ContactsTable.tsx/StatusBadge.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,38 @@ | ||
import { ContactAwaitingAcceptance, ContactConfirmed, ContactNotConfirmed, ContactStatus } from '@/api/types/contact'; | ||
import { | ||
ContactAwaitingAcceptance, | ||
ContactConfirmed, | ||
ContactNotConfirmed, | ||
ContactStatus, | ||
ContactRejected, | ||
} from '@/api/types/contact'; | ||
import { FC } from 'react'; | ||
import { Chip, ChipProps } from '@mui/material'; | ||
import styled from '@emotion/styled'; | ||
|
||
type StatusBadgeProps = { | ||
status: ContactStatus; | ||
status: ContactStatus | 'unknown'; | ||
}; | ||
|
||
export const StatusBadge: FC<StatusBadgeProps> = ({ status }) => { | ||
const { label, color } = contactStatuses[status]; | ||
export const StatusBadge: FC<StatusBadgeProps & React.HTMLAttributes<HTMLDivElement>> = ({ status, ...props }) => { | ||
const { label, color } = contactStatuses[status] ?? { label: 'Unknown', color: 'error' }; | ||
|
||
return <Chip size="small" label={label} color={color} />; | ||
return ( | ||
<ChipContainer {...props}> | ||
<Chip size="small" label={label} color={color} /> | ||
</ChipContainer> | ||
); | ||
}; | ||
|
||
const contactStatuses: Record<ContactStatus, { label: string; color: ChipProps['color'] }> = { | ||
const contactStatuses: Record<ContactStatus | 'unknown', { label: string; color: ChipProps['color'] }> = { | ||
[ContactAwaitingAcceptance]: { label: 'Pending', color: 'primary' }, | ||
[ContactNotConfirmed]: { label: 'Untrusted', color: 'secondary' }, | ||
[ContactConfirmed]: { label: 'Trusted', color: 'success' }, | ||
[ContactRejected]: { label: 'Rejected', color: 'error' }, | ||
unknown: { label: 'Unknown', color: 'warning' }, | ||
}; | ||
|
||
const ChipContainer = styled.div` | ||
& div span { | ||
line-height: 1; | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.