Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/693f3576-eaf4-8011-9c02-8476c83275fd
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== This adds: ==== * Search (email contains) * Division filter (All + your list) * Field filter (All + 1β8) * Solo filter (All/Yes/No) * Date filter (Today / Last 7 days / All) * Export CSV (exports the currently filtered results) ===== <syntaxhighlight lang="js">import * as FileSystem from "expo-file-system"; ===== import * as Sharing from "expo-sharing"; // ... keep your other imports ... function AdminScreen({ user, role }) { const [allCheckins, setAllCheckins] = useState([]); const [loading, setLoading] = useState(true); // Filters const [searchEmail, setSearchEmail] = useState(""); const [divisionFilter, setDivisionFilter] = useState("All"); const [fieldFilter, setFieldFilter] = useState("All"); const [soloFilter, setSoloFilter] = useState("All"); // All | Yes | No const [dateFilter, setDateFilter] = useState("All"); // All | Today | Last7 useEffect(() => { // Pull a reasonable recent window to avoid huge reads; increase if needed. const qy = query(collection(db, "checkins"), orderBy("createdAt", "desc"), limit(500)); const unsub = onSnapshot( qy, (snap) => { setAllCheckins(snap.docs.map((d) => ({ id: d.id, ...d.data() }))); setLoading(false); }, () => setLoading(false) ); return () => unsub(); }, []); const filtered = useMemo(() => { const emailNeedle = searchEmail.trim().toLowerCase(); const now = new Date(); const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()); const startOfLast7 = new Date(startOfToday.getTime() - 7 '' 24 '' 60 '' 60 '' 1000); return allCheckins.filter((c) => { const email = (c.email || "").toLowerCase(); // Email search if (emailNeedle && !email.includes(emailNeedle)) return false; // Division if (divisionFilter !== "All" && c.division !== divisionFilter) return false; // Field if (fieldFilter !== "All" && Number(c.field) !== Number(fieldFilter)) return false; // Solo if (soloFilter !== "All" && c.soloUmpire !== soloFilter) return false; // Date const created = c.createdAt?.toDate?.() ?? null; if (dateFilter === "Today") { if (!created || created < startOfToday) return false; } if (dateFilter === "Last7") { if (!created || created < startOfLast7) return false; } return true; }); }, [allCheckins, searchEmail, divisionFilter, fieldFilter, soloFilter, dateFilter]); const toCsv = (rows) => { const header = [ "createdAt", "email", "division", "field", "soloUmpire", "distanceMeters", "lat", "lng", "userId", "docId", ]; const escape = (v) => { const s = String(v ?? ""); // CSV escape: wrap in quotes if contains comma, quote, or newline if (/[",\n]/.test(s)) return <code>"${s.replace(/"/g, '""')}"</code>; return s; }; const lines = [header.join(",")]; for (const c of rows) { const created = c.createdAt?.toDate?.() ?? null; const line = [ created ? created.toISOString() : "", c.email ?? "", c.division ?? "", c.field ?? "", c.soloUmpire ?? "", c.distanceMeters ?? "", c.location?.lat ?? "", c.location?.lng ?? "", c.userId ?? "", c.id ?? "", ].map(escape); lines.push(line.join(",")); } return lines.join("\n"); }; const exportCsv = async () => { try { if (!filtered.length) { Alert.alert("Nothing to export", "Your current filter returns no results."); return; } const csv = toCsv(filtered); const filename = <code>pbysa_checkins_${new Date().toISOString().slice(0, 10)}.csv</code>; const path = FileSystem.cacheDirectory + filename; await FileSystem.writeAsStringAsync(path, csv, { encoding: FileSystem.EncodingType.UTF8, }); if (!(await Sharing.isAvailableAsync())) { Alert.alert( "Sharing not available", <code>CSV saved to:\n${path}\n\n(Sharing isn't available on this device.)</code> ); return; } await Sharing.shareAsync(path, { mimeType: "text/csv", dialogTitle: "Export Check-Ins CSV", }); } catch (e) { Alert.alert("Export failed", e?.message ?? "Could not export CSV."); } }; const logout = async () => { try { await signOut(auth); } catch (e) { Alert.alert("Logout error", e?.message ?? "Could not log out."); } }; return ( <SafeAreaView style={styles.container}> <Text style={styles.title}>Admin β’ Check-Ins</Text> <Text style={styles.subtitle}> {user.email} β’ {role} β’ Showing {filtered.length} / {allCheckins.length} </Text> <View style={styles.card}> <Text style={styles.label}>Search by email</Text> <TextInput value={searchEmail} onChangeText={setSearchEmail} placeholder="type part of an emailβ¦" autoCapitalize="none" style={styles.input} /> <Text style={styles.label}>Division</Text> <View style={styles.pickerWrap}> <Picker selectedValue={divisionFilter} onValueChange={setDivisionFilter}> <Picker.Item label="All" value="All" /> {DIVISIONS.map((d) => ( <Picker.Item key={d} label={d} value={d} /> ))} </Picker> </View> <Text style={styles.label}>Field</Text> <View style={styles.pickerWrap}> <Picker selectedValue={fieldFilter} onValueChange={setFieldFilter}> <Picker.Item label="All" value="All" /> {FIELDS.map((f) => ( <Picker.Item key={String(f)} label={String(f)} value={String(f)} /> ))} </Picker> </View> <Text style={styles.label}>Solo</Text> <View style={styles.pickerWrap}> <Picker selectedValue={soloFilter} onValueChange={setSoloFilter}> <Picker.Item label="All" value="All" /> <Picker.Item label="Yes" value="Yes" /> <Picker.Item label="No" value="No" /> </Picker> </View> <Text style={styles.label}>Date</Text> <View style={styles.pickerWrap}> <Picker selectedValue={dateFilter} onValueChange={setDateFilter}> <Picker.Item label="All" value="All" /> <Picker.Item label="Today" value="Today" /> <Picker.Item label="Last 7 days" value="Last7" /> </Picker> </View> <Button title="Export filtered CSV" onPress={exportCsv} /> <View style={{ height: 10 }} /> <Button title="Log Out" onPress={logout} variant="secondary" /> </View> <Text style={[styles.sectionTitle, { marginTop: 14 }]}>Filtered results</Text> {loading ? ( <ActivityIndicator style={{ marginTop: 12 }} /> ) : ( <FlatList data={filtered} keyExtractor={(item) => item.id} ListEmptyComponent={<Text style={styles.hint}>No check-ins match these filters.</Text>} renderItem={({ item }) => ( <View style={styles.listRow}> <Text style={styles.listRowTitle}> {item.email || item.userId} β’ {item.division} β’ Field {item.field} β’ Solo:{" "} {item.soloUmpire} </Text> <Text style={styles.listRowSub}> {formatWhen(item.createdAt)} β’ {Math.round(item.distanceMeters ?? 0)}m </Text> </View> )} /> )} </SafeAreaView> ); } </syntaxhighlight>
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)