Skip to content

Commit

Permalink
Merge branch 'total-old-events'
Browse files Browse the repository at this point in the history
  • Loading branch information
lil5 committed Dec 7, 2023
2 parents 31ac5d6 + 263052c commit 62e8428
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bruno/event/create.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: create
type: http
seq: 6
seq: 7
}

post {
Expand Down
2 changes: 1 addition & 1 deletion bruno/event/delete.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: delete
type: http
seq: 3
seq: 4
}

delete {
Expand Down
18 changes: 18 additions & 0 deletions bruno/event/get previous.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
meta {
name: get previous
type: http
seq: 3
}

get {
url: {{base}}/v2/event/previous?latitude=52.641460&longitude=5.056810&radius=1000&include_total=true
body: none
auth: none
}

query {
latitude: 52.641460
longitude: 5.056810
radius: 1000
include_total: true
}
2 changes: 1 addition & 1 deletion bruno/event/update Copy.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: update Copy
type: http
seq: 5
seq: 6
}

patch {
Expand Down
2 changes: 1 addition & 1 deletion bruno/event/update.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: update
type: http
seq: 4
seq: 5
}

patch {
Expand Down
1 change: 1 addition & 0 deletions frontend/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
"other": "Other",
"events": "Events",
"previousEvents": "Previous Events",
"nMoreEventsPrev": "<s>{{ n }}</s> more events<br/>previously posted",
"allEvents": "All Events",
"eventName": "Event name",
"sorryNoEvents": "Sorry, there are no events that match these filters.",
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/api/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ export const EVENT_IMAGE_EXPIRATION = 60 * 60 * 24 * 30 * 5;
export function eventGetAll(params?: EventGetAllParams) {
return window.axios.get<Event[]>("/v2/event/all", { params });
}
export function eventGetPrevious(params?: EventGetAllParams) {
return window.axios.get<Event[]>("/v2/event/previous", { params });
export interface EventGetPreviousResponse {
previous_events: Event[];
previous_total: number;
}
export function eventGetPrevious(
params?: EventGetAllParams & { include_total?: boolean }
) {
return window.axios.get<EventGetPreviousResponse>("/v2/event/previous", {
params,
});
}
export function eventGet(uid: UID) {
return window.axios.get<Event>(`/v2/event/${uid}`);
Expand Down
44 changes: 38 additions & 6 deletions frontend/src/pages/Events.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Helmet } from "react-helmet";
import { useState, useContext, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { Trans, useTranslation } from "react-i18next";
import { Link, useHistory } from "react-router-dom";

import { Event } from "../api/types";
import { eventGetAll, eventGetPrevious } from "../api/event";
import {
EventGetPreviousResponse,
eventGetAll,
eventGetPrevious,
} from "../api/event";
import CategoriesDropdown from "../components/CategoriesDropdown";
import { SizeBadges } from "../components/Badges";
import useForm from "../util/form.hooks";
Expand Down Expand Up @@ -38,7 +42,10 @@ export default function Events() {
const { addToastError, addModal } = useContext(ToastContext);
const authUser = useContext(AuthContext).authUser;
const [events, setEvents] = useState<Event[] | null>(null);
const [prevEvents, setPrevEvents] = useState<Event[] | null>(null);
const [prevEvents, setPrevEvents] = useState<EventGetPreviousResponse | null>(
null
);
const [nMorePrevEvents, setNMorePrevEvents] = useState(0);
const [values, setValue, setValues] = useForm<SearchValues>(() => {
const urlParams = new URLSearchParams("/events");
let latitude =
Expand Down Expand Up @@ -85,12 +92,22 @@ export default function Events() {
try {
const [allData, prevData] = await Promise.all([
eventGetAll({ latitude, longitude, radius }),
eventGetPrevious({ latitude, longitude, radius }),
eventGetPrevious({ latitude, longitude, radius, include_total: true }),
]);

const filterFunc = createFilterFunc(filterGenders);
setEvents(allData.data?.filter(filterFunc));
setPrevEvents(prevData.data);

{
let nMorePrevEvents = 0;
if (prevData.data?.previous_events) {
nMorePrevEvents =
prevData.data.previous_total - prevData.data.previous_events.length;
if (nMorePrevEvents < 0) nMorePrevEvents = 0;
}
setNMorePrevEvents(nMorePrevEvents);
}
} catch (err: any) {
addToastError(GinParseErrors(t, err), err.status);
}
Expand Down Expand Up @@ -236,7 +253,7 @@ export default function Events() {
))}
</div>
)}
{prevEvents ? (
{prevEvents?.previous_events ? (
<div key="event-prev">
<div className="sticky top-0 z-20 bg-white/50 flex justify-center">
<h4
Expand All @@ -249,14 +266,29 @@ export default function Events() {
</h4>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 opacity-70">
{prevEvents
{prevEvents.previous_events
.sort((a, b) =>
new Date(a.date) < new Date(b.date) ? 1 : -1
)
.map((event) => (
<EventItem event={event} key={event.uid} />
))}
</div>
{nMorePrevEvents ? (
<div className="flex justify-center">
<h4 className="font-semibold text-black/90 px-3 my-6">
<Trans
i18nKey="nMoreEventsPrev"
values={{
n: nMorePrevEvents,
}}
components={{
s: <span className="text-4xl text-accent" />,
}}
/>
</h4>
</div>
) : null}
</div>
) : null}
</div>
Expand Down
21 changes: 17 additions & 4 deletions server/internal/controllers/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ func EventGetPrevious(c *gin.Context) {
db := getDB(c)

var query struct {
Latitude float32 `form:"latitude" binding:"required,latitude"`
Longitude float32 `form:"longitude" binding:"required,longitude"`
Radius float32 `form:"radius"`
Latitude float32 `form:"latitude" binding:"required,latitude"`
Longitude float32 `form:"longitude" binding:"required,longitude"`
Radius float32 `form:"radius"`
IncludeTotal bool `form:"include_total"`
}
if err := c.ShouldBindQuery(&query); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
Expand All @@ -174,7 +175,19 @@ func EventGetPrevious(c *gin.Context) {
return
}

c.JSON(http.StatusOK, events)
res := gin.H{
"previous_events": events,
}
if query.IncludeTotal {
total := 0
db.Raw(`SELECT COUNT(*) FROM events WHERE date < NOW()`).Scan(&total)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
res["previous_total"] = total
}
c.JSON(http.StatusOK, res)
}

// The distance between two longlat points calculated in km.
Expand Down

0 comments on commit 62e8428

Please sign in to comment.