forked from mattermost/mattermost-plugin-msteams
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1baf888
commit 13bb9a6
Showing
30 changed files
with
531 additions
and
15 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
55 changes: 55 additions & 0 deletions
55
webapp/src/components/LinkChannelModal/LinkChannelModal.component.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,55 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
|
||
import {LinearProgress, Modal} from '@brightscout/mattermost-ui-library'; | ||
|
||
import usePluginApi from 'hooks/usePluginApi'; | ||
|
||
import {getCurrentTeam, getLinkModalState} from 'selectors'; | ||
|
||
import {SearchMMChannels} from './SearchMMChannels'; | ||
import {SearchMSTeams} from './SearchMSTeams'; | ||
import {SearchMSChannels} from './SearchMSChannels'; | ||
|
||
export const LinkChannelModal = ({onClose}: {onClose: () => void}) => { | ||
const {state} = usePluginApi(); | ||
const {show = false, isLoading} = getLinkModalState(state); | ||
const currentTeam = getCurrentTeam(state); | ||
const [mMChannel, setMmChannel] = useState<Channel | null>(null); | ||
const [mSTeam, setMsTeam] = useState<MSTeamOrChannel | null>(null); | ||
const [mSChannel, setMsChannel] = useState<MSTeamOrChannel | null>(null); | ||
|
||
const handleModalClose = () => { | ||
setMmChannel(null); | ||
setMsTeam(null); | ||
setMsChannel(null); | ||
onClose(); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
show={show} | ||
title='Link a channel' | ||
subtitle='Link a channel in Mattermost with a channel in Microsoft Teams.' | ||
primaryActionText='Link Channels' | ||
secondaryActionText='Cancel' | ||
onFooterCloseHandler={handleModalClose} | ||
onHeaderCloseHandler={handleModalClose} | ||
isPrimaryButtonDisabled={!mMChannel || !mSChannel || !mSTeam} | ||
onSubmitHandler={() => { | ||
// TODO: handle channel linking | ||
}} | ||
> | ||
{isLoading && <LinearProgress className='fixed w-full left-0 top-100'/>} | ||
<SearchMMChannels | ||
setChannel={setMmChannel} | ||
teamId={currentTeam} | ||
/> | ||
<hr className='w-full my-32'/> | ||
<SearchMSTeams setMsTeam={setMsTeam}/> | ||
<SearchMSChannels | ||
setChannel={setMsChannel} | ||
teamId={mSTeam?.ID} | ||
/> | ||
</Modal> | ||
); | ||
}; |
101 changes: 101 additions & 0 deletions
101
webapp/src/components/LinkChannelModal/SearchMMChannels/SearchMMChannels.component.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,101 @@ | ||
import React, {useCallback, useEffect, useState} from 'react'; | ||
|
||
import {Client4} from 'mattermost-redux/client'; | ||
|
||
import {ListItemType, MMSearch} from '@brightscout/mattermost-ui-library'; | ||
|
||
import {useDispatch} from 'react-redux'; | ||
|
||
import utils from 'utils'; | ||
|
||
import {Icon} from 'components/Icon'; | ||
|
||
import {debounceFunctionTimeLimit} from 'constants/common.constants'; | ||
|
||
import {setLinkModalLoading} from 'reducers/linkModal'; | ||
|
||
import {SearchMMChannelProps} from './SearchMMChannels.types'; | ||
|
||
export const SearchMMChannels = ({ | ||
setChannel, | ||
teamId, | ||
}: SearchMMChannelProps) => { | ||
const dispatch = useDispatch(); | ||
const [searchTerm, setSearchTerm] = useState<string>(''); | ||
|
||
const [searchSuggestions, setSearchSuggestions] = useState<ListItemType[]>([]); | ||
const [suggestionsLoading, setSuggestionsLoading] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
handleClearInput(); | ||
}, [teamId]); | ||
|
||
const searchChannels = ({searchFor}: {searchFor?: string}) => { | ||
if (searchFor && teamId) { | ||
setSuggestionsLoading(true); | ||
dispatch(setLinkModalLoading(true)); | ||
Client4.autocompleteChannelsForSearch(teamId, searchFor). | ||
then((channels) => { | ||
const suggestions = []; | ||
for (const channel of channels) { | ||
suggestions.push({ | ||
label: channel.display_name, | ||
value: channel.id, | ||
}); | ||
} | ||
setSearchSuggestions(suggestions); | ||
setSuggestionsLoading(false); | ||
dispatch(setLinkModalLoading(false)); | ||
}).catch((err) => { | ||
// TODO: Handle error here | ||
setSuggestionsLoading(false); | ||
dispatch(setLinkModalLoading(true)); | ||
}); | ||
} | ||
}; | ||
|
||
const debouncedSearchChannels = useCallback(utils.debounce(searchChannels, debounceFunctionTimeLimit), [searchChannels]); | ||
|
||
const handleSearch = (val: string) => { | ||
if (!val) { | ||
setSearchSuggestions([]); | ||
setChannel(null); | ||
} | ||
setSearchTerm(val); | ||
debouncedSearchChannels({searchFor: val}); | ||
}; | ||
|
||
const handleChannelSelect = (_: any, option: ListItemType) => { | ||
setChannel({ | ||
id: option.value, | ||
displayName: option.label as string, | ||
}); | ||
setSearchTerm(option.label as string); | ||
}; | ||
|
||
const handleClearInput = () => { | ||
setSearchTerm(''); | ||
setSearchSuggestions([]); | ||
setChannel(null); | ||
}; | ||
|
||
return ( | ||
<div className='d-flex flex-column gap-24'> | ||
<div className='d-flex gap-8 align-items-center'> | ||
<Icon iconName='mattermost'/> | ||
<h5 className='my-0 lh-20 wt-600'>{'Select a Mattermost channel'}</h5> | ||
</div> | ||
<MMSearch | ||
autoFocus={true} | ||
fullWidth={true} | ||
label='Search Mattermost channels' | ||
items={searchSuggestions} | ||
onSelect={handleChannelSelect} | ||
searchValue={searchTerm} | ||
setSearchValue={handleSearch} | ||
onClearInput={handleClearInput} | ||
optionsLoading={suggestionsLoading} | ||
/> | ||
</div> | ||
); | ||
}; |
4 changes: 4 additions & 0 deletions
4
webapp/src/components/LinkChannelModal/SearchMMChannels/SearchMMChannels.types.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,4 @@ | ||
export type SearchMMChannelProps = { | ||
setChannel: React.Dispatch<React.SetStateAction<Channel | null>>; | ||
teamId: string | null, | ||
} |
1 change: 1 addition & 0 deletions
1
webapp/src/components/LinkChannelModal/SearchMMChannels/index.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 @@ | ||
export {SearchMMChannels} from './SearchMMChannels.component'; |
105 changes: 105 additions & 0 deletions
105
webapp/src/components/LinkChannelModal/SearchMSChannels/SearchMSChannels.component.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,105 @@ | ||
import React, {useCallback, useEffect, useState} from 'react'; | ||
|
||
import {ListItemType, MMSearch} from '@brightscout/mattermost-ui-library'; | ||
|
||
import {useDispatch} from 'react-redux'; | ||
|
||
import usePluginApi from 'hooks/usePluginApi'; | ||
import utils from 'utils'; | ||
import {debounceFunctionTimeLimit, defaultPage, defaultPerPage} from 'constants/common.constants'; | ||
import {pluginApiServiceConfigs} from 'constants/apiService.constant'; | ||
import useApiRequestCompletionState from 'hooks/useApiRequestCompletionState'; | ||
|
||
import {setLinkModalLoading} from 'reducers/linkModal'; | ||
|
||
import {SearchMSChannelProps} from './SearchMSChannels.types'; | ||
|
||
export const SearchMSChannels = ({setChannel, teamId}: SearchMSChannelProps) => { | ||
const dispatch = useDispatch(); | ||
const {makeApiRequestWithCompletionStatus, getApiState} = usePluginApi(); | ||
const [searchTerm, setSearchTerm] = useState<string>(''); | ||
const [searchChannelsPayload, setSearchChannelsPayload] = useState<SearchMSChannelsParams | null>(null); | ||
const [searchSuggestions, setSearchSuggestions] = useState<ListItemType[]>([]); | ||
|
||
useEffect(() => { | ||
handleClearInput(); | ||
}, [teamId]); | ||
|
||
const searchChannels = ({searchFor}: {searchFor?: string}) => { | ||
if (searchFor && teamId) { | ||
const payload = { | ||
search: searchFor, | ||
page: defaultPage, | ||
per_page: defaultPerPage, | ||
teamId, | ||
}; | ||
setSearchChannelsPayload(payload); | ||
makeApiRequestWithCompletionStatus(pluginApiServiceConfigs.searchMSChannels.apiServiceName, payload); | ||
dispatch(setLinkModalLoading(true)); | ||
} | ||
}; | ||
|
||
const debouncedSearchChannels = useCallback(utils.debounce(searchChannels, debounceFunctionTimeLimit), [searchChannels]); | ||
|
||
const handleSearch = (val: string) => { | ||
if (!val) { | ||
setSearchSuggestions([]); | ||
setChannel(null); | ||
} | ||
setSearchTerm(val); | ||
debouncedSearchChannels({searchFor: val}); | ||
}; | ||
|
||
const {data: searchedChannels, isLoading: searchSuggestionsLoading} = getApiState(pluginApiServiceConfigs.searchMSChannels.apiServiceName, searchChannelsPayload as SearchMSChannelsParams); | ||
const handleChannelSelect = (_: any, option: ListItemType) => { | ||
setChannel({ | ||
ID: option.value, | ||
DisplayName: option.label as string, | ||
}); | ||
setSearchTerm(option.label as string); | ||
}; | ||
|
||
const handleClearInput = () => { | ||
setSearchTerm(''); | ||
setChannel(null); | ||
setSearchSuggestions([]); | ||
}; | ||
|
||
useApiRequestCompletionState({ | ||
serviceName: pluginApiServiceConfigs.searchMSChannels.apiServiceName, | ||
payload: searchChannelsPayload as SearchMSChannelsParams, | ||
handleSuccess: () => { | ||
if (searchedChannels) { | ||
const suggestions: ListItemType[] = []; | ||
for (const channel of searchedChannels as MSTeamsSearchResponse) { | ||
suggestions.push({ | ||
label: channel.DisplayName, | ||
value: channel.ID, | ||
}); | ||
} | ||
setSearchSuggestions(suggestions); | ||
} | ||
dispatch(setLinkModalLoading(false)); | ||
}, | ||
handleError: () => { | ||
dispatch(setLinkModalLoading(false)); | ||
|
||
// TODO: Handle this error | ||
}, | ||
}); | ||
|
||
return ( | ||
<div className='mt-24'> | ||
<MMSearch | ||
fullWidth={true} | ||
label='Select a channel in Microsoft Teams' | ||
items={searchSuggestions} | ||
onSelect={handleChannelSelect} | ||
searchValue={searchTerm} | ||
setSearchValue={handleSearch} | ||
onClearInput={handleClearInput} | ||
optionsLoading={searchSuggestionsLoading} | ||
disabled={!teamId} | ||
/> | ||
</div>); | ||
}; |
4 changes: 4 additions & 0 deletions
4
webapp/src/components/LinkChannelModal/SearchMSChannels/SearchMSChannels.types.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,4 @@ | ||
export type SearchMSChannelProps = { | ||
setChannel: React.Dispatch<React.SetStateAction<MSTeamOrChannel | null>>; | ||
teamId?: string | null, | ||
} |
1 change: 1 addition & 0 deletions
1
webapp/src/components/LinkChannelModal/SearchMSChannels/index.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 @@ | ||
export {SearchMSChannels} from './SearchMSChannels.component'; |
Oops, something went wrong.