diff --git a/app/actions/addColumn.js b/app/actions/addColumn.js index 6436e2ab..c68ef497 100644 --- a/app/actions/addColumn.js +++ b/app/actions/addColumn.js @@ -1,6 +1,6 @@ // @flow import type {Dispatch, Action} from '../types' -import Pixiv from '../util/pixiv' +import Pixiv from '../api/pixiv' import type {Params, Endpoint} from '../types/column' import type {User} from '../types/user' import * as endpoint from '../constants/endpoint' diff --git a/app/actions/drawer.js b/app/actions/drawer.js index 9bc779f0..daba36d6 100644 --- a/app/actions/drawer.js +++ b/app/actions/drawer.js @@ -2,7 +2,7 @@ import type {Action, Dispatch, State} from '../types' import type {User, Profile} from '../types/user' import type {DrawerType} from '../types/drawer' -import Pixiv, {normalizeIllusts} from '../util/pixiv' +import Pixiv, {normalizeIllusts} from '../api/pixiv' import {getNextUrl} from '../reducers/drawer' import {apiRequestSuccess} from './api' diff --git a/app/api/index.js b/app/api/index.js index f2f39bc8..68e9624d 100644 --- a/app/api/index.js +++ b/app/api/index.js @@ -1,6 +1,6 @@ // @flow -import Pixiv, {normalizeIllusts, parseUrl} from '../util/pixiv' import type {Endpoint, Params} from '../types/column' +import Pixiv, {normalizeIllusts, parseUrl} from './pixiv' type FetchResponse = { response: Object, diff --git a/app/util/pixiv.js b/app/api/pixiv.js similarity index 100% rename from app/util/pixiv.js rename to app/api/pixiv.js diff --git a/app/util/schema.js b/app/api/schema.js similarity index 100% rename from app/util/schema.js rename to app/api/schema.js diff --git a/app/components/SearchField/SearchFiled.js b/app/components/SearchField/SearchFiled.js index 2e7af491..fcecdcbb 100644 --- a/app/components/SearchField/SearchFiled.js +++ b/app/components/SearchField/SearchFiled.js @@ -2,7 +2,7 @@ import React, {Component} from 'react' import {findDOMNode} from 'react-dom' import throttle from 'lodash.throttle' -import Pixiv from '../../util/pixiv' +import Pixiv from '../../api/pixiv' import PopoverAuto from './PopoverAuto' import UsersOver from './UsersOver' import styles from './SearchField.css' diff --git a/app/index.js b/app/index.js index 2e1dcf29..9e7ba8cc 100644 --- a/app/index.js +++ b/app/index.js @@ -1,3 +1,4 @@ +// @flow import React from 'react' import {render} from 'react-dom' import {Provider} from 'react-redux' @@ -6,23 +7,19 @@ import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider' import type {State} from './types' import configureStore from './store' import App from './components' -import Pixiv from './util/pixiv' +import Api from './api' import './app.global.css'; // eslint-disable-line injectTapEventPlugin() async function init() { - const storage: State = localStorage.getItem('store') - let initialState: State = storage ? JSON.parse(storage) : {} + const storage: ?string = localStorage.getItem('store') + let initialState: $Shape = storage ? JSON.parse(storage) : {} - const {auth, manage, columns} = initialState + const {auth, columns} = initialState - if (auth && manage) { - const {username, password} = auth - const {isLogin} = manage - if (username && password && isLogin) { - await Pixiv.login(username, password) - } + if (auth && auth.username && auth.password) { + await Api.login(auth.username, auth.password) } else if (columns) { initialState = {columns: initialState.columns} } diff --git a/app/sagas/auth.js b/app/sagas/auth.js index 067a3c3d..e30b9d2b 100644 --- a/app/sagas/auth.js +++ b/app/sagas/auth.js @@ -25,11 +25,14 @@ function * authorize(username: string, password: string): Generator<*, *, *> { export function * autoLogin(): Generator<*, *, *> { const {username, password} = yield select((state: State) => state.auth) - if (username && password) { - yield call(Api.login, username, password) - } else { + try { + if (username && password) { + yield call(Api.login, username, password) + return true + } yield put(logout()) - } + return false + } catch (err) { } } function * loginFlow(): Generator<*, *, *> { diff --git a/app/sagas/column.js b/app/sagas/column.js index fd9ce5f5..2b407bf2 100644 --- a/app/sagas/column.js +++ b/app/sagas/column.js @@ -1,6 +1,7 @@ // @flow import {fork, take, call, select, put} from 'redux-saga/effects' import * as Actions from '../constants/column' +import * as endpoint from '../constants/endpoint' import {getColumn} from '../reducers' import type {Endpoint, Params} from '../types/column' import { @@ -23,24 +24,44 @@ export function * refreshAllColumns(): Generator<*, *, *> { } function * add(id: Id, endpoint: Endpoint, inputParams: Params): Generator<*, *, *> { - const {response, params} = yield call(Api.fetch, endpoint, inputParams) - yield put(apiRequestSuccess(response)) - yield put(addColumnIllusts(id, response.result)) - return {response, params} + try { + const {response, params} = yield call(Api.fetch, endpoint, inputParams) + yield put(apiRequestSuccess(response)) + yield put(addColumnIllusts(id, response.result)) + return {response, params} + } catch (err) { + console.error(err) + } } +const checkEndpoint = (target: Endpoint) => target === endpoint.FOLLOW || target === endpoint.FOLLOW + function * checkUpdate(id: Id): Generator<*, *, *> { - yield call(autoLogin) const state = yield select() const column = getColumn(state, id) + + if (checkEndpoint(column.endpoint)) { + const isLogin = yield call(autoLogin) + if (!isLogin) { + return false + } + } + const opts = {...column.params, ...resetParam} yield call(add, id, column.endpoint, opts) } function * fetchColumn(id: Id) { - yield call(autoLogin) const state = yield select() const column = getColumn(state, id) + + if (checkEndpoint(column.endpoint)) { + const isLogin = yield call(autoLogin) + if (!isLogin) { + return false + } + } + const {params} = yield call(add, id, column.endpoint, column.params) if (params) { diff --git a/package.json b/package.json index 2eba87ef..08ffe0d5 100644 --- a/package.json +++ b/package.json @@ -94,6 +94,6 @@ "style-loader": "^0.13.1", "webpack": "^1.14.0", "webpack-dev-server": "^1.16.2", - "webpack-merge": "^1.1.0" + "webpack-merge": "^1.1.1" } } diff --git a/yarn.lock b/yarn.lock index e0b7103f..c7a13fde 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5543,9 +5543,9 @@ webpack-dev-server@^1.16.2: supports-color "^3.1.1" webpack-dev-middleware "^1.4.0" -webpack-merge@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/webpack-merge/-/webpack-merge-1.1.0.tgz#e51d941c75f180219e73cc514cbf709f109ea26f" +webpack-merge@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/webpack-merge/-/webpack-merge-1.1.1.tgz#2816dce279f38fe05d62b6411144b7638553e61c" dependencies: lodash.clonedeep "^4.5.0" lodash.differencewith "^4.5.0"