|
@@ -1,187 +0,0 @@
|
|
|
-function createStore(reducer) {
|
|
|
- let state = reducer(undefined, {})
|
|
|
- let cbs = []
|
|
|
-
|
|
|
- const getState = () => state
|
|
|
- const subscribe = cb => (cbs.push(cb),
|
|
|
- () => cbs = cbs.filter(c => c !== cb))
|
|
|
-
|
|
|
- const dispatch = action => {
|
|
|
- if (typeof action === 'function') {
|
|
|
- return action(dispatch, getState)
|
|
|
- }
|
|
|
- const newState = reducer(state, action)
|
|
|
- if (newState !== state) {
|
|
|
- state = newState
|
|
|
- for (let cb of cbs) cb(state)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- getState,
|
|
|
- dispatch,
|
|
|
- subscribe
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-function combineReducers(reducers) {
|
|
|
- function totalReducer(state = {}, action) {
|
|
|
- const newTotalState = {}
|
|
|
- for (const [reducerName, reducer] of Object.entries(reducers)) {
|
|
|
- const newSubState = reducer(state[reducerName], action)
|
|
|
- if (newSubState !== state[reducerName]) {
|
|
|
- newTotalState[reducerName] = newSubState
|
|
|
- }
|
|
|
- }
|
|
|
- if (Object.keys(newTotalState).length) {
|
|
|
- return { ...state, ...newTotalState }
|
|
|
- }
|
|
|
- return state
|
|
|
- }
|
|
|
-
|
|
|
- return totalReducer
|
|
|
-}
|
|
|
-
|
|
|
-const reducers = {
|
|
|
- promise: promiseReducer,
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-const totalReducer = combineReducers(reducers)
|
|
|
-
|
|
|
-
|
|
|
-function promiseReducer(state = {}, { type, status, payload, error }) {
|
|
|
- if (type === 'PROMISE') {
|
|
|
-
|
|
|
- return { status, payload, error }
|
|
|
- }
|
|
|
- return state
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-const actionPending = () => ({ type: 'PROMISE', status: 'PENDING' })
|
|
|
-const actionFulfilled = payload => ({ type: 'PROMISE', status: 'FULFILLED', payload })
|
|
|
-const actionRejected = error => ({ type: 'PROMISE', status: 'REJECTED', error })
|
|
|
-
|
|
|
-
|
|
|
-const actionPromise = promise =>
|
|
|
- async dispatch => {
|
|
|
- dispatch(actionPending())
|
|
|
- try {
|
|
|
- const payload = await promise
|
|
|
- dispatch(actionFulfilled(payload))
|
|
|
- return payload
|
|
|
- }
|
|
|
- catch (error) {
|
|
|
- dispatch(actionRejected(error))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-const store = createStore(totalReducer)
|
|
|
-store.subscribe(() => console.log(store.getState()))
|
|
|
-
|
|
|
-const drawPeople = (state) => {
|
|
|
- const [, route] = location.hash.split('/')
|
|
|
- if (route !== 'people') return
|
|
|
-
|
|
|
- const { status, payload, error } = store.getState().promise
|
|
|
- if (status === 'PENDING') {
|
|
|
- main.innerHTML = `<img src='https://cdn.dribbble.com/users/63485/screenshots/1309731/infinite-gif-preloader.gif' />`
|
|
|
- }
|
|
|
- if (status === 'FULFILLED') {
|
|
|
- const { name, mass, eye_color, films } = payload
|
|
|
- main.innerHTML = `<h1>${name}</h1>
|
|
|
- <section>ЖЫРНОСТЬ: ${mass}кг</section>
|
|
|
- <section style="color: ${eye_color}">Цвет глаз</section>
|
|
|
- `
|
|
|
- for (const filmUrl of films) {
|
|
|
- const filmId = filmUrl.split('/films/')[1].slice(0, -1)
|
|
|
- main.innerHTML += `<a href="#/films/${filmId}">Фильм №${filmId}</a>`
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-store.subscribe(drawPeople)
|
|
|
-
|
|
|
-store.subscribe(() => {
|
|
|
- const [, route] = location.hash.split('/')
|
|
|
- if (route !== 'films') return
|
|
|
-
|
|
|
- const { status, payload, error } = store.getState().promise
|
|
|
- if (status === 'PENDING') {
|
|
|
- main.innerHTML = `<img src='https://cdn.dribbble.com/users/63485/screenshots/1309731/infinite-gif-preloader.gif' />`
|
|
|
- }
|
|
|
- if (status === 'FULFILLED') {
|
|
|
- const { title, opening_crawl, characters } = payload
|
|
|
- main.innerHTML = `<h1>${title}</h1>
|
|
|
- <p>${opening_crawl}</p>
|
|
|
- `
|
|
|
- for (const peopleUrl of characters) {
|
|
|
- const peopleId = peopleUrl.split('/people/')[1].slice(0, -1)
|
|
|
- main.innerHTML += `<a href="#/people/${peopleId}">Герой №${peopleId}</a>`
|
|
|
- }
|
|
|
- }
|
|
|
-})
|
|
|
-
|
|
|
-const actionGetPeople = id =>
|
|
|
- actionPromise(fetch(`https://swapi.dev/api/people/${id}`).then(res => res.json()))
|
|
|
-
|
|
|
-const actionGetFilm = id =>
|
|
|
- actionPromise(fetch(`https://swapi.dev/api/films/${id}`).then(res => res.json()))
|
|
|
-
|
|
|
-const actionSomePeople = () =>
|
|
|
- actionPromise(fetch(`https://swapi.dev/api/people/`).then(res => res.json()))
|
|
|
-
|
|
|
-store.dispatch(actionSomePeople())
|
|
|
-
|
|
|
-store.subscribe(() => {
|
|
|
- const { status, payload, error } = store.getState().promise
|
|
|
- if (status === 'FULFILLED' && payload.results) {
|
|
|
- aside.innerHTML = ''
|
|
|
- for (const { url: peopleUrl, name } of payload.results) {
|
|
|
- const peopleId = peopleUrl.split('/people/')[1].slice(0, -1)
|
|
|
- aside.innerHTML += `<a href="#/people/${peopleId}">${name}</a>`
|
|
|
- }
|
|
|
- }
|
|
|
-})
|
|
|
-
|
|
|
-window.onhashchange = () => {
|
|
|
- const [, route, _id] = location.hash.split('/')
|
|
|
-
|
|
|
- const routes = {
|
|
|
- people() {
|
|
|
- console.log('People', _id)
|
|
|
- store.dispatch(actionGetPeople(_id))
|
|
|
- },
|
|
|
- films() {
|
|
|
- store.dispatch(actionGetFilm(_id))
|
|
|
- },
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- login() {
|
|
|
- console.log('А ТУТ ЩА ДОЛЖНА БЫТЬ ФОРМА ЛОГИНА')
|
|
|
-
|
|
|
- },
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if (route in routes) {
|
|
|
- routes[route]()
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-window.onhashchange()
|