Skip to content

Commit

Permalink
feat(Album View Page): Next/Prev arrows will load cached photo image …
Browse files Browse the repository at this point in the history
…when possible
  • Loading branch information
danactive committed Apr 1, 2020
1 parent 9dee9af commit 5d9ce5b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 89 deletions.
2 changes: 1 addition & 1 deletion ui/app/containers/AlbumViewPage/tests/reducer.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global beforeEach, describe, expect, test */
import produce from 'immer';

import json from '../../App/tests/fixtures/album';
import json from '../../App/tests/fixtures/album.json';
import pageReducer, { initialState } from '../reducer';
import {
loadAlbum,
Expand Down
2 changes: 1 addition & 1 deletion ui/app/containers/AlbumViewPage/tests/saga.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
LOAD_ALBUM_SUCCESS,
LOAD_ALBUM_ERROR,
} from '../constants';
import albumFixture from '../../App/tests/fixtures/album';
import albumFixture from '../../App/tests/fixtures/album.json';

describe('AlbumViewPage Saga', () => {
describe('getAlbumFile on Dropbox', () => {
Expand Down
22 changes: 0 additions & 22 deletions ui/app/containers/App/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
LOAD_NEXT_THUMB_PAGE_SUCCESS,
LOAD_THUMBS_SUCCESS,
PAGE_SIZE,
NEXT_MEMORY,
PREV_MEMORY,
} from '../AlbumViewPage/constants';
import { insertPage } from '../AlbumViewPage/paging';
import { LOAD_GALLERY, LOAD_GALLERY_SUCCESS } from '../GalleryViewPage/constants';
Expand Down Expand Up @@ -104,26 +102,6 @@ const appReducer = (state = initialState, action) => produce(state, (draft) => {
break;
}

case NEXT_MEMORY:
case PREV_MEMORY: {
const { memories } = state[state.host][state.gallery][state.album];
const currentMemoryId = state.currentMemory.id || 0;
const currentMemoryIndex = memories.findIndex(item => item.id === currentMemoryId);
let adjacentMemoryIndex = currentMemoryIndex + action.adjacentInt;

const carouselEnd = adjacentMemoryIndex >= memories.length;
if (carouselEnd) adjacentMemoryIndex -= memories.length;

const carouselBegin = adjacentMemoryIndex < 0;
if (carouselBegin) adjacentMemoryIndex = memories.length + adjacentMemoryIndex;

const findIndex = memories[adjacentMemoryIndex].id;

const found = state[state.host][state.gallery][state.album].memories.filter(item => item.id === findIndex)[0];
draft.currentMemory = found || {};
break;
}

case LOAD_GALLERY: {
draft.host = action.host;
draft.gallery = action.gallery;
Expand Down
60 changes: 2 additions & 58 deletions ui/app/containers/App/tests/reducer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
import produce from 'immer';

import appReducer, { initialState } from '../reducer';
import {
albumLoadSuccess,
chooseAdjacentMemory, loadAlbum,
} from '../../AlbumViewPage/actions';
import json from './fixtures/album';
import { albumLoadSuccess, loadAlbum } from '../../AlbumViewPage/actions';
import json from './fixtures/album.json';

/* eslint-disable default-case, no-param-reassign */
describe('appReducer', () => {
Expand Down Expand Up @@ -64,57 +61,4 @@ describe('appReducer', () => {

expect(received).toStrictEqual(expected);
});

describe('should handle the currentMemory action correctly', () => {
const testState = {
host: 'dropbox',
gallery: 'demo',
album: 'sample',
dropbox: {
demo: {
sample: {
memories: [
{ id: 1 },
{ id: 2 },
{ id: 3 },
],
},
},
},
currentMemory: {
id: 2,
},
};
test('should be one after ID two', () => {
const expectedResult = produce(testState, (draft) => {
draft.currentMemory.id = 3;
});

expect(appReducer(testState, chooseAdjacentMemory(1))).toEqual(expectedResult);
});

test('should be one before ID two', () => {
const expectedResult = produce(testState, (draft) => {
draft.currentMemory.id = 1;
});

expect(appReducer(testState, chooseAdjacentMemory(-1))).toEqual(expectedResult);
});

test('should be two after ID two', () => {
const expectedResult = produce(testState, (draft) => {
draft.currentMemory.id = 1;
});

expect(appReducer(testState, chooseAdjacentMemory(2))).toEqual(expectedResult);
});

test('should be two before ID two', () => {
const expectedResult = produce(testState, (draft) => {
draft.currentMemory.id = 3;
});

expect(appReducer(testState, chooseAdjacentMemory(-2))).toEqual(expectedResult);
});
});
});
40 changes: 33 additions & 7 deletions ui/app/containers/InfiniteThumbs/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import normalizeError from '../../utils/error';
import { CHOOSE_MEMORY } from '../App/constants';
import { makeSelectCurrentMemory } from '../App/selectors';
import { makeSelectMemories } from '../AlbumViewPage/selectors';
import { photoLoadError, photoLoadSuccess } from '../App/actions';
import {
NEXT_MEMORY,
PREV_MEMORY,
} from '../AlbumViewPage/constants';
import { chooseMemory, photoLoadError, photoLoadSuccess } from '../App/actions';
import { NEXT_MEMORY, PREV_MEMORY } from '../AlbumViewPage/constants';

const dbx = (process.env.HISTORY_DROPBOX_ACCESS_TOKEN) ? new Dropbox({ accessToken: process.env.HISTORY_DROPBOX_ACCESS_TOKEN, fetch }) : null;

Expand All @@ -34,7 +31,7 @@ export const argsPhotoXmlPath = ({ gallery, filename }) => {
};


// saga WORKER for CHOOSE_MEMORY
// saga WORKER for CHOOSE_MEMORY for Dropbox gallery
export function* getPhotoPathsOnDropbox({
currentMemory,
host,
Expand All @@ -57,6 +54,7 @@ export function* getPhotoPathsOnDropbox({
}
}

// saga WORKER for CHOOSE_MEMORY for local gallery
export function* getPhotoPathsLocally({
id,
currentMemory,
Expand All @@ -73,6 +71,7 @@ export function* getPhotoPathsLocally({
}));
}

// saga WORKER for CHOOSE_MEMORY
export function* getCurrentMemoryPhotoPath({ id }) {
const {
currentMemory,
Expand Down Expand Up @@ -107,8 +106,35 @@ export function* getCurrentMemoryPhotoPath({ id }) {
}
}

export const determineAdjacentInCarousel = ({ adjacentInt, currentMemory, memories }) => {
const currentMemoryId = currentMemory.id || 0;
const currentMemoryIndex = memories.findIndex(item => item.id === currentMemoryId);
let adjacentMemoryIndex = currentMemoryIndex + adjacentInt;

const carouselEnd = adjacentMemoryIndex >= memories.length;
if (carouselEnd) adjacentMemoryIndex -= memories.length;

const carouselBegin = adjacentMemoryIndex < 0;
if (carouselBegin) adjacentMemoryIndex = memories.length + adjacentMemoryIndex;

const findIndex = memories[adjacentMemoryIndex].id;

return findIndex;
};

// saga WORKER for NEXT_MEMORY, PREV_MEMORY
export function* calculateAdjacentMemoryId({ adjacentInt }) {
const { currentMemory } = yield select(makeSelectCurrentMemory());
const memories = yield select(makeSelectMemories());

const findIndex = determineAdjacentInCarousel({ adjacentInt, currentMemory, memories });

yield put(chooseMemory(findIndex));
}


// ROOT saga manages WATCHER lifecycle
export default function* InfiniteThumbsSagaWatcher() {
yield takeEvery([CHOOSE_MEMORY, NEXT_MEMORY, PREV_MEMORY], getCurrentMemoryPhotoPath);
yield takeEvery([CHOOSE_MEMORY], getCurrentMemoryPhotoPath);
yield takeEvery([NEXT_MEMORY, PREV_MEMORY], calculateAdjacentMemoryId);
}
36 changes: 36 additions & 0 deletions ui/app/containers/InfiniteThumbs/tests/saga.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* global describe, expect, test */
import { determineAdjacentInCarousel } from '../saga';

/* eslint-disable default-case, no-param-reassign */
describe('InfiniteThumbs Saga', () => {
describe('determineAdjacentInCarousel', () => {
describe('should handle the currentMemory action correctly', () => {
const testState = {
memories: [
{ id: 1 },
{ id: 2 },
{ id: 3 },
],
currentMemory: {
id: 2,
},
};

test('should be one after ID two', () => {
expect(determineAdjacentInCarousel({ adjacentInt: 1, ...testState })).toEqual(3);
});

test('should be one before ID two', () => {
expect(determineAdjacentInCarousel({ adjacentInt: -1, ...testState })).toEqual(1);
});

test('should be two after ID two', () => {
expect(determineAdjacentInCarousel({ adjacentInt: 2, ...testState })).toEqual(1);
});

test('should be two before ID two', () => {
expect(determineAdjacentInCarousel({ adjacentInt: -2, ...testState })).toEqual(3);
});
});
});
});

0 comments on commit 5d9ce5b

Please sign in to comment.