first commit
This commit is contained in:
118
resources/js/plugins/fake-api/handlers/apps/calendar/db.js
Normal file
118
resources/js/plugins/fake-api/handlers/apps/calendar/db.js
Normal file
@@ -0,0 +1,118 @@
|
||||
const date = new Date()
|
||||
const nextDay = new Date(new Date().getTime() + 24 * 60 * 60 * 1000)
|
||||
const nextMonth = date.getMonth() === 11 ? new Date(date.getFullYear() + 1, 0, 1) : new Date(date.getFullYear(), date.getMonth() + 1, 1)
|
||||
const prevMonth = date.getMonth() === 11 ? new Date(date.getFullYear() - 1, 0, 1) : new Date(date.getFullYear(), date.getMonth() - 1, 1)
|
||||
export const db = {
|
||||
events: [
|
||||
{
|
||||
id: 1,
|
||||
url: '',
|
||||
title: 'Design Review',
|
||||
start: date,
|
||||
end: nextDay,
|
||||
allDay: false,
|
||||
extendedProps: {
|
||||
calendar: 'Business',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
url: '',
|
||||
title: 'Meeting With Client',
|
||||
start: new Date(date.getFullYear(), date.getMonth() + 1, -11),
|
||||
end: new Date(date.getFullYear(), date.getMonth() + 1, -10),
|
||||
allDay: true,
|
||||
extendedProps: {
|
||||
calendar: 'Business',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
url: '',
|
||||
title: 'Family Trip',
|
||||
allDay: true,
|
||||
start: new Date(date.getFullYear(), date.getMonth() + 1, -9),
|
||||
end: new Date(date.getFullYear(), date.getMonth() + 1, -7),
|
||||
extendedProps: {
|
||||
calendar: 'Holiday',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
url: '',
|
||||
title: 'Doctor\'s Appointment',
|
||||
start: new Date(date.getFullYear(), date.getMonth() + 1, -11),
|
||||
end: new Date(date.getFullYear(), date.getMonth() + 1, -10),
|
||||
allDay: true,
|
||||
extendedProps: {
|
||||
calendar: 'Personal',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
url: '',
|
||||
title: 'Dart Game?',
|
||||
start: new Date(date.getFullYear(), date.getMonth() + 1, -13),
|
||||
end: new Date(date.getFullYear(), date.getMonth() + 1, -12),
|
||||
allDay: true,
|
||||
extendedProps: {
|
||||
calendar: 'ETC',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
url: '',
|
||||
title: 'Meditation',
|
||||
start: new Date(date.getFullYear(), date.getMonth() + 1, -13),
|
||||
end: new Date(date.getFullYear(), date.getMonth() + 1, -12),
|
||||
allDay: true,
|
||||
extendedProps: {
|
||||
calendar: 'Personal',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
url: '',
|
||||
title: 'Dinner',
|
||||
start: new Date(date.getFullYear(), date.getMonth() + 1, -13),
|
||||
end: new Date(date.getFullYear(), date.getMonth() + 1, -12),
|
||||
allDay: true,
|
||||
extendedProps: {
|
||||
calendar: 'Family',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
url: '',
|
||||
title: 'Product Review',
|
||||
start: new Date(date.getFullYear(), date.getMonth() + 1, -13),
|
||||
end: new Date(date.getFullYear(), date.getMonth() + 1, -12),
|
||||
allDay: true,
|
||||
extendedProps: {
|
||||
calendar: 'Business',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 9,
|
||||
url: '',
|
||||
title: 'Monthly Meeting',
|
||||
start: nextMonth,
|
||||
end: nextMonth,
|
||||
allDay: true,
|
||||
extendedProps: {
|
||||
calendar: 'Business',
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 10,
|
||||
url: '',
|
||||
title: 'Monthly Checkup',
|
||||
start: prevMonth,
|
||||
end: prevMonth,
|
||||
allDay: true,
|
||||
extendedProps: {
|
||||
calendar: 'Personal',
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
import is from '@sindresorhus/is'
|
||||
import destr from 'destr'
|
||||
import { rest } from 'msw'
|
||||
import { db } from '@db/apps/calendar/db'
|
||||
import { genId } from '@api-utils/genId'
|
||||
|
||||
export const handlerAppsCalendar = [
|
||||
// 👉 Get Calendar Events
|
||||
rest.get(('/api/apps/calendar'), (req, res, ctx) => {
|
||||
const queries = req.url.searchParams.getAll('calendars')
|
||||
const parsedCalendars = destr(queries)
|
||||
const calendars = is.array(parsedCalendars) ? parsedCalendars : undefined
|
||||
const events = db.events.filter(event => calendars?.includes(event.extendedProps.calendar))
|
||||
|
||||
return res(ctx.status(200), ctx.json(events))
|
||||
}),
|
||||
|
||||
// 👉 Add Calendar Event
|
||||
rest.post(('/api/apps/calendar'), async (req, res, ctx) => {
|
||||
const event = await req.json()
|
||||
|
||||
db.events.push({
|
||||
...event,
|
||||
id: genId(db.events),
|
||||
})
|
||||
|
||||
return res(ctx.status(200), ctx.json(event))
|
||||
}),
|
||||
|
||||
// 👉 Update Calendar Event
|
||||
rest.put(('/api/apps/calendar/:id'), async (req, res, ctx) => {
|
||||
const updatedEvent = await req.json()
|
||||
|
||||
updatedEvent.id = Number(updatedEvent.id)
|
||||
|
||||
const eventId = Number(req.params.id)
|
||||
|
||||
// Find the index of the event in the database
|
||||
const currentEvent = db.events.find(e => e.id === eventId)
|
||||
|
||||
// update event
|
||||
if (currentEvent) {
|
||||
Object.assign(currentEvent, updatedEvent)
|
||||
|
||||
return res(ctx.status(200), ctx.json(currentEvent))
|
||||
}
|
||||
|
||||
return res(ctx.status(400), ctx.json({ message: 'Something Went Wrong' }))
|
||||
}),
|
||||
|
||||
// 👉 Delete Calendar Event
|
||||
rest.delete(('/api/apps/calendar/:id'), (req, res, ctx) => {
|
||||
const eventId = Number(req.params.id)
|
||||
const eventIndex = db.events.findIndex(e => e.id === eventId)
|
||||
if (eventIndex !== -1) {
|
||||
db.events.splice(eventIndex, 1)
|
||||
|
||||
return res(ctx.status(204))
|
||||
}
|
||||
|
||||
return res(ctx.status(400), ctx.json({ message: 'Something Went Wrong' }))
|
||||
}),
|
||||
]
|
@@ -0,0 +1 @@
|
||||
export {}
|
Reference in New Issue
Block a user