Files
gas-react-sheets/src/client/dialog-demo/components/SheetEditor.jsx
T
Elisha Nuchi 531a952df3 Support react-refresh and shared client/server types (#98)
- Adds support for react-refresh through `@pmmwh/react-refresh-webpack-plugin`. Updated webpack config to support this. Means entire app is no longer refreshed on changes, only individual components, and state is maintained.
- React refresh removes need for `google-apps-script-webpack-dev-server` package
- In client code, support for server function autocomplete through use of gas-client 1.0.0. Requires certain files to be typescript .ts files
- Remove need for "global" exports in main server file through updated gas-webpack-plugin
2022-03-14 12:31:52 -04:00

78 lines
2.2 KiB
React
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React, { useState, useEffect } from 'react';
import { TransitionGroup, CSSTransition } from 'react-transition-group';
import FormInput from './FormInput';
import SheetButton from './SheetButton';
// This is a wrapper for google.script.run that lets us use promises.
import { serverFunctions } from '../../utils/serverFunctions';
const SheetEditor = () => {
const [names, setNames] = useState([]);
useEffect(() => {
// Call a server global function here and handle the response with .then() and .catch()
serverFunctions
.getSheetsData()
.then(setNames)
.catch(alert);
}, []);
const deleteSheet = sheetIndex => {
serverFunctions
.deleteSheet(sheetIndex)
.then(setNames)
.catch(alert);
};
const setActiveSheet = sheetName => {
serverFunctions
.setActiveSheet(sheetName)
.then(setNames)
.catch(alert);
};
// You can also use async/await notation for server calls with our server wrapper.
// (This does the same thing as .then().catch() in the above handlers.)
const submitNewSheet = async newSheetName => {
try {
const response = await serverFunctions.addSheet(newSheetName);
setNames(response);
} catch (error) {
// eslint-disable-next-line no-alert
alert(error);
}
};
return (
<div>
<p>
<b>☀️ React demo! ☀️</b>
</p>
<p>
This is a sample page that demonstrates a simple React app. Enter a name
for a new sheet, hit enter and the new sheet will be created. Click the
red &times; next to the sheet name to delete it.
</p>
<FormInput submitNewSheet={submitNewSheet} />
<TransitionGroup className="sheet-list">
{names.length > 0 &&
names.map(name => (
<CSSTransition
classNames="sheetNames"
timeout={500}
key={name.name}
>
<SheetButton
sheetDetails={name}
deleteSheet={deleteSheet}
setActiveSheet={setActiveSheet}
/>
</CSSTransition>
))}
</TransitionGroup>
</div>
);
};
export default SheetEditor;