Skip to content

Commit

Permalink
feat: publish xp updated event
Browse files Browse the repository at this point in the history
  • Loading branch information
HamoonZamiri committed Sep 11, 2024
1 parent c65bf68 commit c3cfddf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions backend/users/service/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,12 @@ func (us *userService) handleGoalUpdatedEvent(event events.Event) {
slog.Error("service.handleGoalUpdatedEvent: store.UpdateUserById:", "err", err)
return
}

eventData := &events.XpUpdatedData{
LevelId: newLevel,
Xp: newXp,
}
us.eventPublisher.Publish(events.NewEventWithUserId(events.XP_UPDATED, eventData, oldGoal.UserId.String()))

}
}
5 changes: 5 additions & 0 deletions backend/utils/events/event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ type GoalUpdatedData struct {
NewGoal *entities.Goal
Xp int
}

type XpUpdatedData struct {
LevelId int `json:"level_id"`
Xp int `json:"xp"`
}
1 change: 1 addition & 0 deletions backend/utils/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
GOAL_CATEGORY_CREATED string = "goal_category_created"
DEFAULT_GOAL_CREATED string = "default_goal_created"
SSE_CONNECTED string = "sse_connected"
XP_UPDATED string = "xp_updated"
)

func ParseEventData[T any](event Event) (T, error) {
Expand Down
17 changes: 16 additions & 1 deletion frontend/src/hooks/events/useSse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { events } from "@/utils/constants";
import { Schemas } from "@/utils/schemas";
import { Schemas, type User } from "@/utils/schemas";
import { onUnmounted, ref } from "vue";
import useGoals from "@/hooks/goals/useGoals";
import useAuth from "../auth/useAuth";
import { z } from "zod";

const xpUpdateSchema = z.object({
xp: z.number(),
level_id: z.number(),
});

export function useSSE(url: string) {
const { addGoal } = useGoals();
const { getUser, setUser } = useAuth();
const eventSource = ref<EventSource | null>(null);

const connect = () => {
Expand All @@ -31,6 +39,13 @@ export function useSSE(url: string) {
console.log("initial sse event");
});

es.addEventListener(events.XP_UPDATED, (event) => {
const json = JSON.parse(event.data);
const parsedData = xpUpdateSchema.parse(json);
const user = getUser() as User;
setUser({ ...user, ...parsedData });
});

eventSource.value = es;
};

Expand Down
1 change: 1 addition & 0 deletions frontend/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const events = {
GOAL_CATEGORY_CREATED: "goal_category_created",
DEFAULT_GOAL_CREATED: "default_goal_created",
SSE_CONNECTED: "sse_connected",
XP_UPDATED: "xp_updated",
} as const;

export const http = {
Expand Down

0 comments on commit c3cfddf

Please sign in to comment.