-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
RelatedSources
Core Data component (#286)
* add relatedworks component * export RelatedWorks * refactor into RelatedSources * fix bad import path * add RelatedItems back in
- Loading branch information
1 parent
a26e09b
commit 4b81f04
Showing
13 changed files
with
297 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// @flow | ||
|
||
import React from 'react'; | ||
import _ from 'underscore'; | ||
import type { Source as SourceType } from '../types/Source'; | ||
import LoadAnimation from './LoadAnimation'; | ||
import { useLoader } from '../hooks/CoreData'; | ||
|
||
type Props = { | ||
/** | ||
* Name of the class(es) to apply to the `ul` element. | ||
*/ | ||
className?: string, | ||
|
||
/** | ||
* Callback fired when a source in the list is clicked. | ||
*/ | ||
onClick: (source: SourceType) => void, | ||
|
||
/** | ||
* Callback fired on mount to load the list of items. | ||
*/ | ||
onLoad: () => Promise<any>, | ||
|
||
/** | ||
* Function used to render the description element. | ||
*/ | ||
renderDescription?: (source: SourceType) => JSX.Element, | ||
|
||
/** | ||
* Function used to render the header element. | ||
*/ | ||
renderHeader?: (source: SourceType) => JSX.Element, | ||
|
||
/** | ||
* Function used to render the image element. | ||
*/ | ||
renderImage?: (source: SourceType) => JSX.Element, | ||
|
||
/** | ||
* Type of the source being fetched. | ||
*/ | ||
sourceType: 'instances' | 'items' | 'works' | ||
}; | ||
|
||
/** | ||
* This component render a list of related items. | ||
*/ | ||
const RelatedSources = (props: Props) => { | ||
const { data = {}, loading } = useLoader(props.onLoad, []); | ||
|
||
if (loading) { | ||
return ( | ||
<LoadAnimation /> | ||
); | ||
} | ||
|
||
return ( | ||
<ul | ||
className={props.className} | ||
> | ||
{ _.map(data[props.sourceType], (item) => ( | ||
<li> | ||
<div | ||
className='min-h-[5.5em] flex flex-col justify-start' | ||
> | ||
<button | ||
className='my-0.5 flex-grow text-left inline-flex rounded-none bg-gray-100' | ||
onClick={() => props.onClick(item)} | ||
type='button' | ||
> | ||
{ props.renderImage && ( | ||
<div | ||
className='w-[160px] h-[120px]' | ||
> | ||
{ props.renderImage(item) } | ||
</div> | ||
)} | ||
<div | ||
className='py-3 px-5 flex-grow' | ||
> | ||
{ props.renderHeader && ( | ||
<div | ||
className='py-0.5 font-semibold uppercase text-sm' | ||
> | ||
{ props.renderHeader(item) } | ||
</div> | ||
)} | ||
<h2 | ||
className='py-0.5 font-semibold text-lg' | ||
> | ||
{ item.primary_name?.name?.name } | ||
</h2> | ||
{ props.renderDescription && ( | ||
<div | ||
className='py-0.5 font-light text-sm' | ||
> | ||
{ props.renderDescription(item) } | ||
</div> | ||
)} | ||
</div> | ||
</button> | ||
</div> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
|
||
export default RelatedSources; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @flow | ||
|
||
import type { SourceTitle } from './SourceTitle'; | ||
|
||
export type Instance = { | ||
uuid: string, | ||
primary_name: SourceTitle, | ||
source_titles: Array<SourceTitle> | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// @flow | ||
|
||
import type { Instance } from './Instance'; | ||
import type { Item } from './Item'; | ||
import type { Work } from './Work'; | ||
|
||
export type Source = Instance | Item | Work; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @flow | ||
|
||
import type { SourceTitle } from './SourceTitle'; | ||
|
||
export type Work = { | ||
uuid: string, | ||
primary_name: SourceTitle, | ||
source_titles: Array<SourceTitle> | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @flow | ||
|
||
import { faker } from '@faker-js/faker'; | ||
import Base from './Base'; | ||
|
||
class Instances extends Base { | ||
/** | ||
* Returns a single instance. | ||
*/ | ||
buildItem() { | ||
return { | ||
uuid: faker.string.uuid(), | ||
primary_name: { | ||
name: { | ||
name: faker.lorem.words({ min: 1, max: 3 }) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Returns the instances parameter name. | ||
* | ||
* @returns {string} | ||
*/ | ||
getIndexAttribute() { | ||
return 'instances'; | ||
} | ||
|
||
getShowAttribute() { | ||
return 'instance'; | ||
} | ||
} | ||
|
||
export default new Instances(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @flow | ||
|
||
import { faker } from '@faker-js/faker'; | ||
import Base from './Base'; | ||
|
||
class Works extends Base { | ||
/** | ||
* Returns a single work. | ||
*/ | ||
buildItem() { | ||
return { | ||
uuid: faker.string.uuid(), | ||
primary_name: { | ||
name: { | ||
name: faker.lorem.words({ min: 1, max: 3 }) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Returns the works parameter name. | ||
* | ||
* @returns {string} | ||
*/ | ||
getIndexAttribute() { | ||
return 'works'; | ||
} | ||
|
||
getShowAttribute() { | ||
return 'work'; | ||
} | ||
} | ||
|
||
export default new Works(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
packages/storybook/src/core-data/RelatedSources.stories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// @flow | ||
|
||
import { action } from '@storybook/addon-actions'; | ||
import React from 'react'; | ||
import RelatedSources from '../../../core-data/src/components/RelatedSources'; | ||
import { useEventsService } from '../../../core-data/src/hooks/CoreData'; | ||
import withCoreDataContextProvider from '../hooks/CoreDataContextProvider'; | ||
|
||
export default { | ||
title: 'Components/Core Data/RelatedSources', | ||
component: RelatedSources | ||
}; | ||
|
||
export const Default = withCoreDataContextProvider(() => { | ||
const EventsService = useEventsService(); | ||
|
||
return ( | ||
<RelatedSources | ||
onClick={action('click')} | ||
onLoad={(params) => EventsService.fetchRelatedItems('1', params)} | ||
sourceType='items' | ||
/> | ||
); | ||
}); | ||
|
||
export const Instances = withCoreDataContextProvider(() => { | ||
const EventsService = useEventsService(); | ||
|
||
return ( | ||
<RelatedSources | ||
onClick={action('click')} | ||
onLoad={(params) => EventsService.fetchRelatedInstances('1', params)} | ||
sourceType='instances' | ||
/> | ||
); | ||
}); | ||
|
||
export const Works = withCoreDataContextProvider(() => { | ||
const EventsService = useEventsService(); | ||
|
||
return ( | ||
<RelatedSources | ||
onClick={action('click')} | ||
onLoad={(params) => EventsService.fetchRelatedWorks('1', params)} | ||
sourceType='works' | ||
/> | ||
); | ||
}); |