2023-07-31 08:16:52 +00:00
|
|
|
import {
|
|
|
|
ASSETS_CATEGORIES,
|
|
|
|
IMAGE_CATEGORIES,
|
|
|
|
} from 'features/gallery/store/types';
|
|
|
|
import { ImageCache, ImageDTO, ListImagesArgs } from './types';
|
|
|
|
import { createEntityAdapter } from '@reduxjs/toolkit';
|
|
|
|
import { dateComparator } from 'common/util/dateComparator';
|
|
|
|
import queryString from 'query-string';
|
|
|
|
|
|
|
|
export const getIsImageInDateRange = (
|
|
|
|
data: ImageCache | undefined,
|
|
|
|
imageDTO: ImageDTO
|
|
|
|
) => {
|
|
|
|
if (!data) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-11 18:41:48 +00:00
|
|
|
const totalCachedImageDtos = imagesSelectors.selectAll(data);
|
|
|
|
|
|
|
|
if (totalCachedImageDtos.length <= 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:55:28 +00:00
|
|
|
const cachedStarredImages = [];
|
|
|
|
const cachedUnstarredImages = [];
|
2023-08-11 18:41:48 +00:00
|
|
|
|
2023-08-11 20:14:47 +00:00
|
|
|
for (let index = 0; index < totalCachedImageDtos.length; index++) {
|
|
|
|
const image = totalCachedImageDtos[index];
|
2023-08-15 16:55:28 +00:00
|
|
|
if (image?.starred) cachedStarredImages.push(image)
|
|
|
|
if (!image?.starred) cachedUnstarredImages.push(image)
|
2023-08-11 20:14:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 16:55:28 +00:00
|
|
|
if (imageDTO.starred) {
|
|
|
|
const lastStarredImage = cachedStarredImages[cachedStarredImages.length - 1];
|
|
|
|
// if starring or already starred, want to look in list of starred images
|
|
|
|
if (!lastStarredImage) return true; // no starred images showing, so always show this one
|
2023-08-11 20:14:47 +00:00
|
|
|
const createdDate = new Date(imageDTO.created_at);
|
2023-08-15 16:55:28 +00:00
|
|
|
const oldestDate = new Date(lastStarredImage.created_at);
|
2023-08-11 20:14:47 +00:00
|
|
|
return createdDate >= oldestDate;
|
|
|
|
} else {
|
2023-08-15 16:55:28 +00:00
|
|
|
const lastUnstarredImage = cachedUnstarredImages[cachedUnstarredImages.length - 1];
|
|
|
|
// if unstarring or already unstarred, want to look in list of unstarred images
|
|
|
|
if (!lastUnstarredImage) return false; // no unstarred images showing, so don't show this one
|
2023-07-31 08:16:52 +00:00
|
|
|
const createdDate = new Date(imageDTO.created_at);
|
2023-08-15 16:55:28 +00:00
|
|
|
const oldestDate = new Date(lastUnstarredImage.created_at);
|
2023-07-31 08:16:52 +00:00
|
|
|
return createdDate >= oldestDate;
|
|
|
|
}
|
2023-08-11 20:14:47 +00:00
|
|
|
|
2023-07-31 08:16:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getCategories = (imageDTO: ImageDTO) => {
|
|
|
|
if (IMAGE_CATEGORIES.includes(imageDTO.image_category)) {
|
|
|
|
return IMAGE_CATEGORIES;
|
|
|
|
}
|
|
|
|
return ASSETS_CATEGORIES;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The adapter is not actually the data store - it just provides helper functions to interact
|
|
|
|
// with some other store of data. We will use the RTK Query cache as that store.
|
|
|
|
export const imagesAdapter = createEntityAdapter<ImageDTO>({
|
|
|
|
selectId: (image) => image.image_name,
|
2023-08-11 18:41:48 +00:00
|
|
|
sortComparer: (a, b) => {
|
2023-08-15 16:55:28 +00:00
|
|
|
// Compare starred images first
|
|
|
|
if (a.starred && !b.starred) {
|
2023-08-11 18:41:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2023-08-15 16:55:28 +00:00
|
|
|
if (!a.starred && b.starred) {
|
2023-08-11 18:41:48 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return dateComparator(b.created_at, a.created_at)
|
|
|
|
},
|
2023-07-31 08:16:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Create selectors for the adapter.
|
|
|
|
export const imagesSelectors = imagesAdapter.getSelectors();
|
|
|
|
|
|
|
|
// Helper to create the url for the listImages endpoint. Also we use it to create the cache key.
|
|
|
|
export const getListImagesUrl = (queryArgs: ListImagesArgs) =>
|
|
|
|
`images/?${queryString.stringify(queryArgs, { arrayFormat: 'none' })}`;
|