|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { Button, Typography } from '@mui/material'; |
| 3 | + |
| 4 | +import FormInput from './FormInput'; |
| 5 | +import SheetTable from './SheetTable'; |
| 6 | + |
| 7 | + |
| 8 | +// This is a wrapper for google.script.run that lets us use promises. |
| 9 | +import { serverFunctions } from '../../utils/serverFunctions'; |
| 10 | + |
| 11 | +const SheetEditor = () => { |
| 12 | + const [names, setNames] = useState([]); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + serverFunctions.getSheetsData().then(setNames).catch(alert); |
| 16 | + }, []); |
| 17 | + |
| 18 | + const deleteSheet = (sheetIndex) => { |
| 19 | + serverFunctions.deleteSheet(sheetIndex).then(setNames).catch(alert); |
| 20 | + }; |
| 21 | + |
| 22 | + const setActiveSheet = (sheetName) => { |
| 23 | + serverFunctions.setActiveSheet(sheetName).then(setNames).catch(alert); |
| 24 | + }; |
| 25 | + |
| 26 | + const submitNewSheet = async (newSheetName) => { |
| 27 | + try { |
| 28 | + const response = await serverFunctions.addSheet(newSheetName); |
| 29 | + setNames(response); |
| 30 | + } catch (error) { |
| 31 | + // eslint-disable-next-line no-alert |
| 32 | + alert(error); |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + return ( |
| 37 | + <div style={{ padding: '3px', overflowX: 'hidden' }}> |
| 38 | + <Typography variant="h4" gutterBottom> |
| 39 | + ☀️ MUI demo! ☀️ |
| 40 | + </Typography> |
| 41 | + |
| 42 | + <Typography variant="body1" gutterBottom sx={{ marginBottom: '30px' }}> |
| 43 | + This is a sample app that uses the <code>mui</code> library |
| 44 | + to help us build a simple React app. Enter a name for a new sheet, hit |
| 45 | + enter and the new sheet will be created. Click the red button next to the sheet name to |
| 46 | + delete it. |
| 47 | + </Typography> |
| 48 | + <FormInput submitNewSheet={submitNewSheet} /> |
| 49 | + |
| 50 | + {names.length > 0 && |
| 51 | + <SheetTable rows={names.map((name) => { |
| 52 | + return { |
| 53 | + sheetName: name.name, |
| 54 | + goToButton: <Button |
| 55 | + variant="outlined" |
| 56 | + disabled={name?.isActive} |
| 57 | + onClick={() => setActiveSheet(name.name)} |
| 58 | + >Go To Sheet</Button>, |
| 59 | + deleteButton: <Button |
| 60 | + variant="contained" |
| 61 | + color="error" |
| 62 | + onClick={() => deleteSheet(name.index)} |
| 63 | + >Delete</Button> |
| 64 | + } |
| 65 | + })} />} |
| 66 | + </div> |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +export default SheetEditor; |
0 commit comments