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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225import React, { useState, FormEvent } from "react";
import { Link, useNavigate } from "react-router-dom";
import { useUser } from "../queries";
import * as API from "aws-amplify/api";
import {
AuditEmailSubscriptionInput,
EmailSubscriptionList,
} from "#src/components/AuditEmailSubscriptionInput.tsx";
import { v4 as uuidv4 } from "uuid";
import { useGlobalStore } from "../utils";
import { AuditPagesInput } from "#src/components/AuditPagesInput.tsx";
import { createLog } from "#src/utils/createLog.ts";
import { StyledLabeledInput } from "#src/components/StyledLabeledInput.tsx";
import { Card } from "#src/components/Card.tsx";
import { CgOptions } from "react-icons/cg";
import { TbList, TbMail } from "react-icons/tb";
import { StyledButton } from "#src/components/StyledButton.tsx";
import { LuClipboardCheck, LuClipboardPaste } from "react-icons/lu";
import styles from "./BuildAudit.module.scss";
import * as Switch from "@radix-ui/react-switch";
interface Page {
url: string;
type: "html" | "pdf";
}
export const BuildAudit = () => {
const navigate = useNavigate();
const { setAnnounceMessage } = useGlobalStore();
const { data: user } = useUser();
const [emailNotifications, setEmailNotifications] = useState(false);
const [pages, setPages] = useState<Page[]>([]);
const [auditNameValid, setAuditNameValid] = useState(false);
const [isSaving, setIsSaving] = useState(false);
const [isSavingAndRunning, setIsSavingAndRunning] = useState(false);
const defaultEmailList = {
emails: [
/* {
id: uuidv4(),
email: user?.email ?? "user@uic.edu",
frequency: "Weekly",
lastSent: "", // we'll populate this on send in buildAuditData
}, */
],
};
const [emailList, setEmailList] =
useState<EmailSubscriptionList>(defaultEmailList);
const buildAuditData = (formData: FormData) => {
let notifications: EmailSubscriptionList = { emails: [] };
if (emailNotifications) {
// if notification are enabled, add the current date to newly-added emails
const theDate = new Date().toISOString();
notifications.emails = emailList.emails.map((item) => {
return {
...item,
lastSent: theDate,
};
});
}
return {
auditName: formData.get("auditName") as string,
scanFrequency: formData.get("scanFrequency") as string,
emailNotifications: JSON.stringify(notifications),
pages: pages.map((page) => ({
url: page.url,
type: page.type,
})),
};
};
const saveAndRunAudit = async (e: FormEvent) => {
e.preventDefault();
if (pages.length === 0) {
window.alert(`You need to add at least 1 URL to your audit.`);
return;
}
const form = (e.currentTarget as HTMLElement).closest("form");
if (!form) return;
const formData = new FormData(form);
const auditData = buildAuditData(formData);
console.log("Audit Data (Save & Run):", JSON.stringify(auditData));
setIsSavingAndRunning(true);
try {
const response = (await (
await API.post({
apiName: "auth",
path: "/saveAudit",
options: { body: { ...auditData, saveAndRun: true } },
}).response
).body.json()) as { id: string };
setAnnounceMessage(`Audit saved and audit run started!`, "success");
await createLog(`Audit created and audit run started!`, response.id);
navigate(`/audits/${response?.id}`);
} finally {
setIsSavingAndRunning(false);
}
};
const saveAudit = async (e: FormEvent) => {
e.preventDefault();
if (pages.length === 0) {
window.alert(`You need to add at least 1 URL to your audit.`);
return;
}
const form = (e.currentTarget as HTMLElement).closest("form");
if (!form) return;
const formData = new FormData(form);
const auditData = buildAuditData(formData);
console.log("Audit Data (Save):", JSON.stringify(auditData));
setIsSaving(true);
try {
const response = (await (
await API.post({
apiName: "auth",
path: "/saveAudit",
options: { body: { ...auditData, saveAndRun: false } },
}).response
).body.json()) as { id: string };
setAnnounceMessage(`Audit saved!`, "success");
await createLog(`Audit created!`, response.id);
navigate(`/audits/${response?.id}`);
} finally {
setIsSaving(false);
}
};
const validateAuditName = (e: React.ChangeEvent<HTMLInputElement>) => {
const auditName = e.target.value.trim();
if (auditName) {
setAuditNameValid(true);
} else {
setAuditNameValid(false);
}
};
return (
<div className={styles.buildAudit}>
{/* <Link to="..">← Go Back</Link>
*/}
<h1 className="initial-focus-element">Audit Builder</h1>
<form onSubmit={saveAndRunAudit}>
<div className="cards-38-62">
<Card variant="light">
<h2>
<CgOptions className={"icon-small"} />
General Info
</h2>
<StyledLabeledInput>
<label htmlFor="auditName">
Audit Name <span>(required)</span>:
</label>
<input
id="auditName"
name="auditName"
onBlur={validateAuditName}
required
className={styles["input-element"]}
/>
</StyledLabeledInput>
<StyledLabeledInput>
<label htmlFor="scanFrequency">Scan Frequency:</label>
<select
id="scanFrequency"
name="scanFrequency"
className={styles["input-element"]}
>
<option>Manually</option>
<option>Daily</option>
<option>Weekly</option>
<option>Monthly</option>
{/*
<option>On Monitor Update</option> */}
</select>
</StyledLabeledInput>
</Card>
<Card variant="light">
<h2>
<TbMail className="icon-small" />
Email Notifications:
</h2>
<AuditEmailSubscriptionInput
initialValue={emailList}
onValueChange={setEmailList}
/>
</Card>
</div>
<Card variant="light">
<h2>
<TbList className={"icon-small"} />
Add URLs
</h2>
<AuditPagesInput
initialPages={pages}
setParentPages={setPages}
returnMutation={false}
/>
</Card>
<div className={styles["action-buttons"]}>
<StyledButton
icon={<LuClipboardCheck />}
onClick={saveAudit}
label="Save Audit"
disabled={pages.length < 1 || !auditNameValid || isSaving || isSavingAndRunning}
loading={isSaving}
/>
<StyledButton
icon={<LuClipboardPaste />}
onClick={saveAndRunAudit}
label="Save & Run Audit"
variant="green"
disabled={pages.length < 1 || !auditNameValid || isSaving || isSavingAndRunning}
loading={isSavingAndRunning}
/>
</div>
</form>
</div>
);
};