aboutsummaryrefslogtreecommitdiff
path: root/src/components/PeerTeachers.svelte
blob: 608163865050ed98b2b4aace8ee2c031859a5254 (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
135
<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;

  // TODO introducing layer of indirection to how Peer Teachers are displayed in order to sort based on aribitrary paramters
  function ptSort(a: PeerTeacher, b: PeerTeacher): number {
    return a.lastname.toUpperCase() === b.lastname.toUpperCase()
      ? a.firstname.toUpperCase().localeCompare(b.firstname.toUpperCase())
      : a.lastname.toUpperCase().localeCompare(b.lastname.toUpperCase());
  }
  $: peer_teachers = [...$ptStore.values()].sort(ptSort);

  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 copyEmailsToClipboard() {
    const emails = peer_teachers.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="overflow-auto h-full">
  <table class="table w-full table-compact">
    <thead class="sticky top-0">
      <tr>
        {#each headers as header, i}
          <th>
            {#if i == 0}
              <!-- display num PTs if first row -->
              {peer_teachers.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={copyEmailsToClipboard}
                />
              </div>
            {:else}
              {header}
            {/if}
          </th>
        {/each}
      </tr>
    </thead>

    <tbody>
      {#each peer_teachers 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>