images_viewer/src/components/BookComponent.tsx

140 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-05-08 17:02:03 +07:00
import React, {createContext} from 'react'
import OnePageView from './OnePageView'
2020-05-09 11:45:34 +07:00
import TwoPagesView from './TwoPagesView'
2020-05-11 10:17:26 +07:00
import BookComponentsController from './BookComponentsController'
import './index.css'
2020-05-08 17:02:03 +07:00
export const BookContext = createContext({})
export interface IBookPage {
src: string
alt?: string
thumbnail?: string
}
2020-05-11 10:17:26 +07:00
export const getBookViewModes = (currIndex = 0, srcLength = 0)=>[
2020-05-08 17:02:03 +07:00
{
text: 'One Page',
value: 'one page',
2020-05-11 10:17:26 +07:00
icon: '',
pagesToTurn: 1,
nextDisable: currIndex >= srcLength - 1,
prevDisable: currIndex === 0,
2020-05-08 17:02:03 +07:00
},
{
text: 'Two Pages',
value: 'two pages',
2020-05-11 10:17:26 +07:00
icon: '',
pagesToTurn: 2,
nextDisable: currIndex >= srcLength - 2,
prevDisable: currIndex <= 1,
2020-05-08 17:02:03 +07:00
}
]
export enum BookViewMode {
onePage = 'one page',
twoPages = 'two pages'
}
export interface IBookContext {
currIndex?: number,
src?: IBookPage[],
flagToReverse?: boolean,
mode?: BookViewMode,
readonly setBookContextState: (newState: any)=> undefined
}
2020-05-11 10:17:26 +07:00
const getPagesCountToTurn = (mode: string)=>{
let pagesToTurn = getBookViewModes().find(item=> item.value === mode)?.pagesToTurn
return pagesToTurn || 0
}
2020-05-09 11:45:34 +07:00
class BookComponent extends React.Component<any, any>{
2020-05-08 17:02:03 +07:00
constructor(props: any){
super(props)
this.state = {
currIndex: 0,
src: [],
flagToReverse: false,
mode: "one page",
setBookContextState: (newState: IBookContext)=>{
2020-05-09 11:45:34 +07:00
this.setState((oldState: any)=> ({
2020-05-08 17:02:03 +07:00
...oldState,
...newState
}))
2020-05-09 11:45:34 +07:00
},
2020-05-11 10:17:26 +07:00
changePage: this.changePage,
pagesToTurn: 1,
2020-05-08 17:02:03 +07:00
}
}
2020-05-11 10:17:26 +07:00
changePage = (goToPrevious: boolean, indexToGo?: number)=>{
const {mode, currIndex} = this.state
const pagesCountToTurn = indexToGo || getPagesCountToTurn(mode)
const prefixPageCount = goToPrevious? -1 : 1
this.setState({
currIndex: (pagesCountToTurn * prefixPageCount) + currIndex,
flagToReverse: goToPrevious
})
}
2020-05-08 17:02:03 +07:00
componentDidMount(){
fetch('https://picsum.photos/v2/list')
.then(response=>{
return response.json()
}).then(data=>{
data = data.map((item: any)=>{
return {
src: item.download_url,
alt: item.author
}
})
this.setState({
src: data
})
})
}
2020-05-11 10:17:26 +07:00
getControlsDisable = ()=>{
const {mode, currIndex, src} = this.state
const {nextDisable, prevDisable} = getBookViewModes(currIndex, src.length).find(item=> item.value === mode) || {
nextDisable: true,
prevDisable: true
}
return {
nextDisable,
prevDisable
}
}
2020-05-08 17:02:03 +07:00
render(){
2020-05-09 11:45:34 +07:00
const {mode} = this.state
2020-05-08 17:02:03 +07:00
return <BookContext.Provider value={{...this.state}}>
<BookContext.Consumer>
{state=>(
<>
2020-05-09 11:45:34 +07:00
{mode === "one page" && <OnePageView {...state}/>}
{mode === "two pages" && <TwoPagesView {...state}/>}
2020-05-11 10:17:26 +07:00
<BookComponentsController
onPrevClick={()=>{
this.changePage(true)
}}
onNextClick={()=>{
this.changePage(false)
}}
{...this.getControlsDisable()}
{...this.state}
/>
2020-05-08 17:02:03 +07:00
</>
)}
</BookContext.Consumer>
</BookContext.Provider>
}
}
export default BookComponent