Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/main_endpoints/models/Advertisement.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
},
expireDate: {
type: Date,
},
expireAt: {
type: Date,
default: undefined,
index: {expireAfterSeconds: 0}, // TTL only kicks in when expireAt is set

Check failure on line 17 in api/main_endpoints/models/Advertisement.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Trailing spaces not allowed
}
},
{ collection: 'Advertisements' }
Expand Down
14 changes: 12 additions & 2 deletions api/main_endpoints/routes/Advertisement.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
});
});


router.get('/getAllAdvertisements', async (req, res) => {
if (!checkIfTokenSent(req)) {
return res.sendStatus(FORBIDDEN);
Expand All @@ -39,9 +40,16 @@
} else if (!await decodeToken(req)) {
return res.sendStatus(UNAUTHORIZED);
}
const now = new Date();
// const expireInOneMinute = new Date(now.getTime() + 60 * 1000);

const newAd = new Advertisement({
// message: req.body.message,
// expireDate: expireInOneMinute, // testing and it doesn't work
// expireAt: expireInOneMinute

Check failure on line 49 in api/main_endpoints/routes/Advertisement.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Trailing spaces not allowed
message: req.body.message,
expireDate: req.body.expireDate
expireDate: req.body.expireDate,
expireAt: req.body.expireDate
});

Advertisement.create(newAd)
Expand All @@ -59,9 +67,11 @@
} else if (!await decodeToken(req)) {
return res.sendStatus(UNAUTHORIZED);
}
console.log("expire date in delete " + expireDate);

Check failure on line 70 in api/main_endpoints/routes/Advertisement.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Strings must use singlequote

Check failure on line 70 in api/main_endpoints/routes/Advertisement.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Unexpected console statement

Advertisement.deleteOne({ _id: req.body._id })
.then(result => {
if (result.n < 1) {
if (result.deletedCount < 1) { // used to be result.n
res.sendStatus(NOT_FOUND);
} else {
res.sendStatus(OK);
Expand Down
1 change: 1 addition & 0 deletions src/APIFunctions/Advertisement.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
status.responseData = err;
status.error = true;
}
console.log("STATUS " + JSON.stringify(status.responseData));

Check failure on line 69 in src/APIFunctions/Advertisement.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Strings must use singlequote

Check failure on line 69 in src/APIFunctions/Advertisement.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Unexpected console statement
return status;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function membershipStateToString(accessLevel) {
return membershipStatusArray[accessLevel + 2];
}

const BASE_API_URL = process.env.REACT_APP_BASE_API_URL || 'http://localhost:8080/';
const BASE_API_URL = process.env.REACT_APP_BASE_API_URL || 'http://localhost:8080';

module.exports = {
memberApplicationState,
Expand Down
88 changes: 29 additions & 59 deletions src/Pages/Advertisement/AdvertisementAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import { useState, useEffect } from 'react';
import { useUser } from '../../Components/context/UserContext';

// createAd returns a value - what is it?

Check failure on line 6 in src/Pages/Advertisement/AdvertisementAdmin.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Expected indentation of 0 spaces but found 3
// how do we know if createAd() works or not?

Check failure on line 7 in src/Pages/Advertisement/AdvertisementAdmin.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Expected indentation of 0 spaces but found 4
// is there a field we can check?^

Check failure on line 8 in src/Pages/Advertisement/AdvertisementAdmin.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Expected indentation of 0 spaces but found 4

Check failure on line 9 in src/Pages/Advertisement/AdvertisementAdmin.js

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Trailing spaces not allowed
// if createAd worked, how can we update the array without calling the backend again?

export default function AdvertisementAdmin() {
const { user } = useUser();

const [errorMesssage, setErrorMessage] = useState('');
const [ads, setAds] = useState([]);
const [message, setMessage] = useState('');
const [year, setYear] = useState();
const [month, setMonth] = useState();
const [day, setDay] = useState();
const [expireDate, setExpireDate] = useState();

async function getAdsFromDB() {
const adsFromDB = await getAds(user.token);
Expand All @@ -20,60 +24,43 @@
}

async function createAdHandler() {
// make sure empty inputs are properly set as undefined if empty
if (year === '') {
setYear(undefined);
}
if (month === '') {
setMonth(undefined);
if(expireDate === null){
setExpireDate(undefined);
}
if (day === '') {
setDay(undefined);
// expireDate is a string so we need to turn into date object

if(isExpired(expireDate)){
setErrorMessage("Date is in the past or the present, unable to create ad!!!");
return;
}else{
setErrorMessage("");
}

let expireDate = new Date(year, month - 1, day);
if (isNaN(expireDate.getTime())) {
expireDate = undefined;
}

await createAd({
message,
expireDate,
}, user.token);

await getAdsFromDB();

}

async function deleteExpiredAds() {
const adsFromDB = await getAds(user.token);
if (!adsFromDB.error) {
const currentDate = new Date();
const expiredAds = adsFromDB.responseData.filter(ad => {
if (ad.expireDate === undefined) return false;
return new Date(ad.expireDate) < currentDate;
});

for (const ad of expiredAds) {
await deleteAd(ad, user.token);
}
}
function isExpired(expireDate){
const currDate = new Date();
console.log("current date" + currDate);
const expireDateObject = new Date(expireDate);
return expireDateObject < currDate;
}

useEffect(() => {
getAdsFromDB();

const intervalId = setInterval(async () => {
await deleteExpiredAds();
await getAdsFromDB();
}, 20);

}, []);

return (
<div className='m-10'>
<h1 className="text-4xl font-extrabold leading-none tracking-tight text-gray-900 md:text-5xl lg:text-6xl dark:text-white">
Welcome to the Advertisement Admin Page!!
</h1>
<h2 style={{fontWeight: 'bold', color: 'red'}} >{errorMesssage}</h2>
<div className="relative overflow-x-auto">
<div className='py-6'>
<label className="w-full form-control">
Expand Down Expand Up @@ -102,27 +89,10 @@
</div>
<div className="flex items-center space-x-4 mb-6">
<input
className="flex-1 text-sm input input-bordered sm:text-base"
type="text"
placeholder="Year"
onChange={event => {
setYear(event.target.value);
}}
/>
<input
className="flex-1 text-sm input input-bordered sm:text-base"
type="text"
placeholder="Month"
onChange={event => {
setMonth(event.target.value);
}}
/>
<input
className="flex-1 text-sm input input-bordered sm:text-base"
type="text"
placeholder="Day"
onChange={event => {
setDay(event.target.value);
className='flex-1 text-sm input input-bordered sm:text-base'
type='datetime-local'
onChange={ event => {
setExpireDate(event.target.value);
}}
/>
<button
Expand All @@ -141,7 +111,7 @@
Advertisement Message
</th>
<th scope="col" className="px-6 py-3">
Expiriation Date
Expiration Date
</th>
<th scope="col" className="px-6 py-3">
Delete
Expand Down
Loading