Skip to content

Commit

Permalink
Solved issue Number-6143
Browse files Browse the repository at this point in the history
When selecting a start datetime, the end datetime should be after the start datetime. #6143.

github - Harshit-7373.
  • Loading branch information
Harshit-7373 committed Nov 16, 2024
1 parent 8ea1215 commit 1cdcdbe
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
64 changes: 52 additions & 12 deletions apps/vite-ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import dns from 'dns';

const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD;

// If DNS returns both ipv4 and ipv6 addresses, prefer ipv4

export async function createServer(
root = process.cwd(),
isProd = process.env.NODE_ENV === 'production',
Expand All @@ -16,12 +14,9 @@ export async function createServer(
const app = express();

const prodIndexHtml = isProd
? await fs.readFile('./dist/client/index.html', 'utf-8')
? await fs.readFile('./event_scheduler/dist/client/index.html', 'utf-8')
: '';

/**
* @type {import('vite').ViteDevServer}
*/
let vite;
if (!isProd) {
vite = await (
Expand All @@ -32,8 +27,6 @@ export async function createServer(
server: {
middlewareMode: true,
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100,
},
Expand All @@ -43,14 +36,62 @@ export async function createServer(
},
appType: 'custom',
});
// use vite's connect instance as middleware
app.use(vite.middlewares);
} else {
const sirv = (await import('sirv')).default;
app.use((await import('compression')).default());
app.use('/', sirv('./dist/client', { extensions: [] }));
app.use('/', sirv('./event_scheduler/dist/client', { extensions: [] }));
}

// Middleware to parse JSON requests
app.use(express.json());

// Event scheduling validation logic
app.post('/schedule-event', (req, res) => {
const { startDatetime, endDatetime, isAllDay, isRecurring } = req.body;

if (!startDatetime) {
return res.status(400).json({ error: 'Start datetime is required.' });
}

const start = new Date(startDatetime);
let end = endDatetime ? new Date(endDatetime) : null;

// Validate event times
if (end && end <= start) {
return res.status(400).json({
error: 'End datetime must be after start datetime.',
});
}

// Handle all-day event logic
if (isAllDay) {
const startDate = new Date(start.toDateString());
const endDate = new Date(start.toDateString());
endDate.setHours(23, 59, 59);
end = endDate;

return res.json({
message: 'All-day event created.',
event: { start: startDate, end: endDate },
});
}

// Handle recurring event logic
if (isRecurring && !end) {
return res.json({
message: 'Recurring event created without end datetime.',
event: { start },
});
}

// Return successful response
res.json({
message: 'Event scheduled successfully.',
event: { start, end },
});
});

app.use('*', async (req, res) => {
try {
const url = req.originalUrl;
Expand All @@ -62,7 +103,6 @@ export async function createServer(
return;
}

// Extract the head from vite's index transformation hook
let viteHead = !isProd
? await vite.transformIndexHtml(
url,
Expand All @@ -79,7 +119,7 @@ export async function createServer(
if (!isProd) {
return vite.ssrLoadModule('/src/entry-server.tsx');
} else {
return import('./dist/server/entry-server.js');
return import('./event_scheduler/dist/server/entry-server.js');
}
})();

Expand Down
26 changes: 26 additions & 0 deletions event_scheduler/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Event Scheduler</title>
<script defer src="script.js"></script>
</head>
<body>
<form id="event-form">
<label for="start-datetime">Start Datetime:</label>
<input type="datetime-local" id="start-datetime" name="start-datetime" required>

<label for="end-datetime">End Datetime:</label>
<input type="datetime-local" id="end-datetime" name="end-datetime">

<label for="all-day-checkbox">All-Day Event:</label>
<input type="checkbox" id="all-day-checkbox">

<label for="is-recurring">Recurring Event:</label>
<input type="checkbox" id="is-recurring">

<button type="submit">Save Event</button>
</form>
</body>
</html>
46 changes: 46 additions & 0 deletions event_scheduler/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
document.getElementById("event-form").addEventListener("submit", function (event) {
event.preventDefault();

const startField = document.getElementById("start-datetime");
const endField = document.getElementById("end-datetime");
const allDayCheckbox = document.getElementById("all-day-checkbox");

const start = new Date(startField.value);
const end = new Date(endField.value);

// Validate start and end times
if (!allDayCheckbox.checked && end <= start) {
alert("End datetime must be after start datetime.");
return;
}

// Handle all-day event logic
if (allDayCheckbox.checked) {
const startDate = start.toISOString().slice(0, 10);
startField.value = `${startDate}T00:00`;
endField.value = `${startDate}T23:59`;
}

// Send data to the backend
const eventData = {
startDatetime: startField.value,
endDatetime: endField.value,
isAllDay: allDayCheckbox.checked,
isRecurring: document.getElementById("is-recurring").checked,
};

fetch("/create-event", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(eventData),
})
.then((response) => response.json())
.then((data) => {
if (data.error) {
alert(data.error);
} else {
alert("Event saved successfully!");
}
})
.catch((error) => console.error("Error:", error));
});

0 comments on commit 1cdcdbe

Please sign in to comment.