|
| 1 | +// @flow |
| 2 | +import React, { useState, useContext, useMemo } from 'react'; |
| 3 | +import { Modal, Text, Pressable, View, TextInput, Platform } from 'react-native'; |
| 4 | +import type { JSX$Element } from 'tsflower/subst/react'; |
| 5 | +import { ThemeContext, BRAND_COLOR, createStyleSheet } from '../styles'; |
| 6 | +import { updateMessage } from '../api'; |
| 7 | +import type { Auth, Stream, GetText } from '../types'; |
| 8 | + |
| 9 | +type TopicEditModalArgs = { |
| 10 | + topicModalState: { |
| 11 | + visible: boolean, |
| 12 | + topic: string, |
| 13 | + fetchArgs: { |
| 14 | + auth: Auth, |
| 15 | + messageId: number, |
| 16 | + zulipFeatureLevel: number |
| 17 | + } |
| 18 | + }, |
| 19 | + topicModalHandler: { |
| 20 | + startEditTopic: ( |
| 21 | + streamId: number, |
| 22 | + topic: string, |
| 23 | + streamsById: Map<number, Stream>, |
| 24 | + _: GetText, |
| 25 | + ) => void, |
| 26 | + closeEditTopicModal: () => void, |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +export default function TopicEditModal({ topicModalState, topicModalHandler }:TopicEditModalArgs): JSX$Element { |
| 31 | + const { topic, fetchArgs } = topicModalState; |
| 32 | + const [topicState, onChangeTopic] = useState(topic); |
| 33 | + const { closeEditTopicModal } = topicModalHandler; |
| 34 | + const { auth, messageId, zulipFeatureLevel } = fetchArgs; |
| 35 | + |
| 36 | + const { backgroundColor } = useContext(ThemeContext); |
| 37 | + |
| 38 | + const inputMarginPadding = useMemo( |
| 39 | + () => ({ |
| 40 | + paddingHorizontal: 8, |
| 41 | + paddingVertical: Platform.select({ |
| 42 | + ios: 8, |
| 43 | + android: 2, |
| 44 | + }), |
| 45 | + }), |
| 46 | + [], |
| 47 | + ); |
| 48 | + |
| 49 | + const styles = createStyleSheet({ |
| 50 | + wrapper: { |
| 51 | + flex: 1, |
| 52 | + justifyContent: 'center', |
| 53 | + alignItems: 'center', |
| 54 | + }, |
| 55 | + modalView: { |
| 56 | + margin: 10, |
| 57 | + alignItems: 'center', |
| 58 | + backgroundColor, |
| 59 | + padding: 20, |
| 60 | + borderStyle: 'solid', |
| 61 | + borderWidth: 1, |
| 62 | + borderColor: 'gray', |
| 63 | + borderRadius: 20, |
| 64 | + width: '90%', |
| 65 | + }, |
| 66 | + buttonContainer: { |
| 67 | + display: 'flex', |
| 68 | + flexDirection: 'row', |
| 69 | + justifyContent: 'space-between', |
| 70 | + width: '60%', |
| 71 | + }, |
| 72 | + input: { |
| 73 | + width: '90%', |
| 74 | + borderWidth: 1, |
| 75 | + borderRadius: 5, |
| 76 | + marginBottom: 16, |
| 77 | + ...inputMarginPadding, |
| 78 | + backgroundColor: 'white', |
| 79 | + borderStyle: 'solid', |
| 80 | + borderColor: 'black', |
| 81 | + }, |
| 82 | + button: { |
| 83 | + position: 'relative', |
| 84 | + justifyContent: 'center', |
| 85 | + alignItems: 'center', |
| 86 | + borderRadius: 32, |
| 87 | + padding: 8, |
| 88 | + }, |
| 89 | + titleText: { |
| 90 | + fontSize: 18, |
| 91 | + lineHeight: 21, |
| 92 | + fontWeight: 'bold', |
| 93 | + color: BRAND_COLOR, |
| 94 | + marginBottom: 12, |
| 95 | + }, |
| 96 | + text: { |
| 97 | + fontSize: 14, |
| 98 | + lineHeight: 21, |
| 99 | + fontWeight: 'bold', |
| 100 | + }, |
| 101 | + submitButtonText: { |
| 102 | + color: 'white', |
| 103 | + }, |
| 104 | + cancelButtonText: { |
| 105 | + color: BRAND_COLOR, |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + const handleSubmit = async () => { |
| 110 | + await updateMessage(auth, messageId, { |
| 111 | + propagate_mode: 'change_all', |
| 112 | + subject: topicState, |
| 113 | + ...(zulipFeatureLevel >= 9 && { |
| 114 | + send_notification_to_old_thread: true, |
| 115 | + send_notification_to_new_thread: true, |
| 116 | + }), |
| 117 | + }); |
| 118 | + closeEditTopicModal(); |
| 119 | + }; |
| 120 | + return ( |
| 121 | + <Modal |
| 122 | + transparent |
| 123 | + visible={topicModalState.visible} |
| 124 | + animationType="slide" |
| 125 | + onRequestClose={() => { |
| 126 | + closeEditTopicModal(); |
| 127 | + }} |
| 128 | + > |
| 129 | + <View style={styles.wrapper}> |
| 130 | + <View style={styles.modalView}> |
| 131 | + <Text |
| 132 | + style={styles.titleText} |
| 133 | + > |
| 134 | + Edit topic |
| 135 | + </Text> |
| 136 | + <TextInput |
| 137 | + style={styles.input} |
| 138 | + value={topicState} |
| 139 | + onChangeText={onChangeTopic} |
| 140 | + selectTextOnFocus |
| 141 | + /> |
| 142 | + <View style={styles.buttonContainer}> |
| 143 | + <Pressable |
| 144 | + style={{ |
| 145 | + backgroundColor, |
| 146 | + borderStyle: 'solid', |
| 147 | + borderWidth: 2, |
| 148 | + borderColor: BRAND_COLOR, |
| 149 | + ...styles.button |
| 150 | + }} |
| 151 | + onPress={() => { |
| 152 | + closeEditTopicModal(); |
| 153 | + }} |
| 154 | + > |
| 155 | + <Text |
| 156 | + style={{ ...styles.text, ...styles.cancelButtonText }} |
| 157 | + > |
| 158 | + Cancel |
| 159 | + </Text> |
| 160 | + </Pressable> |
| 161 | + <Pressable |
| 162 | + style={{ |
| 163 | + opacity: !topicState ? 0.25 : 1, |
| 164 | + backgroundColor: BRAND_COLOR, |
| 165 | + ...styles.button |
| 166 | + }} |
| 167 | + onPress={handleSubmit} |
| 168 | + disabled={!topicState} |
| 169 | + > |
| 170 | + <Text |
| 171 | + style={{ ...styles.text, ...styles.submitButtonText }} |
| 172 | + > |
| 173 | + Submit |
| 174 | + </Text> |
| 175 | + </Pressable> |
| 176 | + </View> |
| 177 | + </View> |
| 178 | + </View> |
| 179 | + </Modal> |
| 180 | + ); |
| 181 | +} |
0 commit comments