Skip to content

Commit

Permalink
Disable selected subjects in book edit modal
Browse files Browse the repository at this point in the history
  • Loading branch information
arackaf committed Dec 7, 2022
1 parent a3678e3 commit 4153bef
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 47 deletions.
14 changes: 7 additions & 7 deletions svelte-kit/package-lock.json

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

2 changes: 1 addition & 1 deletion svelte-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"classnames": "^2.3.2",
"mongodb": "^4.12.1",
"sass": "^1.56.1",
"svelte-helpers": "^0.2.12"
"svelte-helpers": "^0.2.13"
}
}
6 changes: 5 additions & 1 deletion svelte-kit/src/data/subjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export const allSubjects = async () => {

const result: Subject[] = (await db
.collection("subjects")
.aggregate([{ $match: { userId: "60a93babcc3928454b5d1cc6" } }, { $project: { _id: 1, name: 1, path: 1 } }, { $sort: { name: 1 } }])
.aggregate([
{ $match: { userId: "60a93babcc3928454b5d1cc6" } },
{ $project: { _id: 1, name: 1, path: 1, textColor: 1, backgroundColor: 1 } },
{ $sort: { name: 1 } }
])
.toArray()) as Subject[];

const nativeEnd = +new Date();
Expand Down
4 changes: 2 additions & 2 deletions svelte-kit/src/data/tags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { env } from "$env/dynamic/private";
import type { Tag, TagHash } from "./types";
import type { Hash, Tag } from "./types";

export const allTags = async () => {
const httpStart = +new Date();
Expand All @@ -23,7 +23,7 @@ export const allTags = async () => {
console.log("HTTP tags time", httpEnd - httpStart);

const tags = res.documents as Tag[];
const tagHash = tags.reduce<TagHash>((hash, tag) => {
const tagHash = tags.reduce<Hash<Tag>>((hash, tag) => {
hash[tag._id] = tag;
return hash;
}, {});
Expand Down
10 changes: 4 additions & 6 deletions svelte-kit/src/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export type Subject = {
_id: string;
name: string;
textColor: string;
backgroundColor: string;
path: string;
};

Expand All @@ -13,15 +15,11 @@ export type FullSubject = Subject & {
childLevel: number;
};

export type SubjectHash = {
[_id: string]: FullSubject;
export type Hash<T> = {
[_id: string]: T;
};

export type Tag = {
_id: string;
name: string;
};

export type TagHash = {
[_id: string]: Tag;
};
6 changes: 3 additions & 3 deletions svelte-kit/src/lib/components/editBook/EditBookInfo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import SelectAvailableSubjects from "$lib/components/subjectsAndTags/subjects/SelectAvailableSubjects.svelte";
import DisplaySelectedTags from "$lib/components/subjectsAndTags/tags/DisplaySelectedTags.svelte";
//import DisplaySelectedSubjects from "app/components/subjectsAndTags/subjects/DisplaySelectedSubjects.svelte";
import DisplaySelectedSubjects from "$lib/components/subjectsAndTags/subjects/DisplaySelectedSubjects.svelte";
import FlexRow from "../layout/FlexRow.svelte";
import FlowItems from "../layout/FlowItems.svelte";
Expand All @@ -25,7 +25,7 @@
editingBook = { ...book };
}
const addSubject = (subject: any) => (editingBook.subjects = editingBook.subjects.concat(subject._id));
const addSubject = (subject: Subject) => (editingBook.subjects = editingBook.subjects.concat(subject._id));
const removeSubject = (subject: any) => (editingBook.subjects = editingBook.subjects.filter(_id => _id != subject._id));
const addTag = (tag: any) => (editingBook.tags = editingBook.tags.concat(tag._id));
Expand Down Expand Up @@ -122,7 +122,7 @@
<SelectAvailableSubjects {subjects} currentlySelected={editingBook.subjects} onSelect={addSubject} />
</div>
<div style="display: {editingBook.subjects.length ? '' : 'none'}" class="col-sm-9 col-xs-12">
<!-- <DisplaySelectedSubjects currentlySelected={editingBook.subjects} onRemove={removeSubject} /> -->
<DisplaySelectedSubjects {subjects} currentlySelected={editingBook.subjects} onRemove={removeSubject} />
</div>
</FlexRow>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<script lang="ts">
import type { Subject } from "$data/types";
import RemovableLabelDisplay from "../RemovableLabelDisplay.svelte";
import LabelDisplay from "../LabelDisplay.svelte";
import { subjectsState } from "app/state/subjectsState";
import FlowItems from "../../layout/FlowItems.svelte";
import { toHash } from "$lib/state/helpers";
export let currentlySelected: any[];
export let onRemove: any;
$: ({ subjectHash } = $subjectsState);
export let subjects: Subject[];
$: subjectHash = toHash(subjects);
</script>

<FlowItems tightest={true}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<script lang="ts">
import type { Subject } from "$data/types";
import type { DisablableSubject, Subject } from "$data/types";
import type { Label } from "../types";
import { filterSubjects, subjectState } from "$lib/state/subjectsState";
import GenericLabelSelect from "../GenericLabelSelect.svelte";
export let onSelect: (item: Label) => void;
export let onSelect: (item: DisablableSubject) => void;
export let placeholder = "Subjects";
export let currentlySelected: string[] = [];
export let subjects: Subject[];
let search = "";
const doSelect = (item: Label) => {
const doSelect = (item: DisablableSubject) => {
if (item.disabled) {
return false;
}
onSelect(item);
search = "";
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
import type { Tag } from "$data/types";
import { toHash } from "$lib/state/helpers";
import RemovableLabelDisplay from "../RemovableLabelDisplay.svelte";
import LabelDisplay from "../LabelDisplay.svelte";
import FlowItems from "../../layout/FlowItems.svelte";
import type { Tag } from "$data/types";
import { toHash } from "$lib/state/tagsState";
export let tags: Tag[];
export let onRemove: ((tag: Tag) => void) | null = null;
Expand Down
12 changes: 12 additions & 0 deletions svelte-kit/src/lib/state/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Hash } from "$data/types";

type WithId = {
_id: string;
};

export const toHash = <T extends WithId>(items: T[]): Hash<T> => {
return items.reduce<Hash<T>>((hash, tag) => {
hash[tag._id] = tag;
return hash;
}, {});
};
12 changes: 3 additions & 9 deletions svelte-kit/src/lib/state/subjectsState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DisablableSubject, FullSubject, Subject, SubjectHash } from "$data/types";
import type { DisablableSubject, FullSubject, Hash, Subject } from "$data/types";
import { toHash } from "./helpers";

export const subjectState = (allSubjectsSorted: Subject[] = []) => {
const subjects = stackAndGetTopLevelSubjects(allSubjectsSorted);
Expand Down Expand Up @@ -33,17 +34,10 @@ export const unwindSubjects = (subjects: FullSubject[]): FullSubject[] => {
return result;
};

export const toHash = (subjects: FullSubject[]): SubjectHash => {
return subjects.reduce<SubjectHash>((hash, tag) => {
hash[tag._id] = tag;
return hash;
}, {});
};

type LookupHash = { [_id: string]: true };
type SearchFn = (s: Subject) => boolean;

export const filterSubjects = (subjects: Subject[], search?: string, lookupMap: SubjectHash = {}, alreadySelected: LookupHash = {}) => {
export const filterSubjects = (subjects: Subject[], search?: string, lookupMap: Hash<FullSubject> = {}, alreadySelected: LookupHash = {}) => {
let searchFn: SearchFn;
if (!search) {
searchFn = s => !alreadySelected[s._id];
Expand Down
9 changes: 1 addition & 8 deletions svelte-kit/src/lib/state/tagsState.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import type { Tag, TagHash } from "$data/types";

export const toHash = (tags: Tag[]): TagHash => {
return tags.reduce<TagHash>((hash, tag) => {
hash[tag._id] = tag;
return hash;
}, {});
};
import type { Tag } from "$data/types";

export const filterTags = (tags: Tag[], search: string) => {
let filterFn: (txt: string) => boolean;
Expand Down
4 changes: 2 additions & 2 deletions svelte-kit/src/routes/(logged-in)/books/SearchModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// import SelectAvailableSubjects from "app/components/subjectsAndTags/subjects/SelectAvailableSubjects.svelte";
// import DisplaySelectedTags from "app/components/subjectsAndTags/tags/DisplaySelectedTags.svelte";
import SelectAvailableTags from "$lib/components/subjectsAndTags/tags/SelectAvailableTags.svelte";
import type { Tag, TagHash } from "$data/types";
import type { Hash, Tag } from "$data/types";
// import { currentSearch } from "./booksSearchState";
// import { applyFilters } from "./setBookFilters";
Expand All @@ -24,7 +24,7 @@
export let onHide = () => {};
export let allTags: Tag[];
export let tagHash: TagHash[];
export let tagHash: Hash<Tag>;
let closeModal: any;
Expand Down

1 comment on commit 4153bef

@vercel
Copy link

@vercel vercel bot commented on 4153bef Dec 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.