-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Squashing View Event Commits Event Page #32
- Loading branch information
1 parent
c76de12
commit 4edc865
Showing
36 changed files
with
602 additions
and
120 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
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,54 @@ | ||
import React from 'react'; | ||
import axios from 'axios'; | ||
import { IPAddr, repeatingData } from './constants'; | ||
import { cLog } from './log' | ||
import GenericViewPageForm from './viewEventPage'; | ||
import { RouteProp, useRoute } from '@react-navigation/native'; | ||
import { RootStackParamList } from '@/components/Types'; | ||
|
||
type ViewEventsRouteProp = RouteProp<RootStackParamList, 'viewCalendarEvents'>; | ||
|
||
export default function ViewCalendarEvents() { | ||
const route = useRoute<ViewEventsRouteProp>(); | ||
const { event } = route.params; | ||
cLog("Passed event:", event); | ||
const fields = [ | ||
{ name: 'title', label: 'Title', type: 'text' }, | ||
{ name: 'event_date', label: 'Date', type: 'date' }, | ||
{ name: 'event_time', label: 'Time', type: 'time' }, | ||
{ name: 'description', label: 'Description', type: 'textarea' }, | ||
{ name: 'repeat_timeline', label: 'Repeating', type: 'dropdown', options: repeatingData }, | ||
]; | ||
const handleSave = async (saveData: any) => { | ||
try { | ||
cLog("Save Data:", saveData); | ||
const hit = IPAddr + '/update_calendar_event'; | ||
cLog('Updating event with:' + hit); | ||
const response = await axios.put(hit, saveData); | ||
cLog('Event updated successfully:' + response.data); | ||
} catch (error) { | ||
console.error('Error updating event:', error); | ||
} | ||
}; | ||
|
||
let eventDate = event.event.event_date; | ||
if (event.event.event_date != event.event.event_time) { | ||
const dateTimeString = `${event.event.event_date}T${event.event.event_time}`; | ||
eventDate = new Date(dateTimeString); | ||
} | ||
|
||
if (eventDate) event.event['event_date'] = eventDate; | ||
if (eventDate) event.event['event_time'] = eventDate; | ||
if (event.event['repeat_timeline']) event.event['repeat_timeline'] = parseInt(event.event['repeat_timeline'], 10); | ||
if (event.event['repeat_timeline'] !== undefined) event.event['repeating'] = event.event['repeat_timeline'] !== 0; | ||
|
||
return ( | ||
<GenericViewPageForm | ||
title="Calendar Event" | ||
initialData={event.event} | ||
fields={fields} | ||
mainPage='calendarEvents' | ||
onSave={handleSave} | ||
/> | ||
); | ||
} |
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,175 @@ | ||
import React, { useState } from 'react'; | ||
import { View, TextInput, Text, TouchableOpacity, ScrollView } from 'react-native'; | ||
import DateTimePicker from '@react-native-community/datetimepicker'; | ||
import { Dropdown } from 'react-native-element-dropdown'; | ||
import { styles } from './styles'; | ||
import { RootStackParamList } from '@/components/Types'; | ||
import { StackNavigationProp } from '@react-navigation/stack'; | ||
import { useNavigation } from '@react-navigation/native'; | ||
import MultiSelect from 'react-native-multiple-select'; | ||
|
||
interface FormProps { | ||
title: string; | ||
initialData: any; | ||
fields: Array<{ name: string; label: string; type: string; options?: any }>; | ||
mainPage: keyof RootStackParamList; | ||
onSave: (data: any) => void; | ||
} | ||
|
||
const GenericViewPageForm: React.FC<FormProps> = ({ title, initialData, fields, mainPage, onSave }) => { | ||
const [formData, setFormData] = useState(initialData); | ||
const [showDatePicker, setShowDatePicker] = useState(false); | ||
const [showTimePicker, setShowTimePicker] = useState(false); | ||
const [currentField, setCurrentField] = useState<string | null>(null); // Tracks which field is being edited | ||
|
||
type TrackerNavigationProp = StackNavigationProp<RootStackParamList, keyof RootStackParamList>; | ||
const navigation = useNavigation<TrackerNavigationProp>(); | ||
|
||
const handleChange = (name: string, value: any) => { | ||
setFormData({ ...formData, [name]: value }); | ||
}; | ||
|
||
const handleDateChange = (name: string, selectedDate: any) => { | ||
if (selectedDate) { | ||
handleChange(name, selectedDate); | ||
setShowDatePicker(false); | ||
} | ||
}; | ||
|
||
const handleTimeChange = (name: string, event: any, selectedTime: any) => { | ||
if (event.type === 'dismissed') { | ||
setShowTimePicker(false); | ||
} else if (selectedTime) { | ||
handleChange(name, selectedTime); | ||
} | ||
}; | ||
|
||
const handleSave = () => { | ||
const formattedData = { | ||
...formData, | ||
event_date: formData.event_date?.toISOString().split('T')[0], | ||
event_time: formData.event_time?.toTimeString().split(' ')[0], | ||
}; | ||
onSave(formattedData); | ||
navigation.navigate(mainPage as any); | ||
}; | ||
|
||
const showPicker = (type: 'date' | 'time', fieldName: string) => { | ||
setCurrentField(fieldName); | ||
if (type === 'date') { | ||
setShowDatePicker(true); | ||
} else { | ||
setShowTimePicker(true); | ||
} | ||
}; | ||
|
||
const handleIngredientChange = (selectedItems: string[]) => { | ||
handleChange('ingredients', selectedItems); | ||
}; | ||
return ( | ||
<ScrollView contentContainerStyle={styles.addContainer}> | ||
<Text style={styles.sectionHeader}>{title}</Text> | ||
<View> | ||
{fields.map((field, index) => ( | ||
<View key={index} style={styles.inputContainer}> | ||
<View style={field.type === 'textarea' ? styles.inLineDescription : styles.inLine}> | ||
<Text style={styles.inputText}>{field.label}</Text> | ||
{field.type === 'text' && ( | ||
<TextInput | ||
style={[styles.input, { height: 50 }]} | ||
value={formData[field.name]} | ||
onChangeText={(text) => handleChange(field.name, text)} | ||
placeholder={field.label} | ||
/> | ||
)} | ||
{field.type === 'date' && ( | ||
<View style={styles.dateTimeInLine}> | ||
<TouchableOpacity onPress={() => showPicker('date', field.name)}> | ||
<Text style={{ textAlign: 'left' }}>{formData[field.name] ? formData[field.name].toDateString() : 'Select Date'}</Text> | ||
</TouchableOpacity> | ||
{showDatePicker && ( | ||
<DateTimePicker | ||
value={currentField ? formData[currentField] || new Date() : new Date()} | ||
mode="date" | ||
display="default" | ||
onChange={(event, selectedDate) => handleDateChange(currentField!, selectedDate)} | ||
/> | ||
)} | ||
</View> | ||
)} | ||
{field.type === 'multi-select' && ( | ||
<MultiSelect | ||
items={initialData.allIngredients} // Ensure ingredients is an array | ||
uniqueKey="value" | ||
selectedItems={formData[field.name] || initialData.ingredients} // Default to an empty array | ||
onSelectedItemsChange={(selectedItems) => handleIngredientChange(selectedItems)} | ||
selectText="Select Ingredients" | ||
searchInputPlaceholderText="Search Ingredients..." | ||
displayKey="label" | ||
styleDropdownMenuSubsection={styles.dropdown} | ||
/> | ||
)} | ||
{field.type === 'time' && ( | ||
<View style={styles.dateTimeInLine}> | ||
<TouchableOpacity onPress={() => showPicker('time', field.name)}> | ||
<Text style={{ textAlign: 'left' }}>{formData[field.name] ? formData[field.name].toLocaleTimeString() : 'Select Time'}</Text> | ||
</TouchableOpacity> | ||
{showTimePicker && ( | ||
<DateTimePicker | ||
minuteInterval={15} | ||
value={currentField ? formData[currentField] || new Date() : new Date()} | ||
mode="time" | ||
display="default" | ||
onChange={(event, selectedTime) => handleTimeChange(currentField!, event, selectedTime)} | ||
/> | ||
)} | ||
</View> | ||
)} | ||
{field.type === 'dropdown' && ( | ||
<Dropdown | ||
style={[styles.dropdown, { height: 50, width: '80%' }]} | ||
data={field.options} | ||
maxHeight={300} | ||
labelField="label" | ||
valueField="value" | ||
value={formData[field.name]} | ||
onChange={(item) => handleChange(field.name, item.value)} | ||
placeholder="Select" | ||
/> | ||
)} | ||
|
||
{field.type === 'textarea' && ( | ||
<TextInput | ||
style={styles.bigInput} | ||
value={formData[field.name]} | ||
onChangeText={(text) => handleChange(field.name, text)} | ||
multiline | ||
placeholder={field.label} | ||
/> | ||
)} | ||
{field.type === 'number' && ( | ||
<TextInput | ||
style={[styles.input, { height: 50 }]} | ||
value={formData[field.name]} | ||
onChangeText={(text) => handleChange(field.name, text)} | ||
placeholder={field.label} | ||
keyboardType="numeric" | ||
/> | ||
)} | ||
</View> | ||
</View> | ||
))} | ||
</View> | ||
<View style={styles.saveCancelContainer}> | ||
<TouchableOpacity style={styles.saveButton} onPress={handleSave}> | ||
<Text style={styles.saveText}>Save</Text> | ||
</TouchableOpacity> | ||
<TouchableOpacity style={styles.cancelButton} onPress={() => navigation.navigate(mainPage as any)}> | ||
<Text style={styles.cancelText}>Cancel</Text> | ||
</TouchableOpacity> | ||
</View> | ||
</ScrollView> | ||
); | ||
}; | ||
|
||
export default GenericViewPageForm; |
Oops, something went wrong.