Skip to content

Commit 1c3b184

Browse files
committed
added regex for URL
1 parent 4da0b6e commit 1c3b184

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

client/src/views/ContentCreator/DayEditor/DayEditor.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const DayEditor = ({
2929
const [scienceObj, setScienceObj] = useState('');
3030
const [makingObj, setMakingObj] = useState('');
3131
const [computationObj, setComputationObj] = useState('');
32+
const [linkError, setLinkError] = useState(false);
3233

3334
const showDayDetailsModal = async (dayObj) => {
3435
const response = await getDay(dayObj.id);
@@ -41,6 +42,7 @@ const DayEditor = ({
4142
setDescription(response.data.description);
4243
setTekS(response.data.TekS);
4344
setLink(response.data.link);
45+
setLinkError(false);
4446
const learningComponents = response.data.objectives;
4547
setScienceObj(
4648
learningComponents[0] ? learningComponents[0].description : ''
@@ -97,6 +99,15 @@ const DayEditor = ({
9799
};
98100

99101
const handleWorkspace = async (e) => {
102+
if (link) {
103+
const goodLink = checkURL(link);
104+
if (!goodLink) {
105+
setLinkError(true);
106+
message.error('Please Enter a valid URL starting with HTTP/HTTPS', 4);
107+
return;
108+
}
109+
}
110+
setLinkError(false);
100111
const res = await updateDayDetails(
101112
selectDay.id,
102113
description,
@@ -133,6 +144,15 @@ const DayEditor = ({
133144
};
134145

135146
const handleSave = async (e) => {
147+
if (link) {
148+
const goodLink = checkURL(link);
149+
if (!goodLink) {
150+
setLinkError(true);
151+
message.error('Please Enter a valid URL starting with HTTP/HTTPS', 4);
152+
return;
153+
}
154+
}
155+
setLinkError(false);
136156
const res = await updateDayDetails(
137157
selectDay.id,
138158
description,
@@ -153,6 +173,15 @@ const DayEditor = ({
153173
}
154174
};
155175

176+
const checkURL = (n) => {
177+
const regex =
178+
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/g;
179+
if (n.search(regex) === -1) {
180+
return null;
181+
}
182+
return n;
183+
};
184+
156185
return (
157186
<div>
158187
<Modal
@@ -294,8 +323,12 @@ const DayEditor = ({
294323
label='Link to Additional Resources (Optional)'
295324
>
296325
<Input
297-
onChange={(e) => setLink(e.target.value)}
326+
onChange={(e) => {
327+
setLink(e.target.value);
328+
setLinkError(false);
329+
}}
298330
value={link}
331+
style={linkError ? { backgroundColor: '#FFCCCC' } : {}}
299332
placeholder='Enter a link'
300333
></Input>
301334
</Form.Item>

client/src/views/ContentCreator/LearningStandardCreator/LearningStandardCreator.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function LearningStandardCreator({
2323
const [numofDays, setNumofDays] = useState('');
2424
const [teks, setTeks] = useState('');
2525
const [link, setLink] = useState('');
26+
const [linkError, setLinkError] = useState(false);
2627
const [learningStandardObj, setLearningStandardObj] = useState('');
2728
let found;
2829

@@ -41,6 +42,7 @@ export default function LearningStandardCreator({
4142
setName('');
4243
setTeks('');
4344
setLink('');
45+
setLinkError(false);
4446
setNumofDays('');
4547
setVisible(true);
4648
};
@@ -49,12 +51,16 @@ export default function LearningStandardCreator({
4951
setVisible(false);
5052
};
5153

52-
const handleSubmit = async (e) => {
53-
if (unit === '') {
54-
message.error('Please select unit');
55-
return;
54+
const handleSubmit = async () => {
55+
if (link) {
56+
const goodLink = checkURL(link);
57+
if (!goodLink) {
58+
setLinkError(true);
59+
message.error('Please Enter a valid URL starting with HTTP/HTTPS', 4);
60+
return;
61+
}
5662
}
57-
console.log(e);
63+
setLinkError(false);
5864
const res = await createLearningStandard(
5965
description,
6066
name,
@@ -88,6 +94,16 @@ export default function LearningStandardCreator({
8894
}
8995
};
9096

97+
const checkURL = (n) => {
98+
console.log(n);
99+
const regex =
100+
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/g;
101+
if (n.search(regex) === -1) {
102+
return null;
103+
}
104+
return n;
105+
};
106+
91107
return (
92108
<div>
93109
<button onClick={showModal} id='add-learning-standard-btn'>
@@ -177,7 +193,9 @@ export default function LearningStandardCreator({
177193
<Input
178194
onChange={(e) => {
179195
setLink(e.target.value);
196+
setLinkError(false);
180197
}}
198+
style={linkError ? { backgroundColor: '#FFCCCC' } : {}}
181199
value={link}
182200
placeholder='Enter a link'
183201
/>

client/src/views/ContentCreator/LessonEditor/LessonEditor.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default function LessonEditor({
1919
const [description, setDescription] = useState('');
2020
const [teks, setTeks] = useState('');
2121
const [link, setLink] = useState('');
22+
const [linkError, setLinkError] = useState(false);
2223

2324
const [displayName, setDisplayName] = useState(learningStandard.name);
2425

@@ -29,6 +30,7 @@ export default function LessonEditor({
2930
setDescription(res.data.expectations);
3031
setTeks(res.data.teks);
3132
setLink(res.data.link);
33+
setLinkError(false);
3234
};
3335

3436
useEffect(() => {
@@ -39,8 +41,15 @@ export default function LessonEditor({
3941
setVisible(false);
4042
};
4143

42-
const handleSubmit = async (e) => {
43-
console.log(e);
44+
const handleSubmit = async () => {
45+
if (link) {
46+
const goodLink = checkURL(link);
47+
if (!goodLink) {
48+
setLinkError(true);
49+
message.error('Please Enter a valid URL starting with HTTP/HTTPS', 4);
50+
return;
51+
}
52+
}
4453
const response = await updateLearningStandard(
4554
learningStandard.id,
4655
name,
@@ -59,6 +68,15 @@ export default function LessonEditor({
5968
}
6069
};
6170

71+
const checkURL = (n) => {
72+
const regex =
73+
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/g;
74+
if (n.search(regex) === -1) {
75+
return null;
76+
}
77+
return n;
78+
};
79+
6280
return (
6381
<div>
6482
<button id='link-btn' onClick={showModal}>
@@ -112,7 +130,9 @@ export default function LessonEditor({
112130
<Input
113131
onChange={(e) => {
114132
setLink(e.target.value);
133+
setLinkError(false);
115134
}}
135+
style={linkError ? { backgroundColor: '#FFCCCC' } : {}}
116136
value={link}
117137
placeholder='Enter a link'
118138
/>

0 commit comments

Comments
 (0)