|
| 1 | +import React from 'react'; |
| 2 | +import { View, Text, TextInput, StyleSheet } from 'react-native'; |
| 3 | +import NLModal from './Modal'; |
| 4 | +import Button from './Button'; |
| 5 | +import { useAppContext } from './AppContext'; |
| 6 | +import { Theme, useTheme, useThemedStyles } from '../theme'; |
| 7 | + |
| 8 | +const FilterButton = ({ |
| 9 | + onPress, |
| 10 | + active, |
| 11 | + children, |
| 12 | +}: { |
| 13 | + onPress: () => void; |
| 14 | + active?: boolean; |
| 15 | + children: string; |
| 16 | +}) => { |
| 17 | + const styles = useThemedStyles(themedStyles); |
| 18 | + |
| 19 | + return ( |
| 20 | + <Button |
| 21 | + style={[styles.methodButton, active && styles.buttonActive]} |
| 22 | + textStyle={[styles.buttonText, active && styles.buttonActiveText]} |
| 23 | + onPress={onPress} |
| 24 | + accessibilityRole="checkbox" |
| 25 | + accessibilityState={{ checked: active }} |
| 26 | + > |
| 27 | + {children} |
| 28 | + </Button> |
| 29 | + ); |
| 30 | +}; |
| 31 | + |
| 32 | +const Filters = ({ open, onClose }: { open: boolean; onClose: () => void }) => { |
| 33 | + const { filter, dispatch } = useAppContext(); |
| 34 | + const styles = useThemedStyles(themedStyles); |
| 35 | + const theme = useTheme(); |
| 36 | + |
| 37 | + const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'] as const; |
| 38 | + |
| 39 | + return ( |
| 40 | + <View> |
| 41 | + <NLModal visible={open} onClose={onClose} title="Filters"> |
| 42 | + <Text style={styles.subTitle} accessibilityRole="header"> |
| 43 | + Method |
| 44 | + </Text> |
| 45 | + <View style={styles.methods}> |
| 46 | + {methods.map((method) => ( |
| 47 | + <FilterButton |
| 48 | + key={method} |
| 49 | + active={filter.methods?.has(method)} |
| 50 | + onPress={() => { |
| 51 | + const newMethods = new Set(filter.methods); |
| 52 | + if (newMethods.has(method)) { |
| 53 | + newMethods.delete(method); |
| 54 | + } else { |
| 55 | + newMethods.add(method); |
| 56 | + } |
| 57 | + |
| 58 | + dispatch({ |
| 59 | + type: 'SET_FILTER', |
| 60 | + payload: { |
| 61 | + ...filter, |
| 62 | + methods: newMethods, |
| 63 | + }, |
| 64 | + }); |
| 65 | + }} |
| 66 | + > |
| 67 | + {method} |
| 68 | + </FilterButton> |
| 69 | + ))} |
| 70 | + </View> |
| 71 | + <Text style={styles.subTitle} accessibilityRole="header"> |
| 72 | + Status |
| 73 | + </Text> |
| 74 | + <View style={styles.methods}> |
| 75 | + <FilterButton |
| 76 | + active={filter.statusErrors} |
| 77 | + onPress={() => { |
| 78 | + dispatch({ |
| 79 | + type: 'SET_FILTER', |
| 80 | + payload: { |
| 81 | + ...filter, |
| 82 | + statusErrors: !filter.statusErrors, |
| 83 | + status: undefined, |
| 84 | + }, |
| 85 | + }); |
| 86 | + }} |
| 87 | + > |
| 88 | + Errors |
| 89 | + </FilterButton> |
| 90 | + <TextInput |
| 91 | + style={styles.statusInput} |
| 92 | + placeholder="Status Code" |
| 93 | + placeholderTextColor={theme.colors.muted} |
| 94 | + keyboardType="number-pad" |
| 95 | + value={filter.status?.toString() || ''} |
| 96 | + maxLength={3} |
| 97 | + accessibilityLabel="Status Code" |
| 98 | + onChangeText={(text) => { |
| 99 | + const status = parseInt(text, 10); |
| 100 | + dispatch({ |
| 101 | + type: 'SET_FILTER', |
| 102 | + payload: { |
| 103 | + ...filter, |
| 104 | + statusErrors: false, |
| 105 | + status: isNaN(status) ? undefined : status, |
| 106 | + }, |
| 107 | + }); |
| 108 | + }} |
| 109 | + /> |
| 110 | + </View> |
| 111 | + <View style={styles.divider} /> |
| 112 | + <Button |
| 113 | + textStyle={styles.clearButton} |
| 114 | + onPress={() => { |
| 115 | + dispatch({ |
| 116 | + type: 'CLEAR_FILTER', |
| 117 | + }); |
| 118 | + onClose(); |
| 119 | + }} |
| 120 | + > |
| 121 | + Reset All Filters |
| 122 | + </Button> |
| 123 | + </NLModal> |
| 124 | + </View> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +const themedStyles = (theme: Theme) => |
| 129 | + StyleSheet.create({ |
| 130 | + subTitle: { |
| 131 | + color: theme.colors.text, |
| 132 | + fontSize: 16, |
| 133 | + fontWeight: 'bold', |
| 134 | + marginBottom: 8, |
| 135 | + }, |
| 136 | + filterValue: { |
| 137 | + fontWeight: 'bold', |
| 138 | + }, |
| 139 | + methods: { |
| 140 | + flexDirection: 'row', |
| 141 | + flexWrap: 'wrap', |
| 142 | + marginBottom: 10, |
| 143 | + }, |
| 144 | + methodButton: { |
| 145 | + margin: 2, |
| 146 | + borderWidth: 1, |
| 147 | + borderRadius: 10, |
| 148 | + borderColor: theme.colors.secondary, |
| 149 | + }, |
| 150 | + statusInput: { |
| 151 | + color: theme.colors.text, |
| 152 | + marginLeft: 10, |
| 153 | + borderColor: theme.colors.secondary, |
| 154 | + padding: 5, |
| 155 | + borderBottomWidth: 1, |
| 156 | + minWidth: 100, |
| 157 | + }, |
| 158 | + buttonText: { |
| 159 | + fontSize: 12, |
| 160 | + }, |
| 161 | + buttonActive: { |
| 162 | + backgroundColor: theme.colors.secondary, |
| 163 | + }, |
| 164 | + buttonActiveText: { |
| 165 | + color: theme.colors.onSecondary, |
| 166 | + }, |
| 167 | + clearButton: { |
| 168 | + color: theme.colors.statusBad, |
| 169 | + }, |
| 170 | + divider: { |
| 171 | + height: 1, |
| 172 | + backgroundColor: theme.colors.muted, |
| 173 | + marginTop: 20, |
| 174 | + }, |
| 175 | + }); |
| 176 | + |
| 177 | +export default Filters; |
0 commit comments