import React, { useEffect, useState } from 'react'; import { FlightTable } from './flight-table'; import { Tabs } from '../tabs/tabs'; import { getFlights, Flight, Aircraft, getAircrafts, getUsers, User, } from '../../airtable-data'; export const FlightTableDemo1 = () => { const [users, setUsers] = useState([]); const [flights, setFlights] = useState([]); const [aircrafts, setAircrafts] = useState([]); useEffect(() => { getUsers().then(({ records }) => { setUsers(records); }); getFlights().then(({ records }) => { setFlights(records); }); getAircrafts().then(({ records }) => { setAircrafts(records); }); }, []); const isString = (string: string): string => typeof string === 'string' ? string : '-'; function checkTypes(input: any[] | string): string { if (Array.isArray(input)) { if (input.length > 0) { return isString(input[0]); } return ''; } return isString(input); } const columns = [ { Header: 'Date', accessor: 'date', }, { Header: 'Type', accessor: 'type', }, { Header: 'Aircraft', accessor: 'aircraft', }, { Header: 'Route', accessor: 'route', }, { Header: 'Dep - Arr', accessor: 'depArr', }, { Header: 'Pilot', accessor: 'pilot', }, { Header: 'Co-Pilot', accessor: 'coPilot', }, { // Make an expander cell Header: 'Actions', id: 'actions', // It needs an ID Cell: ({ row }) => ( // Use Cell to render an expander for each row. // We can use the getToggleRowExpandedProps prop-getter // to build the expander. View{' '} {row.isExpanded ? ( ) : ( )} ), }, ]; // Create a function that will render our row sub components const renderRowSubComponent = React.useCallback( ({ row }) => (
        {JSON.stringify({ values: row.values }, null, 2)}
      
), [], ); if (!flights && !aircrafts) return null; const data = flights.map(flight => { const dataSet = { date: checkTypes(flight.fields['Flight-date-short']), type: checkTypes(flight.fields['Mission Type']), aircraft: '-', route: checkTypes(flight.fields['Route-short']), depArr: '', pilot: '-', coPilot: '-', departureDate: flight.fields['Scheduled Departure'], }; // Check aircraft data and return aircraft name based on ID given in flight info const aircraftId = checkTypes(flight.fields.Aircraft); const aircraft = aircrafts.find(a => a.id === aircraftId); if (aircraft && typeof aircraft.fields['N-Number'] === 'number') { dataSet.aircraft = `N${aircraft.fields['N-Number']}`; } const pilotId = checkTypes(flight.fields.Pilot); const pilot = users.find(p => p.id === pilotId); // Check Pilot data and return full name based on ID given in flight info if ( pilot && typeof pilot.fields['First Name'] === 'string' && typeof pilot.fields['Last Name'] === 'string' ) { dataSet.pilot = `${pilot.fields['First Name']} ${pilot.fields['Last Name']}`; } const coPilotId = checkTypes(flight.fields['Co-Pilot']); const coPilot = users.find(p => p.id === coPilotId); // Check Co-Pilot data and return full name based on ID given in flight info if ( coPilot && typeof coPilot.fields['First Name'] === 'string' && typeof coPilot.fields['Last Name'] === 'string' ) { dataSet.coPilot = `${coPilot.fields['First Name']} ${coPilot.fields['Last Name']}`; } // Check departure and arival data and return combined Dep - Arr field const dep = checkTypes(flight.fields['Departure-short']); const arrival = checkTypes(flight.fields['Arrival-Short']); if (dep !== '-' && arrival !== '-') { dataSet.depArr = `${dep} - ${arrival}`; } else { dataSet.depArr = '-'; } return dataSet; }); const date = new Date(); const pastFlights = data.filter( flight => new Date(flight.departureDate) <= date, ); const futureFlights = data.filter( flight => new Date(flight.departureDate) > date, ); return ( ), }, { id: '2', title: 'History', children: ( ), }, ]} /> ); };