Skip to content

Commit

Permalink
store uuids instead of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
gskorokhod committed Aug 12, 2024
1 parent a119392 commit a397553
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import type { Location, Person } from "$lib/types";
import type { Location } from "$lib/types";
import { getConstraintsFor, locations } from "$lib/stores.ts";
import { createRender, DataBodyCell, FlatColumn, type ReadOrWritable } from "svelte-headless-table";
import LocationBadge from "$lib/components/elements/location/location-badge.svelte";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import type { Location, Person, Skill, Task } from "$lib/types";
import type { Person, Skill, Task } from "$lib/types";
import { skills } from "$lib/stores.ts";
import { createRender, DataBodyCell, FlatColumn, type ReadOrWritable } from "svelte-headless-table";
import SkillBadge from "$lib/components/elements/skill/skill-badge.svelte";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import type { AnyPlugins } from "svelte-headless-table/plugins";
import type { ColumnInitializer } from "$lib/components/elements/data-tables/core";
import RowActions from "$lib/components/elements/data-tables/lib/row-actions.svelte";
import { getRequiredSkillsForTask } from "$lib/types/task.ts";
let data: ReadOrWritable<Task[]> = tasks;
let actions: Map<string, (item: Task) => void> = new Map();
Expand Down Expand Up @@ -54,7 +55,7 @@
},
{
id: "required_skills",
accessor: (row: Task) => row.required_skills,
accessor: (row: Task) => getRequiredSkillsForTask(row),
header: "Required skills",
cell: (cell: DataBodyCell<unknown>) => createRender(SkillsList, {
skills: cell.value as Skill[],
Expand Down
3 changes: 2 additions & 1 deletion src/lib/components/elements/shift/shift-card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { ClockIcon, MapPinIcon } from "lucide-svelte";
import { capitalize } from "$lib/utils.ts";
import { fmtDateTime } from "$lib/utils.js";
import { getTasksForShift } from "$lib/types/shift.ts";
let shift: Shift;
Expand All @@ -28,7 +29,7 @@
{shift.location.name}
</div>
<div class="flex flex-col items-stretch justify-start gap-3 w-full mt-4">
{#each shift.tasks as task}
{#each getTasksForShift(shift) as task}
<Task task={task} />
{/each}
</div>
Expand Down
10 changes: 7 additions & 3 deletions src/lib/components/elements/task/task-card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import PeopleSelectorList from "$lib/components/elements/person/people-selector-list.svelte";
import { capitalize } from "$lib/utils.ts";
import SkillsList from "$lib/components/elements/skill/skills-list.svelte";
import { getAssignedPeopleForTask, getRequiredSkillsForTask } from "$lib/types/task.ts";
let task: Task;
let compact: boolean = false;
$: required_skills = getRequiredSkillsForTask(task);
$: people = getAssignedPeopleForTask(task);
export { task, compact };
</script>

Expand All @@ -18,11 +22,11 @@

{#if !compact}
<p class="ml-1 mb-3 text-muted-foreground">{task.description}</p>
{#if task.required_skills.length > 0}
<SkillsList skills={task.required_skills} compact={false} class="mb-4" />
{#if required_skills.length > 0}
<SkillsList skills={required_skills} compact={false} class="mb-4" />
{/if}
{/if}

<PeopleSelectorList people={task.people} min_people={task.min_people} max_people={task.max_people}
<PeopleSelectorList people={people} min_people={task.min_people} max_people={task.max_people}
class="mt-3 ml-0.5" />
</div>
26 changes: 23 additions & 3 deletions src/lib/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,35 @@ export function getConstraintsFor(item: ConstraintOperand): Constraint[] {
return get(constraints).filter((c) => appliesTo(c, item));
}

export function getSkill(uuid: string | undefined) {
export function getSkill(uuid: string | undefined): Skill | undefined {
if (uuid === undefined) return undefined;
return get(skills).find((s) => s.uuid === uuid);
}

export function getPerson(uuid: string | undefined): Person | undefined {
if (uuid === undefined) return undefined;
return get(employees).find((p) => p.uuid === uuid);
}

export function getTask(uuid: string | undefined): Task | undefined {
if (uuid === undefined) return undefined;
return get(tasks).find((t) => t.uuid === uuid);
}

export function getLocation(uuid: string | undefined): Location | undefined {
if (uuid === undefined) return undefined;
return get(locations).find((l) => l.uuid === uuid);
}

export function getShift(uuid: string | undefined): Shift | undefined {
if (uuid === undefined) return undefined;
return get(shifts).find((s) => s.uuid === uuid);
}

export function deleteEmployee(employee: Person) {
tasks.update((list) =>
list.map((t) => {
t.people = t.people.filter((p) => p.uuid !== employee.uuid);
t.people_uuids = t.people_uuids.filter((p) => p !== employee.uuid);
return t;
})
);
Expand All @@ -44,7 +64,7 @@ export function deleteSkill(skill: Skill) {
);
tasks.update((list) =>
list.map((t) => {
t.required_skills = t.required_skills.filter((s) => s.uuid !== skill.uuid);
t.required_skill_uuids = t.required_skill_uuids.filter((s) => s !== skill.uuid);
return t;
})
);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ export function generateTask(): Task {
icon: generateIcon(),
min_people: faker.number.int({ min: 1, max: 3 }),
max_people: faker.number.int({ min: 3, max: 5 }),
required_skills: sampleSkills(faker.number.int({ min: 0, max: 3 })),
people: samplePeople(faker.number.int({ min: 0, max: 3 }))
required_skill_uuids: sampleSkills(faker.number.int({ min: 0, max: 3 })).map((s) => s.uuid),
people_uuids: samplePeople(faker.number.int({ min: 0, max: 3 })).map((p) => p.uuid)
});
}

Expand Down Expand Up @@ -209,7 +209,7 @@ export function generateShift(): Shift {
start_date_time: faker.date.recent(),
end_date_time: faker.date.soon(),
location: loc,
tasks: sampleTasks(faker.number.int({ min: 1, max: 3 }))
task_uuids: sampleTasks(faker.number.int({ min: 1, max: 3 })).map((t) => t.uuid)
});
}

Expand Down
11 changes: 8 additions & 3 deletions src/lib/types/shift.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { Task } from "$lib/types/task.ts";
import { getAssignedPeopleForTask, type Task } from "$lib/types/task.ts";
import type { Person } from "$lib/types/person.ts";
import type { Location } from "$lib/types/location.ts";
import { v4 as uuidv4 } from "uuid";
import { Type } from "$lib/types/index.ts";
import { getTask } from "$lib/stores.ts";

export interface ShiftProps {
name: string;
description: string;
start_date_time: Date;
end_date_time: Date;
location: Location;
tasks: Task[];
task_uuids: string[];
}

export interface Shift extends ShiftProps {
Expand All @@ -26,6 +27,10 @@ export function createShift(props: ShiftProps): Shift {
};
}

export function getTasksForShift(shift: Shift): Task[] {
return shift.task_uuids.map((uuid) => getTask(uuid)).filter((t) => t !== undefined);
}

export function getPeopleForShift(shift: Shift): Person[] {
return [...new Set(shift.tasks.flatMap((t) => t.people))];
return [...new Set(getTasksForShift(shift).flatMap((t) => getAssignedPeopleForTask(t)))];
}
2 changes: 1 addition & 1 deletion src/lib/types/skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function getPeopleWithSkill(skill: Skill): Person[] {

export function getTasksWithSkill(skill: Skill): Task[] {
const tasks_list = get(tasks);
return tasks_list.filter((t) => t.required_skills.map((s: Skill) => s.uuid).includes(skill.uuid));
return tasks_list.filter((t) => t.required_skill_uuids.includes(skill.uuid));
}

export function defaultSkillProps(): SkillProps {
Expand Down
20 changes: 13 additions & 7 deletions src/lib/types/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IconType } from "$lib/types/ui.ts";
import { get } from "svelte/store";
import type { Skill } from "$lib/types/skill.ts";
import type { Person } from "$lib/types/person.ts";
import { employees } from "$lib/stores";
import { employees, getPerson, getSkill } from "$lib/stores";
import { v4 as uuidv4 } from "uuid";
import { Type } from "$lib/types/index.ts";

Expand All @@ -12,8 +12,8 @@ export interface TaskProps {
icon: IconType;
min_people: number;
max_people: number;
required_skills: Skill[];
people: Person[];
required_skill_uuids: string[];
people_uuids: string[];
}

export interface Task extends TaskProps {
Expand All @@ -29,10 +29,16 @@ export function createTask(props: TaskProps): Task {
};
}

export function getRequiredSkillsForTask(task: Task): Skill[] {
return task.required_skill_uuids.map((uuid) => getSkill(uuid)).filter((s) => s !== undefined);
}

export function getAssignedPeopleForTask(task: Task): Person[] {
return task.people_uuids.map((uuid) => getPerson(uuid)).filter((p) => p !== undefined);
}

export function getCandidatesForTask(task: Task): Person[] {
const employees_list = get(employees);
const required_skills = new Set(task.required_skills.map((s) => s.uuid));
return employees_list.filter((p) =>
required_skills.isSubsetOf(new Set(p.skills.map((s) => s.uuid)))
);
const required_skills = new Set(task.required_skill_uuids);
return employees_list.filter((p) => required_skills.isSubsetOf(new Set(p.skill_uuids)));
}
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import type { Shift } from "$lib/types";
import { shifts } from "$lib/stores.ts";
import MiniSearch from "minisearch";
import { getPeopleForShift } from "$lib/types/shift.ts";
import { getPeopleForShift, getTasksForShift } from "$lib/types/shift.ts";
let schedules: ComboboxItem[] = [
{ label: "Schedule 1", value: "schedule1" },
Expand All @@ -33,7 +33,7 @@
case "location":
return shift.location.name;
case "tasks":
return shift.tasks.map((task) => task.name).join(" ");
return getTasksForShift(shift).map((task) => task.name).join(" ");
case "people":
return getPeopleForShift(shift).map((person) => person.name).join(" ");
case "start_time":
Expand Down

0 comments on commit a397553

Please sign in to comment.