aboutsummaryrefslogtreecommitdiff
path: root/src/components/PeerTeachers.svelte
blob: 986d696a948b32a20bf3a5319aab9ac43cb9d4b8 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<script lang="ts">
  import { labStore, ptStore } from "../stores";
  import type PeerTeacher from "../models/PeerTeacher";
  import Icon from "./helpers/Icon.svelte";
  import type { SnackbarComponentDev } from "@smui/snackbar";
  import Snackbar, { Label, Actions } from "@smui/snackbar";
  import IconButton from "@smui/icon-button";

  let selected_pt: PeerTeacher | undefined;
  let editing: boolean = false;

  let snackbarSuccess: SnackbarComponentDev;

  $: peerTeachers = [...$ptStore.values()].sort((a, b) =>
    a.lastname.toUpperCase() === b.lastname.toUpperCase()
      ? a.firstname.toUpperCase().localeCompare(b.firstname.toUpperCase())
      : a.lastname.toUpperCase().localeCompare(b.lastname.toUpperCase())
  );

  function deletePT(id: number) {
    // Unassign all labs assigned to this Peer Teacher
    $ptStore.get(id)?.labs.forEach((lab_id) => {
      const lab = $labStore?.get(lab_id);
      lab.assigned = false;
    });

    // Yeet the Peer Teacher
    ptStore.update((map) => {
      map.delete(id);
      return map;
    });
  }

  function handleEmail() {
    const emails = peerTeachers.flatMap((pt) => pt.email).join(",");
    navigator.clipboard.writeText(emails);
    if (snackbarSuccess) snackbarSuccess.open();
  }

  let headers = [
    "",
    "First",
    "Last",
    "Email",
    "UIN",
    "Phone",
    "Pref",
    "Lab",
    "Gen",
    "Ethnicity",
    "Grad",
    "Status",
    "",
  ];
</script>

<div class="h-full overflow-auto">
  <table class="table table-compact w-full h-full">
    <thead class="bg-white border-b sticky top-0">
      <tr>
        {#each headers as header, i}
          <th>
            {#if i == 0}
              <!-- display num PTs if first row -->
              {peerTeachers.length}
            {:else if header == "Email"}
              <div class="flex flex-row items-center">
                {header}
                <Icon
                  name="clipboard-copy"
                  class="h-4 w-4 ml-2 mt-0.5"
                  handleClick={handleEmail}
                />
                <!-- </Button> -->
              </div>
            {:else}
              {header}
            {/if}
          </th>
        {/each}
      </tr>
    </thead>

    <tbody>
      {#each peerTeachers as pt, i}
        <tr
          on:click={() => {
            selected_pt = pt;
          }}
          class={selected_pt == pt ? "active" : "hover"}
        >
          <td>{i + 1}</td>
          <td>
            {#if editing && selected_pt == pt}
              <input
                bind:value={pt.firstname}
                type="text"
                class="input input-bordered input-primary  w-full max-w-xs"
                on:blur={() => (editing = false)}
              />
            {:else}
              <div on:dblclick={() => (editing = true)}>
                {pt.firstname}
              </div>
            {/if}
          </td>
          <td>{pt.lastname}</td>
          <td>{pt.email}</td>
          <td>{pt.id}</td>
          <td>{pt.phone}</td>
          <td>{pt.pref_work}</td>
          <td>{pt.lab_hours}</td>
          <td>{pt.gender}</td>
          <td>{pt.ethnicity}</td>
          <td>{pt.graduation}</td>
          <td>{pt.new_ret}</td>
          <th
            ><button
              on:click={() => deletePT(pt.id)}
              class="btn btn-ghost btn-xs">Delete</button
            ></th
          >
        </tr>
      {/each}
    </tbody>
  </table>
</div>

<Snackbar bind:this={snackbarSuccess} class="demo-success">
  <Label>Successfully copied comma-separated list of emails</Label>
  <Actions>
    <IconButton class="material-icons" title="Dismiss">close</IconButton>
  </Actions>
</Snackbar>