This repository was archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathUserProfileBox.tsx
105 lines (97 loc) · 2.85 KB
/
UserProfileBox.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Box, makeStyles, Typography } from '@material-ui/core';
import { ContentCreator } from '@trex/shared/models/ContentCreator';
import * as QR from 'avenger/lib/QueryResult';
import { declareQueries } from 'avenger/lib/react';
import { sequenceS } from 'fp-ts/lib/Apply';
import { pipe } from 'fp-ts/lib/function';
import * as TE from 'fp-ts/lib/TaskEither';
import { toAppError } from '@trex/shared/errors/AppError';
import React from 'react';
import {
updateAuth,
updateProfile,
} from '../../state/dashboard/creator.commands';
import { localProfile } from '../../state/dashboard/creator.queries';
import { doUpdateCurrentView } from '../../utils/location.utils';
import { ErrorBox } from '@trex/shared/components/Error/ErrorBox';
import { LazyFullSizeLoader } from '../common/FullSizeLoader';
import UnlinkProfileButton from '../common/UnlinkProfileButton';
import Avatar from '../external/Avatar';
interface LoggedInUserProfileBoxProps {
onLogout: () => void;
profile: ContentCreator;
}
const useStyles = makeStyles((theme) => ({
avatar: {
marginRight: theme.spacing(1),
},
username: {
marginBottom: theme.spacing(1),
lineHeight: 1,
},
channel: {
display: 'block',
marginBottom: theme.spacing(1),
wordBreak: 'break-all',
},
unlink: {
padding: 0,
lineHeight: 1,
marginBottom: 0,
},
}));
export const LoggedInUserProfileBox: React.FC<LoggedInUserProfileBoxProps> = ({
onLogout,
profile,
}) => {
const classes = useStyles();
return (
<Box display="flex" alignItems="flex-start">
<Avatar src={profile.avatar} className={classes.avatar} />
<Box>
<Typography className={classes.username}>{profile.username}</Typography>
<Typography variant="caption" className={classes.channel}>
Channel ID:
<br />
{profile.channelId}
</Typography>
<UnlinkProfileButton
className={classes.unlink}
variant="text"
size="small"
onLogout={onLogout}
/>
</Box>
</Box>
);
};
const withQueries = declareQueries({ profile: localProfile });
export const UserProfileBox = withQueries(
({ queries }): React.ReactElement | null => {
const handleChannelDelete = React.useCallback(async (): Promise<void> => {
void pipe(
sequenceS(TE.ApplicativePar)({
auth: updateAuth(null),
profile: updateProfile(null),
}),
TE.chain(() =>
pipe(doUpdateCurrentView({ view: 'index' }), TE.mapLeft(toAppError))
)
)();
}, []);
return pipe(
queries,
QR.fold(LazyFullSizeLoader, ErrorBox, ({ profile }) => {
if (profile === null) {
return null;
}
return (
<LoggedInUserProfileBox
profile={profile}
onLogout={() => handleChannelDelete()}
/>
);
})
);
}
);