📦 EqualifyEverything / equalify

📄 AuditEmailSubscriptionInput.tsx · 163 lines
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163import { useCallback, useEffect, useState } from "react";
import { v4 as uuidv4 } from "uuid";
import { StyledButton } from "./StyledButton";
import { TbMailX, TbTrashX } from "react-icons/tb";
import auditEmailStyles from "./AuditEmailSubscriptionInput.module.scss";
import { StyledLabeledInput } from "./StyledLabeledInput";

export interface EmailSubscriptionList {
  emails: EmailSubscriptionEmail[];
}

interface EmailSubscriptionEmail {
  id: string;
  email: string;
  frequency: string; // daily|weekly|monthly
  lastSent: string; // UTC date string
}

interface ChildProps {
  initialValue: EmailSubscriptionList;
  onValueChange: (newValue: EmailSubscriptionList) => void; // Callback function prop
}

const frequencyOpts = ["Daily", "Weekly", "Monthly"];

// main component
export const AuditEmailSubscriptionInput: React.FC<ChildProps> = ({
  initialValue,
  onValueChange,
}) => {
  const [emails, setEmails] = useState(initialValue.emails);

  const handleUpdateField = useCallback(
    (idToUpdate: string, field: string, value: string) => {
      setEmails((prevEmails) =>
        prevEmails.map((entry) =>
          entry.id === idToUpdate ? { ...entry, [field]: value } : entry
        )
      );
    },
    []
  );

  const handleAddEmail = (e:any) => {
    e.preventDefault();
    setEmails((prevEmails) => [
      ...prevEmails,
      {
        id: uuidv4(),
        email: "user@uic.edu",
        frequency: "Weekly",
        lastSent: "",
      },
    ]);
  };

  const handleRemoveEmail = useCallback(
    (idToRemove: string) => {
      //if (emails.length > 1) {
      setEmails((prevEmails) =>
        prevEmails.filter((email) => email.id !== idToRemove)
      );
      //}
    },
    [emails.length]
  );

  // whenever we change values, update the parent data
  useEffect(() => {
    //console.log("Updating...", emails);
    onValueChange({ emails: emails });
  }, [emails]);

  return (
    <div className={auditEmailStyles.AuditEmailSubscriptionInput}>
      <div className={auditEmailStyles["rows"]}>
        {emails.map((entry) => (
          <EmailInputRow
            key={entry.id}
            entry={entry}
            onChange={handleUpdateField}
            onRemove={handleRemoveEmail}
          />
        ))}
      </div>
      <div className={auditEmailStyles["action-buttons"]}>
      <StyledButton
        onClick={handleAddEmail}
        label={
          emails.length > 0 ? "Add Another Email" : "Add Email Notification"
        }
      />
      </div>
    </div>
  );
};

// component for individual row
interface EmailInputRowProps {
  entry: EmailSubscriptionEmail;
  onChange: (id: string, field: string, value: string) => void;
  onRemove: (id: string) => void;
}

const EmailInputRow: React.FC<EmailInputRowProps> = ({
  entry,
  onChange,
  onRemove,
}) => {
  const { id, email, frequency } = entry;

  const handleChange = useCallback(
    (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
      const { name, value } = e.target;
      onChange(id, name, value);
    },
    [id, onChange]
  );

  const handleRemoveClick = useCallback(() => {
    onRemove(id);
  }, [id, onRemove]);

  return (
    <div className={auditEmailStyles["row"]}>
      <StyledLabeledInput>
        <label htmlFor={`email-${id}`}>Email</label>
        <input
          id={`email-${id}`}
          name="email"
          type="email"
          placeholder="user@example.com"
          value={email}
          onChange={handleChange}
        />
      </StyledLabeledInput>

      <StyledLabeledInput>
        <label htmlFor={`frequency-${id}`}>Frequency</label>
        <select
          id={`frequency-${id}`}
          name="frequency"
          value={frequency}
          onChange={handleChange}
        >
          {frequencyOpts.map((option) => (
            <option key={option} value={option}>
              {option}
            </option>
          ))}
        </select>
      </StyledLabeledInput>

      <StyledButton
        onClick={handleRemoveClick}
        label={`Remove email ${email}`}
        icon={<TbTrashX />}
        showLabel={false}
      />
    </div>
  );
};