Skip to content

Commit 44487d3

Browse files
committed
✨ join story with empty vote
1 parent 72eb5b4 commit 44487d3

File tree

10 files changed

+73
-14
lines changed

10 files changed

+73
-14
lines changed

src/back-end/routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Application } from "express";
22
import glitchWebhook from "./routes/glitch-webhook";
33
import { storySSE } from "./routes/story/events";
4+
import join from "./routes/story/join";
45
import reveal from "./routes/story/reveal";
56
import vote from "./routes/story/vote";
67

78
const routes = async (app: Application) => {
89
glitchWebhook(app);
10+
join(app);
911
reveal(app);
1012
storySSE(app);
1113
vote(app);

src/back-end/routes/story/join.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Application } from "express";
2+
import { remult } from "remult";
3+
import { Story } from "../../../models/story";
4+
import server from "../../server";
5+
import { updateStory } from "./events";
6+
7+
const join = (app: Application) =>
8+
app.post("/story/:story/join", server.withRemult, async (r, s) => {
9+
if (!remult.user) {
10+
s.sendStatus(403);
11+
return;
12+
}
13+
14+
const storyId = r.params.story;
15+
const story = await remult.repo(Story).findId(storyId);
16+
17+
console.log(`User ${remult.user.id} joining ${storyId}`);
18+
19+
if (!story._votes?.find((v) => v.participant.id === remult.user?.id)) {
20+
if (!story._votes) story._votes = [];
21+
22+
story._votes.push({
23+
participant: r.body,
24+
vote: null,
25+
});
26+
27+
await remult.repo(Story).update(story.id, story);
28+
}
29+
30+
const updatedStory = await remult.repo(Story).findId(storyId);
31+
32+
if (!updatedStory) {
33+
throw new Error("No story");
34+
}
35+
36+
updateStory(updatedStory);
37+
s.sendStatus(201);
38+
});
39+
40+
export default join;

src/back-end/routes/story/reveal.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ const reveal = (app: Application) =>
1212
await remult.repo(Story).update(story, { revealed: true });
1313
s.sendStatus(202);
1414

15-
const storyObject = await remult.repo(Story).findId(story);
15+
const updatedStory = await remult.repo(Story).findId(story);
1616

17-
if (!storyObject) {
17+
if (!updatedStory) {
1818
throw new Error("No story");
1919
}
2020

21-
updateStory(storyObject);
21+
updateStory(updatedStory);
2222
});
2323

2424
export default reveal;

src/front-end/scripts/components/estimate.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const Estimate = defineComponent({
3636
const votes = new Map<string, number>();
3737
3838
store.state.story.story.votes?.map((v: Vote) => {
39+
if (v.vote === null) return;
40+
3941
const value = v.vote.toString();
4042
4143
if (!votes.has(value)) votes.set(value, 0);
@@ -66,7 +68,7 @@ const Estimate = defineComponent({
6668
return {
6769
participant: {
6870
id: this.$store.state.session.id,
69-
name: this.$store.state.session.userName,
71+
name: this.$store.state.session.name,
7072
},
7173
vote: option,
7274
} as Vote;

src/front-end/scripts/components/participants.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export default Participants;
2222
</span>
2323
</span>
2424
<span class="value">
25-
<span>{{ v.vote }}</span>
25+
<span :title="!v.vote ? 'Waiting' : 'Voted'">
26+
{{ v.vote ?? "⏱️" }}
27+
</span>
2628
</span>
2729
</li>
2830
</ul>

src/front-end/scripts/store/session.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const SESSION_PREFIX = `${LOCALSTORAGE_GLOBAL_PREFIX}session.`;
88
const keys = {
99
darkMode: `${SESSION_PREFIX}darkMode`,
1010
sessionId: `${SESSION_PREFIX}sessionId`,
11-
userName: `${SESSION_PREFIX}userName`,
11+
name: `${SESSION_PREFIX}name`,
1212
};
1313

1414
const session: Module<SessionState, StoreState> = {
@@ -17,9 +17,9 @@ const session: Module<SessionState, StoreState> = {
1717
state.settings.darkMode = payload;
1818
localStorage.setItem(keys.darkMode, payload.toString());
1919
},
20-
"session.settings.userName"(state, payload: string) {
21-
state.userName = payload;
22-
localStorage.setItem(keys.userName, payload);
20+
"session.settings.name"(state, payload: string) {
21+
state.name = payload;
22+
localStorage.setItem(keys.name, payload);
2323
},
2424
},
2525
state() {
@@ -32,7 +32,7 @@ const session: Module<SessionState, StoreState> = {
3232

3333
return {
3434
id: sessionId,
35-
userName: localStorage.getItem(keys.userName) ?? "User",
35+
name: localStorage.getItem(keys.name) ?? "User",
3636
settings: {
3737
darkMode: JSON.parse(localStorage.getItem(keys.darkMode) ?? "false"),
3838
},

src/front-end/scripts/store/story.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,27 @@ import { StoreState, StoryState } from "./types";
88

99
const story: Module<StoryState | Promise<StoryState>, StoreState> = {
1010
actions: {
11+
async "story.join"(ctx) {
12+
const storyId = router.currentRoute.value.params.story as string;
13+
14+
await fetch(`${ROOT_URI}story/${storyId}/join`, {
15+
body: JSON.stringify(ctx.rootState.session),
16+
headers: { "Content-Type": "application/json" },
17+
method: "POST",
18+
});
19+
},
1120
async "story.load"(ctx) {
21+
const firstLoad = !(ctx.state as StoryState).events;
22+
23+
if (firstLoad) await ctx.dispatch("story.join");
24+
1225
const story = await remult
1326
.repo(Story)
1427
.findId(router.currentRoute.value.params.story as string, {
1528
useCache: false,
1629
});
1730

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

2134
events.addEventListener("message", () => {

src/front-end/scripts/store/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type SessionSettings = {
66

77
export type SessionState = {
88
id: string;
9-
userName: string;
9+
name: string;
1010
settings: SessionSettings;
1111
};
1212

src/models/story.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class Story {
3131
return {
3232
...v,
3333
participant: { ...v.participant, id: "" },
34-
vote: s.revealed ? v.vote : "?",
34+
vote: s.revealed ? v.vote : v.vote ? "❓" : null,
3535
};
3636
});
3737
};

src/models/vote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export class Vote {
66
participant!: Participant;
77

88
@Fields.string()
9-
vote!: string;
9+
vote: string | null = null;
1010
}

0 commit comments

Comments
 (0)