-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUserNicknamesPanel.js
More file actions
162 lines (134 loc) · 5.22 KB
/
Copy pathUserNicknamesPanel.js
File metadata and controls
162 lines (134 loc) · 5.22 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { useCallback, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import ConfirmActionButton from '~/components/ConfirmActionButton'
import AddNicknameForm from '~/components/Forms/AddNicknameForm/AddNicknameForm'
import { deleteNickname, setDisplayNickname, getUserProfile } from '~/store/actions/user'
import {
selectUserById,
withCurrentUserId,
selectNicknamesByUserId,
} from '~/store/selectors'
import getResponseError from '~/util/getResponseError'
import NicknameErrorBox from './NicknameErrorBox'
import styles from './UserNicknamesPanel.module.scss'
// Component constants
const MAX_NICKS = 16 // Maximum IRC Nicknames allowed
function UserNicknamesPanel () {
const [error, setError] = useState(null)
const dispatch = useDispatch()
const nicknames = useSelector(withCurrentUserId(selectNicknamesByUserId))
const user = useSelector(withCurrentUserId(selectUserById))
const handleDeleteNickname = useCallback(async (event) => {
setError(null)
const response = await dispatch(deleteNickname(user, nicknames.find((nick) => {
return nick.id === event.target.name
})))
const responseError = getResponseError(response)
if (responseError) {
setError(responseError)
return responseError
}
return true
}, [dispatch, nicknames, user])
const handleSetDisplayNickname = useCallback(async (event) => {
setError(null)
const nickname = nicknames.find((nick) => {
return nick.id === event.target.name
})
const response = await dispatch(setDisplayNickname(nickname.id, nickname.attributes.nick))
const responseError = getResponseError(response)
if (responseError) {
setError(responseError)
return responseError
}
// Refresh user profile to get updated nicknames
await dispatch(getUserProfile())
return true
}, [dispatch, nicknames])
const nickCount = nicknames?.length
const maxNicksReached = (nickCount >= MAX_NICKS)
return (
<div className="panel">
<header>
{'IRC Nicknames'}
<div className="controls">
<span className="nickname-count">{`${nickCount}/${MAX_NICKS}`}</span>
</div>
</header>
<div className={styles.userNicknames}>
{
error && (
<NicknameErrorBox error={error} />
)
}
<ul className={styles.nickList}>
{
(nickCount <= 0) && (
<li className="text-center">{'You do not have any nicknames registered yet.'}</li>
)
}
{
nicknames?.map((nickname) => {
const isDisplayNick = nickname.attributes?.display === nickname.attributes?.nick
return (
<li key={nickname.id}>
<span>
{nickname.attributes?.nick}
{
isDisplayNick && (
<FontAwesomeIcon
fixedWidth
className={styles.displayIcon}
icon="star"
title="Current display nickname" />
)
}
</span>
<div className={styles.controlContainer}>
{
// Only show buttons for non-display nicks
!isDisplayNick && (
<>
<ConfirmActionButton
className="icon"
confirmButtonText={`Set '${nickname.attributes?.nick}' as display nickname`}
confirmSubText=""
denyButtonText="Cancel"
name={nickname.id}
title="Set as display nickname"
onConfirm={handleSetDisplayNickname}
onConfirmText="">
<FontAwesomeIcon fixedWidth icon="farStar" title="Set as display nickname" />
</ConfirmActionButton>
<ConfirmActionButton
className="icon"
confirmButtonText={`Delete nickname '${nickname.attributes?.nick}'`}
confirmSubText=""
denyButtonText="Cancel"
name={nickname.id}
title="Delete nickname"
onConfirm={handleDeleteNickname}
onConfirmText="">
<FontAwesomeIcon fixedWidth icon="trash" title="Delete nickname" />
</ConfirmActionButton>
</>
)
}
</div>
</li>
)
}) ?? null
}
</ul>
<div className={styles.addNicknameFloat}>
<AddNicknameForm
disabled={maxNicksReached}
registeredNicks={nicknames}
title={maxNicksReached ? 'You\'ve used all your nicknames' : 'Add new nickname'} />
</div>
</div>
</div>
)
}
export default UserNicknamesPanel