This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Live location share - set time limit (#8082)
* add mocking helpers for platform peg Signed-off-by: Kerry Archibald <[email protected]> * basic working live duration dropdown Signed-off-by: Kerry Archibald <[email protected]> * add duration format utility Signed-off-by: Kerry Archibald <[email protected]> * add duration dropdown to live location picker Signed-off-by: Kerry Archibald <[email protected]> * adjust style to allow overflow and variable height chin Signed-off-by: Kerry Archibald <[email protected]> * tidy comments Signed-off-by: Kerry Archibald <[email protected]> * arrow fn change Signed-off-by: Kerry Archibald <[email protected]> * lint Signed-off-by: Kerry Archibald <[email protected]>
- Loading branch information
Kerry
authored
Mar 21, 2022
1 parent
8418b4f
commit 14653d1
Showing
13 changed files
with
365 additions
and
36 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
19 changes: 19 additions & 0 deletions
19
res/css/components/views/location/_LiveDurationDropdown.scss
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,19 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
.mx_LiveDurationDropdown { | ||
margin-bottom: $spacing-16; | ||
} |
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,72 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { formatDuration } from '../../../DateUtils'; | ||
import { _t } from '../../../languageHandler'; | ||
import Dropdown from '../elements/Dropdown'; | ||
|
||
const DURATION_MS = { | ||
fifteenMins: 900000, | ||
oneHour: 3600000, | ||
eightHours: 28800000, | ||
}; | ||
|
||
export const DEFAULT_DURATION_MS = DURATION_MS.fifteenMins; | ||
|
||
interface Props { | ||
timeout: number; | ||
onChange: (timeout: number) => void; | ||
} | ||
|
||
const getLabel = (durationMs: number) => { | ||
return _t('Share for %(duration)s', { duration: formatDuration(durationMs) }); | ||
}; | ||
|
||
const LiveDurationDropdown: React.FC<Props> = ({ timeout, onChange }) => { | ||
const options = Object.values(DURATION_MS).map((duration) => | ||
({ key: duration.toString(), duration, label: getLabel(duration) }), | ||
); | ||
|
||
// timeout is not one of our default values | ||
// eg it was set by another client | ||
if (!Object.values(DURATION_MS).includes(timeout)) { | ||
options.push({ | ||
key: timeout.toString(), duration: timeout, label: getLabel(timeout), | ||
}); | ||
} | ||
|
||
const onOptionChange = (key: string) => { | ||
// stringified value back to number | ||
onChange(+key); | ||
}; | ||
|
||
return <Dropdown | ||
id='live-duration' | ||
data-test-id='live-duration-dropdown' | ||
label={getLabel(timeout)} | ||
value={timeout.toString()} | ||
onOptionChange={onOptionChange} | ||
className='mx_LiveDurationDropdown' | ||
> | ||
{ options.map(({ key, label }) => | ||
<div data-test-id={`live-duration-option-${key}`} key={key}>{ label }</div>, | ||
) } | ||
</Dropdown>; | ||
}; | ||
|
||
export default LiveDurationDropdown; |
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
75 changes: 75 additions & 0 deletions
75
test/components/views/location/LiveDurationDropdown-test.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,75 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
import '../../../skinned-sdk'; | ||
import LiveDurationDropdown, { DEFAULT_DURATION_MS } | ||
from '../../../../src/components/views/location/LiveDurationDropdown'; | ||
import { findById, mockPlatformPeg } from '../../../test-utils'; | ||
|
||
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) }); | ||
|
||
describe('<LiveDurationDropdown />', () => { | ||
const defaultProps = { | ||
timeout: DEFAULT_DURATION_MS, | ||
onChange: jest.fn(), | ||
}; | ||
const getComponent = (props = {}) => | ||
mount(<LiveDurationDropdown {...defaultProps} {...props} />); | ||
|
||
const getOption = (wrapper, timeout) => findById(wrapper, `live-duration__${timeout}`).at(0); | ||
const getSelectedOption = (wrapper) => findById(wrapper, 'live-duration_value'); | ||
const openDropdown = (wrapper) => act(() => { | ||
wrapper.find('[role="button"]').at(0).simulate('click'); | ||
wrapper.setProps({}); | ||
}); | ||
|
||
it('renders timeout as selected option', () => { | ||
const wrapper = getComponent(); | ||
expect(getSelectedOption(wrapper).text()).toEqual('Share for 15m'); | ||
}); | ||
|
||
it('renders non-default timeout as selected option', () => { | ||
const timeout = 1234567; | ||
const wrapper = getComponent({ timeout }); | ||
expect(getSelectedOption(wrapper).text()).toEqual(`Share for 21m`); | ||
}); | ||
|
||
it('renders a dropdown option for a non-default timeout value', () => { | ||
const timeout = 1234567; | ||
const wrapper = getComponent({ timeout }); | ||
openDropdown(wrapper); | ||
expect(getOption(wrapper, timeout).text()).toEqual(`Share for 21m`); | ||
}); | ||
|
||
it('updates value on option selection', () => { | ||
const onChange = jest.fn(); | ||
const wrapper = getComponent({ onChange }); | ||
|
||
const ONE_HOUR = 3600000; | ||
|
||
openDropdown(wrapper); | ||
|
||
act(() => { | ||
getOption(wrapper, ONE_HOUR).simulate('click'); | ||
}); | ||
|
||
expect(onChange).toHaveBeenCalledWith(ONE_HOUR); | ||
}); | ||
}); |
Oops, something went wrong.