Skip to content

Commit

Permalink
Merge pull request #101 from NYPL-Simplified/header-library-links
Browse files Browse the repository at this point in the history
Made library-related header links update when library changes.
  • Loading branch information
aslagle committed Jul 12, 2017
2 parents 968fadf + fffd47f commit afe7581
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/components/CatalogHandler.tsx
Expand Up @@ -22,13 +22,13 @@ export interface CatalogHandlerProps extends React.Props<CatalogHandler> {
export default class CatalogHandler extends React.Component<CatalogHandlerProps, any> {
static childContextTypes: React.ValidationMap<any> = {
tab: React.PropTypes.string,
library: React.PropTypes.string
library: React.PropTypes.func
};

getChildContext() {
return {
tab: this.props.params.tab,
library: this.getLibrary(this.props.params.collectionUrl, this.props.params.bookUrl)
library: () => this.getLibrary(this.props.params.collectionUrl, this.props.params.bookUrl)
};
}

Expand Down
13 changes: 7 additions & 6 deletions src/components/Header.tsx
Expand Up @@ -17,10 +17,10 @@ export interface HeaderProps extends React.Props<Header> {
}

export class Header extends React.Component<HeaderProps, any> {
context: { library: string, router: Router };
context: { library: () => string, router: Router };

static contextTypes = {
library: React.PropTypes.string,
library: React.PropTypes.func,
router: React.PropTypes.object.isRequired
};

Expand All @@ -40,7 +40,7 @@ export class Header extends React.Component<HeaderProps, any> {
<EditableInput
elementType="select"
ref="library"
value={this.context.library}
value={this.context.library && this.context.library()}
onChange={this.changeLibrary}
>
{ !this.context.library &&
Expand All @@ -60,21 +60,21 @@ export class Header extends React.Component<HeaderProps, any> {
<Nav>
<li>
<CatalogLink
collectionUrl={"/" + this.context.library + "/groups"}
collectionUrl={"/" + this.context.library() + "/groups"}
bookUrl={null}>
Catalog
</CatalogLink>
</li>
<li>
<CatalogLink
collectionUrl={"/" + this.context.library + "/admin/complaints"}
collectionUrl={"/" + this.context.library() + "/admin/complaints"}
bookUrl={null}>
Complaints
</CatalogLink>
</li>
<li>
<CatalogLink
collectionUrl={"/" + this.context.library + "/admin/suppressed"}
collectionUrl={"/" + this.context.library() + "/admin/suppressed"}
bookUrl={null}>
Hidden Books
</CatalogLink>
Expand Down Expand Up @@ -104,6 +104,7 @@ export class Header extends React.Component<HeaderProps, any> {
let library = (this.refs["library"] as any).getValue();
if (library) {
this.context.router.push("/admin/web/collection/" + library + "%2Fgroups");
this.forceUpdate();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/__tests__/CatalogHandler-test.tsx
Expand Up @@ -58,16 +58,16 @@ describe("CatalogHandler", () => {

it("includes library in child context", () => {
let context = wrapper.instance().getChildContext();
expect(context.library).to.equal("library");
expect(context.library()).to.equal("library");

let newParams = Object.assign({}, params, { collectionUrl: null, bookUrl: "library/bookurl" });
wrapper.setProps({ params: newParams });
context = wrapper.instance().getChildContext();
expect(context.library).to.equal("library");
expect(context.library()).to.equal("library");

newParams = Object.assign({}, params, { collectionUrl: null, bookUrl: null });
wrapper.setProps({ params: newParams });
context = wrapper.instance().getChildContext();
expect(context.library).to.equal(null);
expect(context.library()).to.equal(null);
});
});
4 changes: 2 additions & 2 deletions src/components/__tests__/Header-test.tsx
Expand Up @@ -15,7 +15,7 @@ describe("Header", () => {

beforeEach(() => {
push = stub();
context = { library: "nypl" };
context = { library: () => "nypl" };

wrapper = shallow(
<Header />,
Expand Down Expand Up @@ -48,7 +48,7 @@ describe("Header", () => {
expect(options.at(1).text()).to.equal("bpl");
expect(options.at(1).props().value).to.equal("bpl");

wrapper.setContext({ library: "bpl" });
wrapper.setContext({ library: () => "bpl" });
select = wrapper.find(EditableInput);
expect(select.props().value).to.equal("bpl");

Expand Down

0 comments on commit afe7581

Please sign in to comment.