-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainView.tsx
88 lines (80 loc) · 2.81 KB
/
MainView.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useMemo } from 'react';
import { Container, Grid, Header, Icon, Segment, Divider } from 'semantic-ui-react';
import { Party } from '@daml/types';
import { User } from '@daml.js/blackjacek';
import { useParty, useLedger, useStreamFetchByKeys, useStreamQueries } from '@daml/react';
import UserList from './UserList';
import PartyListEdit from './PartyListEdit';
// USERS_BEGIN
const MainView: React.FC = () => {
const username = useParty();
const myUserResult = useStreamFetchByKeys(User.User, () => [username], [username]);
const myUser = myUserResult.contracts[0]?.payload;
const allUsers = useStreamQueries(User.User).contracts;
// USERS_END
// Sorted list of users that are following the current user
const followers = useMemo(() =>
allUsers
.map(user => user.payload)
.filter(user => user.username !== username)
.sort((x, y) => x.username.localeCompare(y.username)),
[allUsers, username]);
// FOLLOW_BEGIN
const ledger = useLedger();
const follow = async (userToFollow: Party): Promise<boolean> => {
try {
await ledger.exerciseByKey(User.User.Follow, username, {userToFollow});
return true;
} catch (error) {
alert(`Unknown error:\n${error}`);
return false;
}
}
// FOLLOW_END
return (
<Container>
<Grid centered columns={2}>
<Grid.Row stretched>
<Grid.Column>
<Header as='h1' size='huge' color='blue' textAlign='center' style={{padding: '1ex 0em 0ex 0em'}}>
{myUser ? `Welcome, ${myUser.username}!` : 'Loading...'}
</Header>
<Segment>
<Header as='h2'>
<Icon name='user' />
<Header.Content>
{myUser?.username ?? 'Loading...'}
<Header.Subheader>Users I'm following</Header.Subheader>
</Header.Content>
</Header>
<Divider />
<PartyListEdit
parties={myUser?.following ?? []}
onAddParty={follow}
/>
</Segment>
<Segment>
<Header as='h2'>
<Icon name='globe' />
<Header.Content>
The Network
<Header.Subheader>My followers and users they are following</Header.Subheader>
</Header.Content>
</Header>
<Divider />
{/* USERLIST_BEGIN */}
<UserList
users={followers}
onFollow={follow}
/>
{/* USERLIST_END */}
</Segment>
</Grid.Column>
</Grid.Row>
</Grid>
</Container>
);
}
export default MainView;