events api in frontend, setting routes for events
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
core "fr.latosa-escrima/api/core"
|
core "fr.latosa-escrima/api/core"
|
||||||
@@ -13,7 +14,7 @@ func HandleGetEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
_, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background())
|
_, err := core.DB.NewSelect().Model(&event).Where("uuid = ?", event_uuid).ScanAndCount(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.JSONError{
|
core.JSONError{
|
||||||
Status: core.Error,
|
Status: core.Error,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}.Respond(w, http.StatusInternalServerError)
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -22,7 +23,27 @@ func HandleGetEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
core.JSONSuccess{
|
core.JSONSuccess{
|
||||||
Status: core.Success,
|
Status: core.Success,
|
||||||
Message: "Event successfully sent",
|
Message: "Event successfully sent",
|
||||||
Data: event,
|
Data: event,
|
||||||
}.Respond(w, http.StatusOK)
|
}.Respond(w, http.StatusOK)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HangleGetEvents(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var events []core.Event
|
||||||
|
rowsCount, err := core.DB.NewSelect().Model(&events).ScanAndCount(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
core.JSONError{
|
||||||
|
Status: core.Error,
|
||||||
|
Message: err.Error(),
|
||||||
|
}.Respond(w, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
core.JSONSuccess{
|
||||||
|
Status: core.Success,
|
||||||
|
Message: fmt.Sprintf("%d Event successfully sent", rowsCount),
|
||||||
|
Data: events,
|
||||||
|
}.Respond(w, http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -106,22 +106,22 @@ func main() {
|
|||||||
Handler: api.HandleUpdateUser,
|
Handler: api.HandleUpdateUser,
|
||||||
Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}},
|
Middlewares: []core.Middleware{api.Methods("PATCH"), api.AuthJWT}},
|
||||||
"/events": {
|
"/events": {
|
||||||
Handler: api.HandleGetEvent,
|
Handler: api.HangleGetEvents,
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||||
"/events/new": {
|
"/events/new": {
|
||||||
Handler: api.HandleCreateEvent,
|
Handler: api.HandleCreateEvent,
|
||||||
Middlewares: []core.Middleware{api.Methods("POST")}},
|
Middlewares: []core.Middleware{api.Methods("POST")}},
|
||||||
"/events/{event_uuid}": {
|
"/events/{event_uuid}": {
|
||||||
Handler: api.HandleGetEvent,
|
Handler: api.HandleGetEvent,
|
||||||
Middlewares: []core.Middleware{api.Methods("GET")}},
|
Middlewares: []core.Middleware{api.Methods("GET")}},
|
||||||
"/events/{event_uuid}/delete": {
|
"/events/{event_uuid}/delete": {
|
||||||
Handler: api.HandleDeleteEvent,
|
Handler: api.HandleDeleteEvent,
|
||||||
Middlewares: []core.Middleware{api.Methods("DELETE")}},
|
Middlewares: []core.Middleware{api.Methods("DELETE")}},
|
||||||
"/events/{event_uuid}/update": {
|
"/events/{event_uuid}/update": {
|
||||||
Handler: api.HandleUpdateEvent,
|
Handler: api.HandleUpdateEvent,
|
||||||
Middlewares: []core.Middleware{api.Methods("PATCH")}},
|
Middlewares: []core.Middleware{api.Methods("PATCH")}},
|
||||||
"/blogs/new": {
|
"/blogs/new": {
|
||||||
Handler: api.HandleCreateBlog,
|
Handler: api.HandleCreateBlog,
|
||||||
Middlewares: []core.Middleware{api.Methods(("POST"))}},
|
Middlewares: []core.Middleware{api.Methods(("POST"))}},
|
||||||
"/blogs/{uuid}": {
|
"/blogs/{uuid}": {
|
||||||
Handler: api.HandleGetBlog,
|
Handler: api.HandleGetBlog,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { request } from "@/hooks/use-api";
|
import { request, useApi } from "@/hooks/use-api";
|
||||||
import "@schedule-x/theme-shadcn/dist/index.css";
|
import "@schedule-x/theme-shadcn/dist/index.css";
|
||||||
import { useNextCalendarApp, ScheduleXCalendar } from "@schedule-x/react";
|
import { useNextCalendarApp, ScheduleXCalendar } from "@schedule-x/react";
|
||||||
import { createEventsServicePlugin } from "@schedule-x/events-service";
|
import { createEventsServicePlugin } from "@schedule-x/events-service";
|
||||||
@@ -30,6 +30,7 @@ import {
|
|||||||
import { CalendarIcon } from "lucide-react";
|
import { CalendarIcon } from "lucide-react";
|
||||||
import { Calendar } from "@/components/ui/calendar";
|
import { Calendar } from "@/components/ui/calendar";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
import { requestFormReset } from "react-dom";
|
||||||
|
|
||||||
const Planning = () => {
|
const Planning = () => {
|
||||||
const plugins = [createEventsServicePlugin()];
|
const plugins = [createEventsServicePlugin()];
|
||||||
@@ -47,6 +48,8 @@ const Planning = () => {
|
|||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const [requestCreateEvent, setRequestCreateEvent] = useState(false)
|
||||||
|
|
||||||
const calendar = useNextCalendarApp(
|
const calendar = useNextCalendarApp(
|
||||||
{
|
{
|
||||||
theme: "shadcn",
|
theme: "shadcn",
|
||||||
@@ -63,12 +66,48 @@ const Planning = () => {
|
|||||||
callbacks: {
|
callbacks: {
|
||||||
onEventClick(event, e) {
|
onEventClick(event, e) {
|
||||||
setEventSelected(event);
|
setEventSelected(event);
|
||||||
}
|
},
|
||||||
|
async onClickDateTime(dateTime) {
|
||||||
|
setRequestCreateEvent(true)
|
||||||
|
const newEvent: CalendarEventExternal = {
|
||||||
|
id: "5",
|
||||||
|
title: "Event 1",
|
||||||
|
start: dateTime,
|
||||||
|
end: format(
|
||||||
|
new Date(dateTime).getTime() + (1000 * 60 * 60),
|
||||||
|
"yyyy-MM-dd HH:mm",
|
||||||
|
),
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const res = await request<undefined>(
|
||||||
|
`/events/new`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(newEvent),
|
||||||
|
requiresAuth: true,
|
||||||
|
csrfToken: false
|
||||||
|
},)
|
||||||
|
if (res.status === "Error") {
|
||||||
|
console.log("Error")
|
||||||
|
}
|
||||||
|
if (res.status === "Success") {
|
||||||
|
console.log("Success")
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
plugins,
|
plugins,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const {data: requestedEvents, isLoading, success} = useApi("/events", {
|
||||||
|
onSuccess: (data) => {
|
||||||
|
calendar?.events.set(data)
|
||||||
|
}
|
||||||
|
}, false, false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// get all events
|
// get all events
|
||||||
calendar?.events.getAll();
|
calendar?.events.getAll();
|
||||||
@@ -176,32 +215,57 @@ const Planning = () => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<DialogFooter> <Button
|
<DialogFooter className="flex flex-row justify-end">
|
||||||
onClick={async () => {
|
<Button className="bg-red-700"
|
||||||
calendar?.events?.update(eventSelected!)
|
onClick={async () => {
|
||||||
try {
|
calendar?.events?.remove(eventSelected!.id)
|
||||||
const res = await request<undefined>(
|
try {
|
||||||
`/events/${eventSelected!.id}/update`,
|
const res = await request<undefined>(
|
||||||
{
|
`/events/${eventSelected!.id}/delete`,
|
||||||
method: "PATCH",
|
{
|
||||||
body: JSON.stringify(eventSelected),
|
method: "DELETE",
|
||||||
requiresAuth: true,
|
body: JSON.stringify(eventSelected),
|
||||||
csrfToken: false
|
requiresAuth: false,
|
||||||
},)
|
csrfToken: false
|
||||||
if (res.status === "Error") {
|
},)
|
||||||
console.log("Error")
|
if (res.status === "Error") {
|
||||||
|
console.log("Error")
|
||||||
|
}
|
||||||
|
if (res.status === "Success") {
|
||||||
|
console.log("Success")
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
}
|
}
|
||||||
if (res.status === "Success") {
|
}}
|
||||||
console.log("Success")
|
type="submit">
|
||||||
|
Supprimer
|
||||||
|
</Button>
|
||||||
|
<Button className="bg-blue-500"
|
||||||
|
onClick={async () => {
|
||||||
|
calendar?.events?.update(eventSelected!)
|
||||||
|
try {
|
||||||
|
const res = await request<undefined>(
|
||||||
|
`/events/${eventSelected!.id}/update`,
|
||||||
|
{
|
||||||
|
method: "PATCH",
|
||||||
|
body: JSON.stringify(eventSelected),
|
||||||
|
requiresAuth: true,
|
||||||
|
csrfToken: false
|
||||||
|
},)
|
||||||
|
if (res.status === "Error") {
|
||||||
|
console.log("Error")
|
||||||
|
}
|
||||||
|
if (res.status === "Success") {
|
||||||
|
console.log("Success")
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
}
|
}
|
||||||
} catch(e) {
|
}}
|
||||||
console.log(e)
|
type="submit">
|
||||||
}
|
Actualiser
|
||||||
}}
|
</Button>
|
||||||
type="submit"
|
|
||||||
>
|
|
||||||
Mettre à jour
|
|
||||||
</Button>
|
|
||||||
</DialogFooter>
|
</DialogFooter>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
|
|||||||
Reference in New Issue
Block a user