Skip to content

Commit

Permalink
Generate unique member IDs for component reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
jouni-kantola committed Apr 7, 2024
1 parent 47ebd40 commit 70cc18e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
28 changes: 23 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"homepage": "https://github.com/jouni-kantola/mobtimer#readme",
"dependencies": {
"@neutralinojs/lib": "^5.1.0",
"nanoid": "^5.0.7",
"vue": "^3.4.21"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion resources/scripts/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
</div>
<form>
<TeamMember
v-for="{ name, index, isActive } in team"
v-for="{ id, name, index, isActive } in team"
:key="id"
:index="index"
:name="name"
:isActive="isActive"
Expand Down
15 changes: 8 additions & 7 deletions resources/scripts/team.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { nanoid } from "nanoid";

export type Member = {
id: string;
index: number;
name: string;
isHere: boolean;
isActive: boolean;
};

export function createTeam(members: Array<string>): Array<Member> {
return members.map((name, index) => ({
index,
name,
isHere: true,
isActive: index === 0,
}));
const team = new Array<Member>();
members.forEach(name => addMember(name, team));
return team;
}

export function whosNext(team: Array<Member>) {
Expand Down Expand Up @@ -62,10 +62,11 @@ export function getLast(team: Array<Member>) {

function addMember(name: string, team: Array<Member>) {
team.push({
id: nanoid(),
index: team.length,
name,
isHere: true,
isActive: false,
isActive: team.length === 0,
});
}

Expand Down
19 changes: 17 additions & 2 deletions test/team-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,24 @@ import {

test("map names to team", () => {
const team = createTeam(defaultMembers);
assert.strictEqual(team[0].index, 0);
assert.strictEqual(team[0].name, "Member 1");

assert.isTrue(team.every(member => member.isHere));
assert.lengthOf(
team.filter(member => member.isActive),
1
);

const activeMember = team[0];
assert.strictEqual(activeMember.isActive, true);
assert.strictEqual(activeMember.index, 0);
assert.strictEqual(activeMember.name, "Member 1");

assert.strictEqual(team.at(-1)!.index, 5);
assert.strictEqual(team.at(-1)!.name, "Member 6");

const ids = team.map(member => member.id);
const uniqueIds = [...new Set(ids)];
assert.lengthOf(uniqueIds, ids.length);
});

test("whos next when everyone here", () => {
Expand Down Expand Up @@ -80,6 +94,7 @@ test("increase team size", () => {
assert.strictEqual(team.at(-1)!.index, 4);
assert.strictEqual(team.at(-1)!.isHere, true);
assert.strictEqual(team.at(-1)!.isActive, false);
assert.ok(team.at(-1)!.id);
});

test("decrease team size", () => {
Expand Down

0 comments on commit 70cc18e

Please sign in to comment.