Skip to content

Instantly share code, notes, and snippets.

@anderskitson
Last active March 12, 2021 18:39
Show Gist options
  • Save anderskitson/011a877f26563aa85ff27cc5278d104b to your computer and use it in GitHub Desktop.
Save anderskitson/011a877f26563aa85ff27cc5278d104b to your computer and use it in GitHub Desktop.
editable table
import { gql, useQuery } from "@apollo/client";
import useSWR from "swr";
import Layout from "../../components/Layout";
import React from "react";
import { useTable, usePagination } from "react-table";
import styled from "styled-components";
const DECOR_VALUES = gql`
query GetDecorValues($id: ID!) {
findUserByID(id: $id) {
decor {
data {
purchaseDate
description
alterations
cost
pieces
category
purchaser
image
}
}
}
}
`;
const Styles = styled.div`
padding: 1rem;
table {
border-spacing: 0;
border: 1px solid black;
tr {
:last-child {
td {
border-bottom: 0;
}
}
}
th,
td {
margin: 0;
padding: 0.5rem;
border-bottom: 1px solid black;
border-right: 1px solid black;
:last-child {
border-right: 0;
}
input {
font-size: 1rem;
padding: 0;
margin: 0;
border: 0;
}
}
}
.pagination {
padding: 0.5rem;
}
`;
// Create an editable cell renderer
const EditableCell = ({
value: initialValue,
row: { index },
column: { id },
updateMyData, // This is a custom function that we supplied to our table instance
}) => {
// We need to keep and update the state of the cell normally
const [value, setValue] = React.useState(initialValue);
const onChange = (e) => {
setValue(e.target.value);
};
// We'll only update the external data when the input is blurred
const onBlur = () => {
updateMyData(index, id, value);
};
// If the initialValue is changed external, sync it up with our state
React.useEffect(() => {
setValue(initialValue);
}, [initialValue]);
return <input value={value} onChange={onChange} onBlur={onBlur} />;
};
// Set our editable cell renderer as the default Cell renderer
const defaultColumn = {
Cell: EditableCell,
};
const fetcher = (url) => fetch(url).then((r) => r.json());
export default function DecorData() {
const { data: user, error: userError } = useSWR("/api/user", fetcher);
const { data: cookieData, error: cookieError } = useSWR(
"/api/cookie",
fetcher
);
var cookieBearer = `Bearer ${cookieData}`;
return (
<Layout>
<h1>View your Decor Catalog Table Here</h1>
{user && cookieBearer && (
<Table user={user} cookieBearer={cookieBearer} />
)}
</Layout>
);
}
const Table = ({ user, cookieBearer }) => {
const { loading, error, data: decorData } = useQuery(DECOR_VALUES, {
variables: { id: user.id },
context: {
headers: {
authorization: cookieBearer,
},
},
});
const massaged = decorData?.findUserByID?.decor?.data?.map((item) => {
var col = Object.values(item);
return col.map((colItem, i) => {
return { [`col${i}`]: colItem };
});
});
const massaged1 = decorData?.findUserByID?.decor?.data?.map((item, int) => {
var col = Object.keys(item);
return col?.map((colItem, i) => {
console.log(colItem);
if (colItem === "image") {
return {
Header: colItem,
accessor: `col${i}`,
Cell: ({ cell: { value } }) => <img src={value} width="400" />,
};
}
if (colItem === "alterations") {
return {
Header: colItem,
accessor: `col${i}`,
Cell: ({ cell: { value } }) => (
<>
<input type="checkbox" defaultChecked={value} />
</>
),
};
}
return {
Header: colItem,
accessor: `col${i}`,
};
});
});
const result = massaged?.map((a) => Object.assign({}, ...a));
// console.log(result);
return (
<>
{result && massaged1 && <TryThis result={result} massaged1={massaged1} />}
</>
);
};
function TryThis({ result, massaged1 }) {
const [data, setData] = React.useState(result); //bad naming here
// const [originalData] = React.useState(data)
const [skipPageReset, setSkipPageReset] = React.useState(false);
const updateMyData = (rowIndex, columnId, value) => {
// We also turn on the flag to not reset the page
setSkipPageReset(true);
setData((old) => {
// console.log(old);
old.map((row, index) => {
if (index === rowIndex) {
return {
...old[rowIndex],
[columnId]: value,
};
}
return row;
});
});
};
// After data chagnes, we turn the flag back off
// so that if data actually changes when we're not
// editing it, the page is reset
React.useEffect(() => {
setSkipPageReset(false);
}, [data]);
return (
<>
{/* {result && massaged1 && (
<TableRendered result={result} massaged1={massaged1} />
)} */}
{result && massaged1 && (
<Styles>
{/* <button onClick={resetData}>Reset Data</button> */}
<Table1
columns={massaged1[0]}
data={data}
updateMyData={updateMyData}
skipPageReset={skipPageReset}
/>
</Styles>
)}
</>
);
}
function Table1({ columns, data, updateMyData, skipPageReset }) {
// For this example, we're using pagination to illustrate how to stop
// the current page from resetting when our data changes
// Otherwise, nothing is different here.
const {
getTableProps,
getTableBodyProps,
headerGroups,
prepareRow,
page,
canPreviousPage,
canNextPage,
pageOptions,
pageCount,
gotoPage,
nextPage,
previousPage,
setPageSize,
state: { pageIndex, pageSize },
} = useTable(
{
columns,
data,
defaultColumn,
// use the skipPageReset option to disable page resetting temporarily
autoResetPage: !skipPageReset,
// updateMyData isn't part of the API, but
// anything we put into these options will
// automatically be available on the instance.
// That way we can call this function from our
// cell renderer!
updateMyData,
},
usePagination
);
// Render the UI for your table
return (
<>
<table {...getTableProps()}>
<thead>
{headerGroups.map((headerGroup) => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map((column) => (
<th {...column.getHeaderProps()}>{column.render("Header")}</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{page.map((row, i) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map((cell) => {
return (
<td {...cell.getCellProps()}>{cell.render("Cell")}</td>
);
})}
</tr>
);
})}
</tbody>
</table>
<div className="pagination">
<button onClick={() => gotoPage(0)} disabled={!canPreviousPage}>
{"<<"}
</button>{" "}
<button onClick={() => previousPage()} disabled={!canPreviousPage}>
{"<"}
</button>{" "}
<button onClick={() => nextPage()} disabled={!canNextPage}>
{">"}
</button>{" "}
<button onClick={() => gotoPage(pageCount - 1)} disabled={!canNextPage}>
{">>"}
</button>{" "}
<span>
Page{" "}
<strong>
{pageIndex + 1} of {pageOptions.length}
</strong>{" "}
</span>
<span>
| Go to page:{" "}
<input
type="number"
defaultValue={pageIndex + 1}
onChange={(e) => {
const page = e.target.value ? Number(e.target.value) - 1 : 0;
gotoPage(page);
}}
style={{ width: "100px" }}
/>
</span>{" "}
<select
value={pageSize}
onChange={(e) => {
setPageSize(Number(e.target.value));
}}
>
{[10, 20, 30, 40, 50].map((pageSize) => (
<option key={pageSize} value={pageSize}>
Show {pageSize}
</option>
))}
</select>
</div>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment