aboutsummaryrefslogtreecommitdiffstats
path: root/src/pages/SurveyResults/index.js
blob: dec182e896bc940c339796081cfa1169295d8c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { Button } from "@mui/material";
import { DataGrid } from "@mui/x-data-grid";
import { Link, useParams } from "react-router-dom";
import useLocalStorage from "../../hooks/useLocalStorage";
import useForm from "../../hooks/useForm";

export default function SurveyResults() {
  const { surveyId } = useParams();
  const [runs, setRuns] = useLocalStorage(`runs-${surveyId}`, []);
  const [form] = useForm(surveyId);

  const columns = form?.fields?.map(({ id, name }) => ({
    field: id,
    headerName: name,
  }));

  return (
    <>
      <h3>SurveyRuns</h3>
      {surveyId}
      <Button onClick={() => setRuns([{ id: crypto.randomUUID() }, ...runs])}>
        New Run
      </Button>

      {runs &&
        runs.map((r) => {
          const runRs = runResults(surveyId, r.id);

          console.log(runRs);

          return (
            <div key={r.id}>
              <Link to={`${r.id}/answer`}>Add Answer</Link> {JSON.stringify(r)}
              Total Results: {Object.keys(runRs).length}
              <DataGrid
                rows={[]}
                columns={columns}
                initialState={{
                  pagination: {
                    paginationModel: {
                      pageSize: 25,
                    },
                  },
                }}
                pageSizeOptions={[25, 50, 100]}
                checkboxSelection
                disableRowSelectionOnClick
              />
            </div>
          );
        })}
    </>
  );
}

function runResults(surveyId, runId) {
  const str = localStorage[`results-${surveyId}-${runId}`];
  return !str ? {} : JSON.parse(str);
}