Skip to content

Commit

Permalink
Add puzzle tag selector and merge puzzle + hunt tags. (#755)
Browse files Browse the repository at this point in the history
* Add puzzle tag selector and merge them.

* Dedupe by id.
  • Loading branch information
kcaze authored and akirabaruah committed Jan 12, 2024
1 parent d5fa059 commit eb8e372
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions hunts/src/EditPuzzleTagsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import { useDispatch, useSelector } from "react-redux";
import Modal from "react-bootstrap/Modal";
import Button from "react-bootstrap/Button";
import Form from "react-bootstrap/Form";
import { addPuzzleTag } from "./puzzlesSlice";
import { addPuzzleTag, selectPuzzleTags } from "./puzzlesSlice";
import { selectHuntTags } from "./huntSlice";
import { DEFAULT_TAG_COLOR, SELECTABLE_TAG_COLORS } from "./constants";
import { hideModal } from "./modalSlice";
import TagPill from "./TagPill";
import EditableTagList from "./EditableTagList";

function EditPuzzleTagsModal({ puzzleId, puzzleName }) {
const allTags = useSelector(selectHuntTags);
const huntTags = useSelector(selectHuntTags);
const puzzleTags = useSelector(selectPuzzleTags);
// Use a map to concat huntTags and puzzleTags and then dedupe by id.
const tagMap = new Map();
for (const tag of huntTags.concat(puzzleTags)) {
tagMap.set(tag.id, tag);
}
const allTags = Array.from(tagMap.values());

const [newTagName, setNewTagName] = React.useState("");
const [newTagColor, setNewTagColor] = React.useState(DEFAULT_TAG_COLOR);
const dispatch = useDispatch();
Expand Down
14 changes: 14 additions & 0 deletions hunts/src/puzzlesSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,20 @@ export const selectNumMetasUnsolved = createSelector(
}
);

export const selectPuzzleTags = createSelector(
[puzzlesSelectors.selectAll],
(puzzles) => {
// Use a map to dedupe puzzle tags by id.
const tagMap = new Map();
for (const puzzle of puzzles) {
for (const tag of puzzle.tags) {
tagMap.set(tag.id, tag);
}
}
return Array.from(tagMap.values());
}
);

export const { selectById: selectPuzzleById } = puzzlesSelectors;
export const { reducers } = puzzlesSlice.actions;

Expand Down

0 comments on commit eb8e372

Please sign in to comment.