-
-
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.
message_actions: Add move message option
- Loading branch information
1 parent
1a281fa
commit 139c454
Showing
8 changed files
with
256 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/* @flow strict-local */ | ||
import React, { useState, useContext } from 'react'; | ||
import { Text, View, Platform, Picker, TouchableOpacity, ScrollView } from 'react-native'; | ||
import type { Node } from 'react'; | ||
import Toast from 'react-native-simple-toast'; | ||
import { ThemeContext, BRAND_COLOR } from '../styles'; | ||
import type { RouteProp } from '../react-navigation'; | ||
import * as api from '../api'; | ||
import { Input } from '../common'; | ||
import type { AppNavigationProp } from '../nav/AppNavigator'; | ||
import { getAuth, getStreams } from '../selectors'; | ||
import { useSelector } from '../react-redux'; | ||
import { showErrorAlert } from '../utils/info'; | ||
import { IconBack } from '../common/Icons'; | ||
import type { Narrow, Message } from '../types'; | ||
import TopicAutocomplete from '../autocomplete/TopicAutocomplete'; | ||
import { streamNameOfNarrow } from '../utils/narrow'; | ||
|
||
type Props = $ReadOnly<{| | ||
navigation: AppNavigationProp<'move-message'>, | ||
route: RouteProp<'move-message', {| message: Message, messageNarrow: Narrow, isadmin: boolean |}>, | ||
|}>; | ||
|
||
const inputMarginPadding = { | ||
paddingHorizontal: 8, | ||
paddingVertical: Platform.select({ | ||
ios: 8, | ||
android: 2, | ||
}), | ||
}; | ||
|
||
export default function MoveMessage(props: Props): Node { | ||
const themeContext = useContext(ThemeContext); | ||
const backgroundColor = themeContext.backgroundColor; | ||
const cardColor = themeContext.cardColor; | ||
const auth = useSelector(getAuth); | ||
const streams = useSelector(getStreams); | ||
const isadmin = props.route.params.isadmin; | ||
const id = props.route.params.message.id; | ||
const names = []; | ||
const [subject, setSubject] = useState(props.route.params.message.subject); | ||
const [propagate_mode, setPropagateMode] = useState('javachange_one'); | ||
const [stream, setStream] = useState(streamNameOfNarrow(props.route.params.messageNarrow)); | ||
const [topicFocus, setTopicFocus] = useState(false); | ||
const messageInputRef = React.createRef<$FlowFixMe>(); | ||
const topicInputRef = React.createRef<$FlowFixMe>(); | ||
|
||
streams.map(item => names.push({ value: item.name })); | ||
|
||
const styles = { | ||
parent: { | ||
backgroundColor: cardColor, | ||
}, | ||
layout: { | ||
margin: 10, | ||
}, | ||
title: { | ||
fontSize: 18, | ||
color: backgroundColor === 'white' ? 'black' : 'white', | ||
}, | ||
autocompleteWrapper: { | ||
position: 'absolute', | ||
top: 0, | ||
width: '100%', | ||
}, | ||
composeText: { | ||
flex: 1, | ||
paddingVertical: 8, | ||
}, | ||
topicInput: { | ||
height: 50, | ||
backgroundColor, | ||
...inputMarginPadding, | ||
}, | ||
viewTitle: { | ||
display: 'flex', | ||
flexDirection: 'row', | ||
justifyContent: 'space-between', | ||
alignItems: 'center', | ||
height: 50, | ||
paddingHorizontal: 10, | ||
marginBottom: 20, | ||
}, | ||
textColor: { | ||
color: backgroundColor === 'white' ? 'black' : 'white', | ||
}, | ||
picker: { backgroundColor, marginBottom: 20 }, | ||
submitButton: { | ||
marginTop: 10, | ||
paddingTop: 15, | ||
paddingBottom: 15, | ||
marginLeft: 30, | ||
marginRight: 30, | ||
backgroundColor: BRAND_COLOR, | ||
borderRadius: 10, | ||
borderWidth: 1, | ||
}, | ||
}; | ||
|
||
const updateTextInput = (textInput, text) => { | ||
if (textInput === null) { | ||
// Depending on the lifecycle events this function is called from, | ||
// this might not be set yet. | ||
return; | ||
} | ||
|
||
// `textInput` is untyped; see definition. | ||
textInput.setNativeProps({ text }); | ||
}; | ||
|
||
const handleTopicChange = (Topic: string) => { | ||
setSubject(Topic); | ||
}; | ||
|
||
const handleTopicFocus = () => { | ||
setTopicFocus(true); | ||
}; | ||
|
||
const setTopicInputValue = (Topic: string) => { | ||
updateTextInput(topicInputRef.current, subject); | ||
handleTopicChange(Topic); | ||
setTopicFocus(false); | ||
}; | ||
|
||
const handleTopicAutocomplete = (Topic: string) => { | ||
setTopicInputValue(Topic); | ||
messageInputRef.current?.focus(); | ||
}; | ||
|
||
const updateMessage = async () => { | ||
const stream_info = await api.getStreamId(auth, stream); | ||
const stream_id = stream_info.stream_id; | ||
if (isadmin) { | ||
api.updateMessage(auth, { subject, stream_id, propagate_mode }, id).catch(error => { | ||
showErrorAlert('Failed to move message', error.message); | ||
props.navigation.goBack(); | ||
}); | ||
} else { | ||
api.updateMessage(auth, { subject, propagate_mode }, id).catch(error => { | ||
showErrorAlert('Failed to move message', error.message); | ||
props.navigation.goBack(); | ||
}); | ||
} | ||
props.navigation.goBack(); | ||
Toast.show('Moved Message'); | ||
}; | ||
|
||
return ( | ||
<ScrollView style={styles.parent}> | ||
<View style={styles.layout}> | ||
<View style={styles.viewTitle}> | ||
<IconBack style={{ color: 'gray' }} size={20} onPress={() => props.navigation.goBack()} /> | ||
<Text style={styles.title}>Move Message</Text> | ||
<View /> | ||
</View> | ||
<Text style={{ fontSize: 14, marginBottom: 10, color: 'gray' }}>Content:</Text> | ||
<Text style={[styles.textColor, { marginBottom: 20 }]}> | ||
{props.route.params.message.content.replace(/<(?:.|\n)*?>/gm, '')} | ||
</Text> | ||
<Text style={{ fontSize: 14, color: 'gray', marginBottom: 10 }}>Stream:</Text> | ||
{isadmin ? ( | ||
<View style={styles.picker}> | ||
<Picker | ||
selectedValue={stream} | ||
onValueChange={(itemValue, itemIndex) => setStream(itemValue.toString())} | ||
style={styles.textColor} | ||
> | ||
{streams.map(item => ( | ||
<Picker.Item label={item.name} value={item.name} key={item.name} /> | ||
))} | ||
</Picker> | ||
</View> | ||
) : ( | ||
<Text style={[styles.textColor, { marginBottom: 10 }]}>{stream}</Text> | ||
)} | ||
<Text style={{ fontSize: 14, color: 'gray', marginBottom: 10 }}>Topic:</Text> | ||
<View style={{ marginBottom: 20 }}> | ||
<Input | ||
underlineColorAndroid="transparent" | ||
placeholder="Topic" | ||
autoFocus={false} | ||
defaultValue={subject} | ||
selectTextOnFocus | ||
textInputRef={messageInputRef} | ||
onChangeText={value => handleTopicChange(value)} | ||
onFocus={handleTopicFocus} | ||
onSubmitEditing={() => messageInputRef.current?.focus()} | ||
blurOnSubmit={false} | ||
returnKeyType="next" | ||
style={styles.topicInput} | ||
/> | ||
<TopicAutocomplete | ||
isFocused={topicFocus} | ||
narrow={props.route.params.messageNarrow} | ||
text={subject} | ||
onAutocomplete={handleTopicAutocomplete} | ||
/> | ||
</View> | ||
<Text style={{ fontSize: 14, color: 'gray', marginBottom: 10 }}>Move options:</Text> | ||
<View style={styles.picker}> | ||
<Picker | ||
selectedValue={propagate_mode} | ||
onValueChange={(itemValue, itemIndex) => setPropagateMode(itemValue.toString())} | ||
style={styles.textColor} | ||
> | ||
<Picker.Item label="Change only this message" value="change_one" key="change_one" /> | ||
<Picker.Item | ||
label="Change later messages to this topic" | ||
value="change_later" | ||
key="change_later" | ||
/> | ||
<Picker.Item | ||
label="Change previous and following messages to this topic" | ||
value="change_all" | ||
key="change_all" | ||
/> | ||
</Picker> | ||
</View> | ||
<TouchableOpacity onPress={updateMessage} style={styles.submitButton}> | ||
<Text style={{ textAlign: 'center' }}>Submit</Text> | ||
</TouchableOpacity> | ||
</View> | ||
</ScrollView> | ||
); | ||
} |
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