Skip to content

Commit

Permalink
✨ join story with empty vote
Browse files Browse the repository at this point in the history
  • Loading branch information
haliphax committed Sep 13, 2023
1 parent 72eb5b4 commit 44487d3
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/back-end/routes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Application } from "express";
import glitchWebhook from "./routes/glitch-webhook";
import { storySSE } from "./routes/story/events";
import join from "./routes/story/join";
import reveal from "./routes/story/reveal";
import vote from "./routes/story/vote";

const routes = async (app: Application) => {
glitchWebhook(app);
join(app);
reveal(app);
storySSE(app);
vote(app);
Expand Down
40 changes: 40 additions & 0 deletions src/back-end/routes/story/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Application } from "express";
import { remult } from "remult";
import { Story } from "../../../models/story";
import server from "../../server";
import { updateStory } from "./events";

const join = (app: Application) =>
app.post("/story/:story/join", server.withRemult, async (r, s) => {
if (!remult.user) {
s.sendStatus(403);
return;
}

const storyId = r.params.story;
const story = await remult.repo(Story).findId(storyId);

console.log(`User ${remult.user.id} joining ${storyId}`);

if (!story._votes?.find((v) => v.participant.id === remult.user?.id)) {
if (!story._votes) story._votes = [];

story._votes.push({
participant: r.body,
vote: null,
});

await remult.repo(Story).update(story.id, story);
}

const updatedStory = await remult.repo(Story).findId(storyId);

if (!updatedStory) {
throw new Error("No story");
}

updateStory(updatedStory);
s.sendStatus(201);
});

export default join;
6 changes: 3 additions & 3 deletions src/back-end/routes/story/reveal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const reveal = (app: Application) =>
await remult.repo(Story).update(story, { revealed: true });
s.sendStatus(202);

const storyObject = await remult.repo(Story).findId(story);
const updatedStory = await remult.repo(Story).findId(story);

if (!storyObject) {
if (!updatedStory) {
throw new Error("No story");
}

updateStory(storyObject);
updateStory(updatedStory);
});

export default reveal;
4 changes: 3 additions & 1 deletion src/front-end/scripts/components/estimate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const Estimate = defineComponent({
const votes = new Map<string, number>();
store.state.story.story.votes?.map((v: Vote) => {
if (v.vote === null) return;
const value = v.vote.toString();
if (!votes.has(value)) votes.set(value, 0);
Expand Down Expand Up @@ -66,7 +68,7 @@ const Estimate = defineComponent({
return {
participant: {
id: this.$store.state.session.id,
name: this.$store.state.session.userName,
name: this.$store.state.session.name,
},
vote: option,
} as Vote;
Expand Down
4 changes: 3 additions & 1 deletion src/front-end/scripts/components/participants.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export default Participants;
</span>
</span>
<span class="value">
<span>{{ v.vote }}</span>
<span :title="!v.vote ? 'Waiting' : 'Voted'">
{{ v.vote ?? "⏱️" }}
</span>
</span>
</li>
</ul>
Expand Down
10 changes: 5 additions & 5 deletions src/front-end/scripts/store/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const SESSION_PREFIX = `${LOCALSTORAGE_GLOBAL_PREFIX}session.`;
const keys = {
darkMode: `${SESSION_PREFIX}darkMode`,
sessionId: `${SESSION_PREFIX}sessionId`,
userName: `${SESSION_PREFIX}userName`,
name: `${SESSION_PREFIX}name`,
};

const session: Module<SessionState, StoreState> = {
Expand All @@ -17,9 +17,9 @@ const session: Module<SessionState, StoreState> = {
state.settings.darkMode = payload;
localStorage.setItem(keys.darkMode, payload.toString());
},
"session.settings.userName"(state, payload: string) {
state.userName = payload;
localStorage.setItem(keys.userName, payload);
"session.settings.name"(state, payload: string) {
state.name = payload;
localStorage.setItem(keys.name, payload);
},
},
state() {
Expand All @@ -32,7 +32,7 @@ const session: Module<SessionState, StoreState> = {

return {
id: sessionId,
userName: localStorage.getItem(keys.userName) ?? "User",
name: localStorage.getItem(keys.name) ?? "User",
settings: {
darkMode: JSON.parse(localStorage.getItem(keys.darkMode) ?? "false"),
},
Expand Down
15 changes: 14 additions & 1 deletion src/front-end/scripts/store/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ import { StoreState, StoryState } from "./types";

const story: Module<StoryState | Promise<StoryState>, StoreState> = {
actions: {
async "story.join"(ctx) {
const storyId = router.currentRoute.value.params.story as string;

await fetch(`${ROOT_URI}story/${storyId}/join`, {
body: JSON.stringify(ctx.rootState.session),
headers: { "Content-Type": "application/json" },
method: "POST",
});
},
async "story.load"(ctx) {
const firstLoad = !(ctx.state as StoryState).events;

if (firstLoad) await ctx.dispatch("story.join");

const story = await remult
.repo(Story)
.findId(router.currentRoute.value.params.story as string, {
useCache: false,
});

if (!(ctx.state as StoryState).events) {
if (firstLoad) {
const events = new EventSource(`${ROOT_URI}story/${story.id}/events`);

events.addEventListener("message", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/front-end/scripts/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type SessionSettings = {

export type SessionState = {
id: string;
userName: string;
name: string;
settings: SessionSettings;
};

Expand Down
2 changes: 1 addition & 1 deletion src/models/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Story {
return {
...v,
participant: { ...v.participant, id: "" },
vote: s.revealed ? v.vote : "?",
vote: s.revealed ? v.vote : v.vote ? "❓" : null,
};
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/models/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export class Vote {
participant!: Participant;

@Fields.string()
vote!: string;
vote: string | null = null;
}

0 comments on commit 44487d3

Please sign in to comment.