-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: React participant-avatar (#9828)
- Loading branch information
AndyLnd
authored
Oct 28, 2020
1 parent
dd70307
commit c6dc0d7
Showing
45 changed files
with
1,380 additions
and
674 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
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
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2020 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import ParticipantAvatar, {ParticipantAvatarProps} from './ParticipantAvatar'; | ||
import TestPage from 'Util/test/TestPage'; | ||
import {User} from '../entity/User'; | ||
import {AssetRepository} from '../assets/AssetRepository'; | ||
|
||
class ParticipantAvatarPage extends TestPage<ParticipantAvatarProps> { | ||
constructor(props?: ParticipantAvatarProps) { | ||
super(ParticipantAvatar, props); | ||
} | ||
|
||
getUserParticpantAvatar = () => this.get('div[data-uie-name="element-avatar-user"]'); | ||
getServiceParticipantAvatar = () => this.get('div[data-uie-name="service-avatar"]'); | ||
getTemporaryGuestAvatar = () => this.get('div[data-uie-name="element-avatar-temporary-guest"]'); | ||
getServiceAvatar = () => this.get('div[data-uie-name="element-avatar-service"]'); | ||
getUserAvatar = () => this.get('div[data-uie-name="element-avatar-user"]'); | ||
|
||
clickUserAvatar = () => this.click(this.getUserParticpantAvatar()); | ||
} | ||
|
||
describe('ParticipantAvatar', () => { | ||
it('executes onClick with current participant', async () => { | ||
const assetRepoSpy = (jasmine.createSpy() as unknown) as AssetRepository; | ||
const participant = new User('id'); | ||
participant.name('Anton Bertha'); | ||
|
||
const participantAvatar = new ParticipantAvatarPage({ | ||
assetRepository: assetRepoSpy, | ||
clickHandler: jasmine.createSpy(), | ||
participant, | ||
}); | ||
|
||
participantAvatar.clickUserAvatar(); | ||
|
||
expect(participantAvatar.getProps().clickHandler).toHaveBeenCalledWith( | ||
participantAvatar.getProps().participant, | ||
jasmine.anything(), | ||
); | ||
}); | ||
|
||
it('renders temporary guest avatar', async () => { | ||
const assetRepoSpy = (jasmine.createSpy() as unknown) as AssetRepository; | ||
const participant = new User('id'); | ||
participant.name('Anton Bertha'); | ||
participant.isTemporaryGuest(true); | ||
|
||
const participantAvatar = new ParticipantAvatarPage({ | ||
assetRepository: assetRepoSpy, | ||
participant, | ||
}); | ||
|
||
expect(participantAvatar.getTemporaryGuestAvatar().exists()).toBe(true); | ||
}); | ||
|
||
it('renders service avatar', async () => { | ||
const assetRepoSpy = (jasmine.createSpy() as unknown) as AssetRepository; | ||
const participant = new User('id'); | ||
participant.name('Anton Bertha'); | ||
participant.isService = true; | ||
|
||
const participantAvatar = new ParticipantAvatarPage({ | ||
assetRepository: assetRepoSpy, | ||
participant, | ||
}); | ||
|
||
expect(participantAvatar.getServiceAvatar().exists()).toBe(true); | ||
}); | ||
|
||
it('renders user avatar', async () => { | ||
const assetRepoSpy = (jasmine.createSpy() as unknown) as AssetRepository; | ||
const participant = new User('id'); | ||
participant.name('Anton Bertha'); | ||
|
||
const participantAvatar = new ParticipantAvatarPage({ | ||
assetRepository: assetRepoSpy, | ||
participant, | ||
}); | ||
|
||
expect(participantAvatar.getUserAvatar().exists()).toBe(true); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2020 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
* | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import {User} from '../entity/User'; | ||
import {ServiceEntity} from '../integration/ServiceEntity'; | ||
import {AssetRepository} from '../assets/AssetRepository'; | ||
import {registerReactComponent} from 'Util/ComponentUtil'; | ||
|
||
import UserAvatar from './participantAvatar/UserAvatar'; | ||
import ServiceAvatar from './participantAvatar/ServiceAvatar'; | ||
import TemporaryGuestAvatar from './participantAvatar/TemporaryGuestAvatar'; | ||
|
||
export enum AVATAR_SIZE { | ||
LARGE = 'avatar-l', | ||
MEDIUM = 'avatar-m', | ||
SMALL = 'avatar-s', | ||
X_LARGE = 'avatar-xl', | ||
X_SMALL = 'avatar-xs', | ||
XX_SMALL = 'avatar-xxs', | ||
XXX_SMALL = 'avatar-xxxs', | ||
} | ||
|
||
export enum STATE { | ||
BLOCKED = 'blocked', | ||
IGNORED = 'ignored', | ||
NONE = '', | ||
PENDING = 'pending', | ||
SELECTED = 'selected', | ||
SELF = 'self', | ||
UNKNOWN = 'unknown', | ||
} | ||
|
||
export const DIAMETER = { | ||
[AVATAR_SIZE.LARGE]: 72, | ||
[AVATAR_SIZE.MEDIUM]: 40, | ||
[AVATAR_SIZE.SMALL]: 28, | ||
[AVATAR_SIZE.X_LARGE]: 200, | ||
[AVATAR_SIZE.X_SMALL]: 24, | ||
[AVATAR_SIZE.XX_SMALL]: 20, | ||
[AVATAR_SIZE.XXX_SMALL]: 16, | ||
}; | ||
|
||
export const INITIALS_SIZE = { | ||
[AVATAR_SIZE.LARGE]: '24px', | ||
[AVATAR_SIZE.MEDIUM]: '16px', | ||
[AVATAR_SIZE.SMALL]: '11px', | ||
[AVATAR_SIZE.X_LARGE]: '32px', | ||
[AVATAR_SIZE.X_SMALL]: '11px', | ||
[AVATAR_SIZE.XX_SMALL]: '11px', | ||
[AVATAR_SIZE.XXX_SMALL]: '8px', | ||
}; | ||
|
||
export interface ParticipantAvatarProps { | ||
assetRepository: AssetRepository; | ||
clickHandler?: (participant: User, target: Node) => void; | ||
noBadge?: boolean; | ||
noFilter?: boolean; | ||
participant: User; | ||
size?: AVATAR_SIZE; | ||
} | ||
|
||
const ParticipantAvatar: React.FunctionComponent<ParticipantAvatarProps> = ({ | ||
assetRepository, | ||
participant, | ||
clickHandler, | ||
noBadge = false, | ||
noFilter = false, | ||
size = AVATAR_SIZE.LARGE, | ||
}) => { | ||
const isUser = participant instanceof User && !participant.isService && !participant.isTemporaryGuest(); | ||
const isService = participant instanceof ServiceEntity || participant.isService; | ||
const isTemporaryGuest = !isService && participant.isTemporaryGuest(); | ||
|
||
const avatarState = (() => { | ||
switch (true) { | ||
case isService: | ||
return STATE.NONE; | ||
case participant.isMe: | ||
return STATE.SELF; | ||
case participant.isTeamMember(): | ||
return STATE.NONE; | ||
case participant.isBlocked(): | ||
return STATE.BLOCKED; | ||
case participant.isRequest(): | ||
return STATE.PENDING; | ||
case participant.isIgnored(): | ||
return STATE.IGNORED; | ||
case participant.isCanceled() || participant.isUnknown(): | ||
return STATE.UNKNOWN; | ||
default: | ||
return STATE.NONE; | ||
} | ||
})(); | ||
|
||
const onClick = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => { | ||
if (typeof clickHandler === 'function') { | ||
clickHandler(participant, (event.currentTarget as Node).parentNode); | ||
} | ||
}; | ||
|
||
if (isUser) { | ||
return ( | ||
<UserAvatar | ||
size={size} | ||
assetRepository={assetRepository} | ||
noBadge={noBadge} | ||
noFilter={noFilter} | ||
participant={participant} | ||
state={avatarState} | ||
onClick={onClick} | ||
/> | ||
); | ||
} | ||
|
||
if (isTemporaryGuest) { | ||
return ( | ||
<TemporaryGuestAvatar | ||
noBadge={noBadge} | ||
participant={participant} | ||
state={avatarState} | ||
size={size} | ||
onClick={onClick} | ||
/> | ||
); | ||
} | ||
|
||
return <ServiceAvatar assetRepository={assetRepository} size={size} participant={participant} onClick={onClick} />; | ||
}; | ||
|
||
export default ParticipantAvatar; | ||
|
||
registerReactComponent('participant-avatar', { | ||
component: ParticipantAvatar, | ||
injected: {assetRepository: AssetRepository}, | ||
optionalParams: ['size', 'click', 'noBadge', 'noFilter'], | ||
template: | ||
'<span data-bind="react: {assetRepository, participant: ko.unwrap(participant), size, clickHandler: click, noBadge, noFilter}"></span>', | ||
}); |
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
Oops, something went wrong.