-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(slack): Introduce slack support channel selector (#7658)
* feat(slack): Introduce slack support channel selector * Use ReactSelectInput * Use hooks instead of class compoonent * Disable slack in settings * Add useLatestPromise hook * Rebase with updated react-select modules * Rebase with updated react-select modules * Update test * Update test * Refactor test
- Loading branch information
1 parent
fc0c9d5
commit bf2ffe2
Showing
14 changed files
with
149 additions
and
0 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
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
45 changes: 45 additions & 0 deletions
45
app/scripts/modules/core/src/slack/SlackChannelSelector.tsx
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,45 @@ | ||
import * as React from 'react'; | ||
import * as Select from 'react-select'; | ||
import { ReactSelectInput, useLatestPromise } from '@spinnaker/core'; | ||
|
||
import { ISlackChannel, SlackReader } from './SlackReader'; | ||
|
||
export interface ISlackChannelSelectorProps { | ||
channel: ISlackChannel; | ||
callback: (name: string, value: any) => void; | ||
} | ||
|
||
export interface ISlackChannelSelectorState { | ||
channels: ISlackChannel[]; | ||
selected: ISlackChannel; | ||
loading: boolean; | ||
} | ||
|
||
export default function SlackChannelSelector({ channel, callback }: ISlackChannelSelectorProps) { | ||
const [selectedChannel, setSelectedChannel] = React.useState(channel); | ||
const fetchChannels = useLatestPromise(() => SlackReader.getChannels(), []); | ||
const channels = fetchChannels.result; | ||
const isLoading = fetchChannels.status === 'PENDING'; | ||
|
||
const onInputChange = (evt: Select.Option<ISlackChannel>) => { | ||
const newChannel = evt ? evt.target.value : null; | ||
callback('slackChannel', newChannel || {}); | ||
setSelectedChannel(newChannel); | ||
}; | ||
|
||
return ( | ||
<div className="form-group row"> | ||
<div className="col-sm-3 sm-label-right">Slack Channel</div> | ||
<div className="col-sm-9"> | ||
<ReactSelectInput | ||
inputClassName="form-control input-sm" | ||
mode="VIRTUALIZED" | ||
options={(channels || []).map((ch: ISlackChannel) => ({ value: ch, label: ch.name }))} | ||
value={selectedChannel && { value: selectedChannel, label: selectedChannel.name }} | ||
onChange={onInputChange} | ||
isLoading={isLoading} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} |
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,38 @@ | ||
import { API } from '@spinnaker/core'; | ||
import { ISlackChannel, SlackReader } from './SlackReader'; | ||
|
||
const mockSlackChannels: ISlackChannel[] = [ | ||
{ | ||
id: '0-test', | ||
name: 'testchannel', | ||
is_channel: true, | ||
creator: 'creator', | ||
is_archived: false, | ||
name_normalized: 'testchannel', | ||
num_members: 25, | ||
}, | ||
{ | ||
id: '1-test', | ||
name: 'fakechannel', | ||
is_channel: true, | ||
creator: 'creator2', | ||
is_archived: false, | ||
name_normalized: 'fakechannel', | ||
num_members: 25, | ||
}, | ||
]; | ||
|
||
describe('SlackReader', () => { | ||
it('should fetch a list of public Slack channels', () => { | ||
spyOn(SlackReader, 'getChannels').and.callThrough(); | ||
spyOn(API, 'one') | ||
.and.callThrough() | ||
.and.returnValue({ getList: () => Promise.resolve(mockSlackChannels) }); | ||
|
||
SlackReader.getChannels().then((channels: ISlackChannel[]) => { | ||
expect(SlackReader.getChannels).toHaveBeenCalled(); | ||
expect(channels.length).toEqual(2); | ||
expect(API.one).toHaveBeenCalledWith('slack/channels'); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import { IPromise } from 'angular'; | ||
import { API } from '@spinnaker/core'; | ||
|
||
export interface ISlackChannel { | ||
id: string; | ||
name: string; | ||
is_channel: boolean; | ||
creator: string; | ||
is_archived: boolean; | ||
name_normalized: string; | ||
num_members: number; | ||
} | ||
|
||
export class SlackReader { | ||
public static getChannels(): IPromise<ISlackChannel[]> { | ||
return API.one('slack/channels') | ||
.getList() | ||
.catch(() => [] as ISlackChannel[]); | ||
} | ||
} |
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,2 @@ | ||
export * from './slackChannelSelector.component'; | ||
export * from './SlackReader'; |
11 changes: 11 additions & 0 deletions
11
app/scripts/modules/core/src/slack/slackChannelSelector.component.ts
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,11 @@ | ||
import { module } from 'angular'; | ||
import { react2angular } from 'react2angular'; | ||
|
||
import SlackChannelSelector from './SlackChannelSelector'; | ||
|
||
export const SLACK_COMPONENT = 'spinnaker.application.slackChannelSelector.component'; | ||
|
||
module(SLACK_COMPONENT, []).component( | ||
'slackChannelSelector', | ||
react2angular(SlackChannelSelector, ['channel', 'callback']), | ||
); |
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