|
| 1 | +import React, { useEffect } from "react" |
| 2 | +import { collection, getFirestore, onSnapshot } from "firebase/firestore" |
| 3 | +import { |
| 4 | + Create, |
| 5 | + Datagrid, |
| 6 | + DateField, |
| 7 | + DateInput, |
| 8 | + Edit, |
| 9 | + EditButton, |
| 10 | + FunctionField, |
| 11 | + List, |
| 12 | + SelectInput, |
| 13 | + SimpleForm, |
| 14 | + TextField, |
| 15 | + TextInput, |
| 16 | + useRefresh |
| 17 | +} from "react-admin" |
| 18 | + |
| 19 | +const typeChoices = [ |
| 20 | + { id: "article", name: "Article" }, |
| 21 | + { id: "award", name: "Award" }, |
| 22 | + { id: "book", name: "Book" } |
| 23 | +] |
| 24 | + |
| 25 | +export function ListNews() { |
| 26 | + const firestore = getFirestore() |
| 27 | + const refresh = useRefresh() |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + const newsRef = collection(firestore, "news") |
| 31 | + const unsubscribe = onSnapshot( |
| 32 | + newsRef, |
| 33 | + () => refresh(), |
| 34 | + (e: Error) => console.log(e) |
| 35 | + ) |
| 36 | + |
| 37 | + return () => unsubscribe() |
| 38 | + }, [firestore, refresh]) |
| 39 | + |
| 40 | + return ( |
| 41 | + <List> |
| 42 | + <Datagrid rowClick="edit" bulkActionButtons={false}> |
| 43 | + <TextField source="id" label="News ID" /> |
| 44 | + <TextField source="title" label="Title" /> |
| 45 | + <TextField source="author" label="Author" /> |
| 46 | + <TextField source="type" label="Type" /> |
| 47 | + <DateField source="publishDate" label="Publish Date" /> |
| 48 | + <DateField source="createdAt" showTime /> |
| 49 | + <EditButton label="Edit" /> |
| 50 | + </Datagrid> |
| 51 | + </List> |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +export function EditNews() { |
| 56 | + return ( |
| 57 | + <Edit> |
| 58 | + <SimpleForm> |
| 59 | + <TextInput source="url" fullWidth /> |
| 60 | + <SelectInput source="type" choices={typeChoices} /> |
| 61 | + <TextInput source="author" /> |
| 62 | + <TextInput source="title" fullWidth /> |
| 63 | + <TextInput source="description" multiline fullWidth /> |
| 64 | + <DateInput source="publishDate" /> |
| 65 | + </SimpleForm> |
| 66 | + </Edit> |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +export function CreateNews() { |
| 71 | + return ( |
| 72 | + <Create |
| 73 | + redirect="list" |
| 74 | + transform={(data: Record<string, unknown>) => ({ |
| 75 | + ...data, |
| 76 | + createdAt: new Date() |
| 77 | + })} |
| 78 | + > |
| 79 | + <SimpleForm> |
| 80 | + <TextInput source="url" fullWidth /> |
| 81 | + <SelectInput source="type" choices={typeChoices} /> |
| 82 | + <TextInput source="author" /> |
| 83 | + <TextInput source="title" fullWidth /> |
| 84 | + <TextInput source="description" multiline fullWidth /> |
| 85 | + <DateInput source="publishDate" /> |
| 86 | + </SimpleForm> |
| 87 | + </Create> |
| 88 | + ) |
| 89 | +} |
| 90 | + |
| 91 | +export default { ListNews, EditNews, CreateNews } |
0 commit comments