-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 Extract components and types from PhonebookScreen to reduce complexity #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TargetMisser
wants to merge
1
commit into
main
Choose a base branch
from
code-health/phonebook-refactor-1613122475562984015
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,180 @@ | ||
| import React, { useState, useEffect, useMemo } from 'react'; | ||
| import { | ||
| View, Text, StyleSheet, TouchableOpacity, ScrollView, | ||
| TextInput, Alert, Modal, Platform, KeyboardAvoidingView, | ||
| } from 'react-native'; | ||
| import { MaterialIcons } from '@expo/vector-icons'; | ||
| import { useAppTheme, type ThemeColors } from '../context/ThemeContext'; | ||
| import { useLanguage } from '../context/LanguageContext'; | ||
| import { Contact, CATEGORIES, CATEGORY_COLORS, genId } from '../types/phonebook'; | ||
|
|
||
| interface EditModalProps { | ||
| visible: boolean; | ||
| contact: Partial<Contact> | null; | ||
| onSave: (c: Contact) => void; | ||
| onClose: () => void; | ||
| } | ||
|
|
||
| function makeModalStyles(c: ThemeColors) { | ||
| return StyleSheet.create({ | ||
| overlay: { | ||
| flex: 1, justifyContent: 'flex-end', | ||
| backgroundColor: 'rgba(15,23,42,0.5)', | ||
| }, | ||
| scrollContent: { flexGrow: 1, justifyContent: 'flex-end' }, | ||
| sheet: { | ||
| backgroundColor: c.card, | ||
| borderTopLeftRadius: 24, borderTopRightRadius: 24, | ||
| padding: 20, paddingBottom: 36, maxHeight: '92%', | ||
| }, | ||
| handle: { | ||
| width: 40, height: 4, borderRadius: 2, | ||
| backgroundColor: c.border, | ||
| alignSelf: 'center', marginBottom: 18, | ||
| }, | ||
| title: { fontSize: 18, fontWeight: '700', color: c.primaryDark, marginBottom: 16 }, | ||
| label: { fontSize: 12, fontWeight: '600', color: c.textSub, marginBottom: 6, marginTop: 12 }, | ||
| input: { | ||
| borderWidth: 1.5, borderColor: c.border, borderRadius: 12, | ||
| paddingHorizontal: 14, paddingVertical: 11, | ||
| fontSize: 15, color: c.text, backgroundColor: c.cardSecondary, | ||
| }, | ||
| catRow: { marginBottom: 4 }, | ||
| catChip: { | ||
| paddingHorizontal: 14, paddingVertical: 7, | ||
| borderRadius: 20, borderWidth: 1.5, borderColor: c.border, | ||
| marginRight: 8, backgroundColor: c.card, | ||
| }, | ||
| catTxt: { fontSize: 12, fontWeight: '600', color: c.textSub }, | ||
| actions: { flexDirection: 'row', gap: 10, marginTop: 20 }, | ||
| cancelBtn: { | ||
| flex: 1, borderWidth: 1.5, borderColor: c.border, | ||
| borderRadius: 12, paddingVertical: 13, alignItems: 'center', | ||
| }, | ||
| cancelTxt: { fontSize: 14, fontWeight: '600', color: c.textSub }, | ||
| saveBtn: { | ||
| flex: 2, backgroundColor: c.primary, | ||
| borderRadius: 12, paddingVertical: 13, | ||
| flexDirection: 'row', alignItems: 'center', justifyContent: 'center', gap: 8, | ||
| }, | ||
| saveTxt: { fontSize: 14, fontWeight: '700', color: '#fff' }, | ||
| }); | ||
| } | ||
|
|
||
| export function ContactEditModal({ visible, contact, onSave, onClose }: EditModalProps) { | ||
| const { colors } = useAppTheme(); | ||
| const { t } = useLanguage(); | ||
| const modalStyles = useMemo(() => makeModalStyles(colors), [colors]); | ||
| const [name, setName] = useState(''); | ||
| const [number, setNumber] = useState(''); | ||
| const [category, setCategory] = useState(CATEGORIES[0]); | ||
| const [note, setNote] = useState(''); | ||
|
|
||
| useEffect(() => { | ||
| if (contact) { | ||
| setName(contact.name ?? ''); | ||
| setNumber(contact.number ?? ''); | ||
| setCategory(contact.category ?? CATEGORIES[0]); | ||
| setNote(contact.note ?? ''); | ||
| } else { | ||
| setName(''); setNumber(''); setCategory(CATEGORIES[0]); setNote(''); | ||
| } | ||
| }, [contact, visible]); | ||
|
|
||
| const handleSave = () => { | ||
| if (!name.trim() || !number.trim()) { | ||
| Alert.alert(t('contactErrReqTitle'), t('contactErrReqMsg')); | ||
| return; | ||
| } | ||
| onSave({ | ||
| id: contact?.id ?? genId(), | ||
| name: name.trim(), | ||
| number: number.trim(), | ||
| category, | ||
| note: note.trim(), | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <Modal visible={visible} transparent animationType="slide" statusBarTranslucent onRequestClose={onClose}> | ||
| <KeyboardAvoidingView | ||
| style={modalStyles.overlay} | ||
| behavior={Platform.OS === 'ios' ? 'padding' : 'height'} | ||
| keyboardVerticalOffset={Platform.OS === 'ios' ? 24 : 0} | ||
| > | ||
| <ScrollView contentContainerStyle={modalStyles.scrollContent} keyboardShouldPersistTaps="handled" showsVerticalScrollIndicator={false}> | ||
| <View style={modalStyles.sheet}> | ||
| <View style={modalStyles.handle} /> | ||
|
|
||
| <Text style={modalStyles.title}> | ||
| {contact?.id ? t('contactModalEdit') : t('contactModalNew')} | ||
| </Text> | ||
|
|
||
| {/* Nome */} | ||
| <Text style={modalStyles.label}>{t('contactNameLabel')}</Text> | ||
| <TextInput | ||
| style={modalStyles.input} | ||
| value={name} | ||
| onChangeText={setName} | ||
| placeholder={t('contactNamePh')} | ||
| placeholderTextColor={colors.textSub} | ||
| autoCapitalize="words" | ||
| /> | ||
|
|
||
| {/* Numero */} | ||
| <Text style={modalStyles.label}>{t('contactNumberLabel')}</Text> | ||
| <TextInput | ||
| style={modalStyles.input} | ||
| value={number} | ||
| onChangeText={setNumber} | ||
| placeholder={t('contactNumberPh')} | ||
| placeholderTextColor={colors.textSub} | ||
| keyboardType="phone-pad" | ||
| /> | ||
|
|
||
| {/* Categoria */} | ||
| <Text style={modalStyles.label}>{t('contactCatLabel')}</Text> | ||
| <ScrollView horizontal showsHorizontalScrollIndicator={false} style={modalStyles.catRow}> | ||
| {CATEGORIES.map(cat => ( | ||
| <TouchableOpacity | ||
| key={cat} | ||
| style={[ | ||
| modalStyles.catChip, | ||
| category === cat && { backgroundColor: CATEGORY_COLORS[cat] }, | ||
| ]} | ||
| onPress={() => setCategory(cat)} | ||
| > | ||
| <Text style={[modalStyles.catTxt, category === cat && { color: '#fff' }]}> | ||
| {cat} | ||
| </Text> | ||
| </TouchableOpacity> | ||
| ))} | ||
| </ScrollView> | ||
|
|
||
| {/* Nota */} | ||
| <Text style={modalStyles.label}>{t('contactNoteLabel')}</Text> | ||
| <TextInput | ||
| style={[modalStyles.input, { height: 70, textAlignVertical: 'top' }]} | ||
| value={note} | ||
| onChangeText={setNote} | ||
| placeholder={t('contactNotePh')} | ||
| placeholderTextColor={colors.textSub} | ||
| multiline | ||
| /> | ||
|
|
||
| {/* Azioni */} | ||
| <View style={modalStyles.actions}> | ||
| <TouchableOpacity style={modalStyles.cancelBtn} onPress={onClose}> | ||
| <Text style={modalStyles.cancelTxt}>Annulla</Text> | ||
| </TouchableOpacity> | ||
| <TouchableOpacity style={modalStyles.saveBtn} onPress={handleSave}> | ||
| <MaterialIcons name="save" size={18} color="#fff" /> | ||
| <Text style={modalStyles.saveTxt}>Salva</Text> | ||
| </TouchableOpacity> | ||
| </View> | ||
| </View> | ||
| </ScrollView> | ||
| </KeyboardAvoidingView> | ||
| </Modal> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Contactis exported asexport type Contactfromsrc/types/phonebook.ts, so it should be imported as a type-only import here (import type { Contact } ...orimport { type Contact, ... }). This avoids emitting a runtime named import forContact, which can cause bundlers to error in stricter ESM environments.