Skip to content

Commit 168a199

Browse files
committed
selectedBook can be null
1 parent 1d16729 commit 168a199

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

src/app/pages/errata-form/errata-form-context.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {useLocation} from 'react-router-dom';
66
import {useDataFromPromise, useDataFromSlug} from '~/helpers/page-data-utils';
77

88
type ContextValue = {
9-
selectedBook: Book;
9+
selectedBook: Book | null;
1010
books: Book[] | null | undefined;
1111
hasError: string | null;
1212
setHasError: (error: string | null) => void;
@@ -30,10 +30,10 @@ function useContextValue(): ContextValue {
3030
const [title, setTitle] = useState<string | null>(initialTitle);
3131
const books = useDataFromPromise(fetchBooks);
3232
const selectedBook = React.useMemo(
33-
() => books?.find((b: Book) => b.title === title) || {} as Book,
33+
() => books?.find((b) => b.title === title) || null,
3434
[books, title]
3535
);
36-
const bookInfo = useDataFromSlug<Book>(selectedBook.slug) || selectedBook;
36+
const bookInfo = useDataFromSlug<Book>(selectedBook ? selectedBook.slug : null) || selectedBook;
3737
const [hasError, setHasError] = useState<string | null>('You have not completed the form');
3838
const [hideErrors, setHideErrors] = useState(true);
3939
const [submitting, setSubmitting] = useState(false);

src/app/pages/errata-form/form/ErrorLocationSelector/toc-selector.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function ChapterOrPageOption(passThruOptions: ChapterOptionProps) {
104104
);
105105
}
106106

107-
function useTocTree(slug: string, chapterFilter: string | undefined) {
107+
function useTocTree(slug: string | undefined, chapterFilter: string | undefined) {
108108
const [tree, updateTree] = useState<TreeEntry[]>([]);
109109
const filteredTree = useMemo(
110110
() => {
@@ -138,7 +138,8 @@ export default function TocSelector({required=true, updateValue}: TocSelectorPro
138138
const inputRef = useRef<HTMLSelectElement>(null);
139139
const [InvalidMessage, updateInvalidMessage] = managedInvalidMessage(inputRef);
140140
const [chapterFilter, updateChapterFilter] = useState<string>();
141-
const filteredTree = useTocTree(selectedBook.slug, chapterFilter);
141+
const slug = selectedBook ? selectedBook.slug : undefined;
142+
const filteredTree = useTocTree(slug, chapterFilter);
142143
const deselect = React.useCallback(
143144
() => {
144145
if (inputRef.current) {

src/app/pages/errata-form/form/ErrorSourceSelector.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function useSourceTypes() {
101101
const initialSource = source && sourceNames[source.toLowerCase()];
102102
const [sourceTypes, updateSourceTypes] = useState<string[]>([]);
103103
const filteredSourceTypes = useMemo(
104-
() => sourceTypes.filter(filterForBook(selectedBook)),
104+
() => sourceTypes.filter(filterForBook(selectedBook ?? {} as Book)),
105105
[sourceTypes, selectedBook]
106106
);
107107
const [selectedSource, updateSelectedSource] = useState(initialSource);

src/app/pages/errata-form/form/form.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ function RevisionSchedule() {
7979
function FormFields() {
8080
const {selectedBook} = useErrataFormContext();
8181
const {accountId: submittedBy} = useUserContext();
82+
const id = selectedBook ? selectedBook.id : '';
8283

8384
return (
8485
<React.Fragment>
8586
<input type="hidden" name="submitted_by_account_id" value={submittedBy} />
86-
<input type="hidden" name="book" value={selectedBook.id} />
87+
<input type="hidden" name="book" value={id} />
8788
<ErrorTypeSelector />
8889
<ErrorSourceSelector />
8990
<ErrorLocationSelector />

0 commit comments

Comments
 (0)