-
-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
topic edit modal: Add new edit-topic UI.
Fixes: #5365
- Loading branch information
Leslie Ngo
authored and
Leslie Ngo
committed
Sep 29, 2022
1 parent
531e268
commit a100423
Showing
12 changed files
with
257 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* @flow strict-local */ | ||
import React, { createContext, useState, useCallback, useContext } from 'react'; | ||
import type { Context, Node } from 'react'; | ||
import { useSelector } from '../react-redux'; | ||
import TopicEditModal from '../topics/TopicEditModal'; | ||
import { getAuth, getZulipFeatureLevel, getStreamsById } from '../selectors'; | ||
import { TranslationContext } from './TranslationProvider'; | ||
|
||
type Props = $ReadOnly<{| | ||
children: Node, | ||
|}>; | ||
|
||
type StartEditTopicContext = (streamId: number, topic: string) => Promise<void>; | ||
|
||
/* $FlowIssue[incompatible-type] We can't provide an initial 'value' prop. We would | ||
need to provide the 'startEditTopic' callback that's defined after this createContext | ||
call, which is impossible. */ | ||
const TopicModal: Context<StartEditTopicContext> = createContext(undefined); | ||
|
||
export const useStartEditTopic = (): StartEditTopicContext => useContext(TopicModal); | ||
|
||
export default function TopicEditModalProvider(props: Props): Node { | ||
const { children } = props; | ||
const auth = useSelector(getAuth); | ||
const zulipFeatureLevel = useSelector(getZulipFeatureLevel); | ||
const streamsById = useSelector(getStreamsById); | ||
const _ = useContext(TranslationContext); | ||
|
||
const [topicModalProviderState, setTopicModalProviderState] = useState({ | ||
visible: false, | ||
streamId: -1, | ||
topic: '', | ||
}); | ||
|
||
const { visible } = topicModalProviderState; | ||
|
||
const startEditTopic = useCallback( | ||
async (streamId: number, topic: string) => { | ||
if (visible) { | ||
return; | ||
} | ||
setTopicModalProviderState({ | ||
visible: true, | ||
streamId, | ||
topic, | ||
}); | ||
}, | ||
[visible], | ||
); | ||
|
||
const closeEditTopicModal = useCallback(() => { | ||
setTopicModalProviderState({ | ||
visible: false, | ||
streamId: -1, | ||
topic: '', | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<TopicModal.Provider value={startEditTopic}> | ||
<TopicEditModal | ||
topicModalProviderState={topicModalProviderState} | ||
closeEditTopicModal={closeEditTopicModal} | ||
auth={auth} | ||
zulipFeatureLevel={zulipFeatureLevel} | ||
streamsById={streamsById} | ||
_={_} | ||
/> | ||
{children} | ||
</TopicModal.Provider> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// @flow strict-local | ||
import React, { useState, useContext, useEffect } from 'react'; | ||
import { Modal, View } from 'react-native'; | ||
import type { Node } from 'react'; | ||
import styles, { ThemeContext, BRAND_COLOR, createStyleSheet } from '../styles'; | ||
import { updateMessage } from '../api'; | ||
import type { Auth, GetText, Stream } from '../types'; | ||
import { fetchSomeMessageIdForConversation } from '../message/fetchActions'; | ||
import ZulipTextIntl from '../common/ZulipTextIntl'; | ||
import ZulipTextButton from '../common/ZulipTextButton'; | ||
import Input from '../common/Input'; | ||
|
||
type Props = $ReadOnly<{| | ||
topicModalProviderState: { | ||
visible: boolean, | ||
topic: string, | ||
streamId: number, | ||
}, | ||
auth: Auth, | ||
zulipFeatureLevel: number, | ||
streamsById: Map<number, Stream>, | ||
_: GetText, | ||
closeEditTopicModal: () => void, | ||
|}>; | ||
|
||
export default function TopicEditModal(props: Props): Node { | ||
const { topicModalProviderState, closeEditTopicModal, auth, zulipFeatureLevel, streamsById, _ } = | ||
props; | ||
|
||
const { visible, topic, streamId } = topicModalProviderState; | ||
|
||
const [topicName, onChangeTopicName] = useState(); | ||
|
||
useEffect(() => { | ||
onChangeTopicName(topic); | ||
}, [topic]); | ||
|
||
const { backgroundColor } = useContext(ThemeContext); | ||
|
||
const modalStyles = createStyleSheet({ | ||
wrapper: { | ||
flex: 1, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
modal: { | ||
justifyContent: 'flex-start', | ||
backgroundColor, | ||
padding: 15, | ||
shadowOpacity: 0.5, | ||
shadowColor: 'gray', | ||
shadowOffset: { | ||
height: 5, | ||
width: 5, | ||
}, | ||
shadowRadius: 5, | ||
borderRadius: 5, | ||
width: '90%', | ||
}, | ||
buttonContainer: { | ||
flexDirection: 'row', | ||
justifyContent: 'flex-end', | ||
}, | ||
titleText: { | ||
fontSize: 18, | ||
lineHeight: 21, | ||
color: BRAND_COLOR, | ||
marginBottom: 10, | ||
fontWeight: 'bold', | ||
}, | ||
}); | ||
|
||
const handleSubmit = async () => { | ||
if (topicName === '') { | ||
return; | ||
} | ||
const messageId = await fetchSomeMessageIdForConversation( | ||
auth, | ||
streamId, | ||
topic, | ||
streamsById, | ||
zulipFeatureLevel, | ||
); | ||
if (messageId == null) { | ||
throw new Error( | ||
_('No messages in topic: {streamAndTopic}', { | ||
streamAndTopic: `#${streamsById.get(streamId)?.name ?? 'unknown'} > ${topic}`, | ||
}), | ||
); | ||
} | ||
await updateMessage(auth, messageId, { | ||
propagate_mode: 'change_all', | ||
subject: topicName, | ||
...(zulipFeatureLevel >= 9 && { | ||
send_notification_to_old_thread: true, | ||
send_notification_to_new_thread: true, | ||
}), | ||
}); | ||
closeEditTopicModal(); | ||
}; | ||
return ( | ||
<Modal | ||
transparent | ||
visible={visible} | ||
animationType="slide" | ||
onRequestClose={closeEditTopicModal} | ||
supportedOrientations={['portrait', 'landscape', 'landscape-left', 'landscape-right']} | ||
> | ||
<View style={modalStyles.wrapper}> | ||
<View style={modalStyles.modal}> | ||
<ZulipTextIntl style={modalStyles.titleText} text="Edit topic" /> | ||
<Input | ||
style={styles.marginBottom} | ||
defaultValue={topicName} | ||
placeholder="Please enter a new topic name." | ||
onChangeText={onChangeTopicName} | ||
maxLength={60} | ||
autoFocus | ||
selectTextOnFocus | ||
/> | ||
<View style={modalStyles.buttonContainer}> | ||
<ZulipTextButton label="Cancel" onPress={closeEditTopicModal} /> | ||
<ZulipTextButton | ||
label="Submit" | ||
onPress={handleSubmit} | ||
disabled={topicName === ''} | ||
/> | ||
</View> | ||
</View> | ||
</View> | ||
</Modal> | ||
); | ||
} |
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.