Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom space avatar images #45148

Merged
merged 20 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/spaces/common/model/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export interface Space {
initials?: string;
disabledFeatures: string[];
_reserved?: boolean;
avatarImage?: string;
friol marked this conversation as resolved.
Show resolved Hide resolved
}
15 changes: 15 additions & 0 deletions x-pack/legacy/plugins/spaces/common/space_attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,18 @@ export function getSpaceInitials(space: Partial<Space> = {}) {

return words.map(word => word.substring(0, 1)).join('');
}

/**
* Determines the avatar image for the provided space.
*
* @param {Space} space
*/
export function getSpaceAvatarImage(space: Partial<Space> = {}) {
const { avatarImage, name = '' } = space;
friol marked this conversation as resolved.
Show resolved Hide resolved

if (avatarImage) {
return avatarImage;
}

return '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { EuiAvatar, isValidHex } from '@elastic/eui';
import React, { SFC } from 'react';
import { getSpaceColor, getSpaceInitials, MAX_SPACE_INITIALS } from '../../common';
import { Space } from '../../common/model/space';
import { getSpaceAvatarImage } from '../../common/space_attributes';

interface Props {
space: Partial<Space>;
Expand Down Expand Up @@ -37,6 +38,7 @@ export const SpaceAvatar: SFC<Props> = (props: Props) => {
initialsLength={MAX_SPACE_INITIALS}
initials={getSpaceInitials(space)}
color={isValidHex(spaceColor) ? spaceColor : ''}
imageUrl={getSpaceAvatarImage(space)}
{...rest}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { EuiColorPicker, EuiFieldText, EuiFlexItem, EuiFormRow, isValidHex } from '@elastic/eui';
import { InjectedIntl, injectI18n } from '@kbn/i18n/react';
import React, { ChangeEvent, Component } from 'react';
import {
EuiButton,
EuiColorPicker,
EuiFieldText,
EuiFlexItem,
EuiFormRow,
EuiFilePicker,
isValidHex,
} from '@elastic/eui';
import { InjectedIntl, injectI18n } from '@kbn/i18n/react';

import { get } from 'lodash';
import { encode } from '../../../../../../canvas/common/lib/dataurl';
friol marked this conversation as resolved.
Show resolved Hide resolved

import { MAX_SPACE_INITIALS } from '../../../../../common/constants';
import { Space } from '../../../../../common/model/space';
import { getSpaceColor, getSpaceInitials } from '../../../../../common/space_attributes';
Expand All @@ -20,6 +32,7 @@ interface Props {
interface State {
initialsHasFocus: boolean;
pendingInitials?: string | null;
avatarImage?: string | null;
}

class CustomizeSpaceAvatarUI extends Component<Props, State> {
Expand All @@ -29,9 +42,27 @@ class CustomizeSpaceAvatarUI extends Component<Props, State> {
super(props);
this.state = {
initialsHasFocus: false,
avatarImage: props.space.avatarImage,
};
}

private _handleChange = (avImg: string) => {
this.setState({ avatarImage: avImg });

if (avImg) {
this.props.onChange({
...this.props.space,
avatarImage: avImg,
});
}
};

private onFileUpload = (files: File[]) => {
const [file] = files;
const [type, subtype] = get(file, 'type', '').split('/');
encode(file).then((dataurl: string) => this._handleChange(dataurl));
friol marked this conversation as resolved.
Show resolved Hide resolved
};

public render() {
const { space, intl } = this.props;

Expand Down Expand Up @@ -73,11 +104,52 @@ class CustomizeSpaceAvatarUI extends Component<Props, State> {
isInvalid={isInvalidSpaceColor}
/>
</EuiFormRow>
{this.filePickerOrImage()}
friol marked this conversation as resolved.
Show resolved Hide resolved
</EuiFlexItem>
</form>
);
}

private removeAvatarImage() {
this.setState({ avatarImage: '' });
this.props.onChange({
...this.props.space,
avatarImage: '',
});
}

public filePickerOrImage() {
const { intl } = this.props;
const avImage = this.state.avatarImage;

if (!this.state.avatarImage) {
return (
<EuiFormRow
label={intl.formatMessage({
id: 'xpack.spaces.management.customizeSpaceAvatar.avatarImage',
defaultMessage: 'Pick an image for your space',
})}
>
<EuiFilePicker
compressed
initialPromptText="Select avatar image"
friol marked this conversation as resolved.
Show resolved Hide resolved
onChange={this.onFileUpload}
accept="image/*"
friol marked this conversation as resolved.
Show resolved Hide resolved
/>
</EuiFormRow>
);
} else {
if (avImage) {
friol marked this conversation as resolved.
Show resolved Hide resolved
return (
<EuiButton color="danger" onClick={() => this.removeAvatarImage()}>
Remove Avatar Image
friol marked this conversation as resolved.
Show resolved Hide resolved
</EuiButton>
);
}
return '';
}
}

public initialsInputRef = (ref: HTMLInputElement) => {
if (ref) {
this.initialsRef = ref;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ class ManageSpacePageUI extends Component<Props, State> {
initials,
color,
disabledFeatures = [],
avatarImage,
} = this.state.space;

const params = {
Expand All @@ -343,6 +344,7 @@ class ManageSpacePageUI extends Component<Props, State> {
initials,
color,
disabledFeatures,
avatarImage,
};

let action;
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/spaces/server/lib/space_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export const spaceSchema = Joi.object({
.items(Joi.string())
.default([]),
_reserved: Joi.boolean(),
avatarImage: Joi.string().allow(''),
legrego marked this conversation as resolved.
Show resolved Hide resolved
}).default();