-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from COS301-SE-2024/QC/web/ip-address-revamp
Qc/web/ip address revamp
- Loading branch information
Showing
8 changed files
with
252 additions
and
199 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
frontend/occupi-web/src/components/modal/AddIPModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React from "react"; | ||
import { | ||
Input, | ||
Button, | ||
ModalBody, | ||
ModalFooter, | ||
ModalHeader, | ||
} from "@nextui-org/react"; | ||
import DataService from "DataService"; | ||
|
||
const AddIPModal = ({ | ||
onClose, | ||
view | ||
}: { | ||
onClose: () => void, | ||
view: "whitelisted" | "blacklisted" | ||
}) => { | ||
const [form, setForm] = React.useState<{ | ||
email: string; | ||
ip: string; | ||
}>({ email: "", ip: "" }); | ||
|
||
const [isLoading, setIsLoading] = React.useState<boolean>(false); | ||
const [err, setErr] = React.useState<string | null>(null); | ||
|
||
const validateEmail = (email: string) => { | ||
return email === "" || email.match( | ||
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z-0-9]+\.)+[a-zA-Z]{2,}))$/ | ||
); | ||
}; | ||
|
||
const validateIP = (ip: string) => { | ||
return ip === "" || ip.match( | ||
/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ | ||
); | ||
}; | ||
|
||
return ( | ||
<> | ||
<ModalHeader className="flex flex-col gap-1"> | ||
{view === "whitelisted" ? "Add new IP address" : "Add new Blacklisted IP address"} | ||
</ModalHeader> | ||
<ModalBody> | ||
<Input | ||
value={form.email} | ||
autoFocus | ||
label="Email" | ||
placeholder="Enter the users email" | ||
variant="bordered" | ||
errorMessage="Please enter a valid email" | ||
isInvalid={!validateEmail(form.email)} | ||
onChange={(e) => setForm({ ...form, email: e.target.value })} | ||
/> | ||
<Input | ||
value={form.ip} | ||
label="IP address" | ||
placeholder="Enter the ip address" | ||
variant="bordered" | ||
errorMessage="Please enter a valid ip address" | ||
isInvalid={!validateIP(form.ip)} | ||
onChange={(e) => setForm({ ...form, ip: e.target.value })} | ||
/> | ||
{err && <p className="text-red-500">{err}</p>} | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button color="primary" variant="light" onPress={onClose}> | ||
Close | ||
</Button> | ||
<Button | ||
color="default" | ||
isLoading={isLoading} | ||
onPress={() => { | ||
setIsLoading(true); | ||
if (view === "whitelisted"){ | ||
DataService.addIP(form.ip, form.email).then(() => { | ||
setIsLoading(false); | ||
onClose(); | ||
}).catch((err) => { | ||
setErr(err.message); | ||
setIsLoading(false); | ||
}); | ||
} else{ | ||
DataService.removeIP(form.ip, form.email).then(() => { | ||
setIsLoading(false); | ||
onClose(); | ||
}).catch((err) => { | ||
setErr(err.message); | ||
setIsLoading(false); | ||
}); | ||
} | ||
}} | ||
> | ||
{view === "whitelisted" ? "Add IP" : "Block IP"} | ||
</Button> | ||
</ModalFooter> | ||
</> | ||
); | ||
}; | ||
|
||
export default AddIPModal; |
73 changes: 73 additions & 0 deletions
73
frontend/occupi-web/src/components/modal/DeleteIPModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from "react"; | ||
import { | ||
Button, | ||
User, | ||
ModalBody, | ||
ModalFooter, | ||
ModalHeader, | ||
} from "@nextui-org/react"; | ||
import DataService from "DataService"; | ||
|
||
type User = { | ||
email: string; | ||
name: string; | ||
city: string; | ||
region: string; | ||
country: string; | ||
location: string; | ||
ipAddress: string; | ||
blackListedIP: string; | ||
}; | ||
|
||
const DeleteIPModal = ({ | ||
selectedUser, | ||
onClose, | ||
}: { | ||
selectedUser: User | null; | ||
onClose: () => void; | ||
}) => { | ||
const [isLoading, setIsLoading] = React.useState<boolean>(false); | ||
const [err, setErr] = React.useState<string | null>(null); | ||
return ( | ||
<> | ||
<ModalHeader className="flex flex-col gap-1"> | ||
Remove IP address {selectedUser?.ipAddress} | ||
</ModalHeader> | ||
<ModalBody> | ||
<h3>Are you sure you want to remove this IP address?</h3> | ||
<h4>This will prevent {selectedUser?.email} from logging in from: </h4> | ||
<ul>{selectedUser?.city}</ul> | ||
<ul>{selectedUser?.region}</ul> | ||
<ul>{selectedUser?.country}</ul> | ||
<h4>They will recieve an email notifying them of this change</h4> | ||
{err && <p className="text-red-500">{err}</p>} | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button color="primary" variant="light" onPress={onClose}> | ||
Close | ||
</Button> | ||
<Button | ||
color="danger" | ||
isLoading={isLoading} | ||
onPress={() => { | ||
setIsLoading(true); | ||
DataService.removeIP( | ||
selectedUser?.email ?? "", | ||
selectedUser?.ipAddress ?? "" | ||
).then(() => { | ||
setIsLoading(false); | ||
onClose(); | ||
}).catch((err) => { | ||
setErr(err.message); | ||
setIsLoading(false); | ||
}); | ||
}} | ||
> | ||
Remove IP | ||
</Button> | ||
</ModalFooter> | ||
</> | ||
); | ||
} | ||
|
||
export default DeleteIPModal |
68 changes: 68 additions & 0 deletions
68
frontend/occupi-web/src/components/modal/UnblockIPModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from "react"; | ||
import { | ||
Button, | ||
User, | ||
ModalBody, | ||
ModalFooter, | ||
ModalHeader, | ||
} from "@nextui-org/react"; | ||
import DataService from "DataService"; | ||
|
||
type User = { | ||
email: string; | ||
name: string; | ||
city: string; | ||
region: string; | ||
country: string; | ||
location: string; | ||
ipAddress: string; | ||
blackListedIP: string; | ||
}; | ||
|
||
const UnblockIPModal = ({ | ||
selectedUser, | ||
onClose, | ||
}: { | ||
selectedUser: User | null; | ||
onClose: () => void; | ||
}) => { | ||
const [isLoading, setIsLoading] = React.useState<boolean>(false); | ||
const [err, setErr] = React.useState<string | null>(null); | ||
|
||
return ( | ||
<> | ||
<ModalHeader className="flex flex-col gap-1"> | ||
Add new IP address | ||
</ModalHeader> | ||
<ModalBody> | ||
<h3>Are you sure you want to allow this IP address?</h3> | ||
<h4>This will allow {selectedUser?.email} to use <ul>{selectedUser?.blackListedIP}</ul> to login</h4> | ||
<h4>They will recieve an email notifying them of this change</h4> | ||
{err && <p className="text-red-500">{err}</p>} | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button color="primary" variant="light" onPress={onClose}> | ||
Close | ||
</Button> | ||
<Button | ||
color="default" | ||
isLoading={isLoading} | ||
onPress={() => { | ||
setIsLoading(true); | ||
DataService.addIP(selectedUser?.blackListedIP ?? "", selectedUser?.email ?? "").then(() => { | ||
setIsLoading(false); | ||
onClose(); | ||
}).catch((err) => { | ||
setErr(err.message); | ||
setIsLoading(false); | ||
}); | ||
}} | ||
> | ||
Allow IP | ||
</Button> | ||
</ModalFooter> | ||
</> | ||
); | ||
}; | ||
|
||
export default UnblockIPModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.