Skip to content

Commit

Permalink
refactor albums by date to RTK
Browse files Browse the repository at this point in the history
  • Loading branch information
mdm88 committed Feb 13, 2024
1 parent dc71883 commit 32c3810
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/actions/albumActions.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export const FetchAutoAlbumsListResponseSchema = z.object({
export const FetchDateAlbumsListResponseSchema = z.object({
results: IncompleteDatePhotosGroupSchema.array(),
});
export const FetchDateAlbumResponseSchema = z.object({
results: IncompleteDatePhotosGroupSchema,
});

export const FetchUserAlbumsSharedResponseSchema = z.object({
results: UserAlbumInfoSchema.array(),
Expand Down
62 changes: 62 additions & 0 deletions src/api_client/albums/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { FetchDateAlbumResponseSchema, FetchDateAlbumsListResponseSchema } from "../../actions/albumActions.types";
import { IncompleteDatePhotosGroup } from "../../actions/photosActions.types";
import { addTempElementsToGroups } from "../../util/util";
import { api } from "../api";

enum Endpoints {
fetchDateAlbums = "fetchDateAlbums",
fetchDateAlbum = "fetchDateAlbum",
}

export const dateAlbumsApi = api
.injectEndpoints({
endpoints: builder => ({
[Endpoints.fetchDateAlbums]: builder.query<IncompleteDatePhotosGroup[], void>({
query: () => "albums/date/list/",
transformResponse: response => {
const { results } = FetchDateAlbumsListResponseSchema.parse(response);
addTempElementsToGroups(results);

return results;
},
}),
[Endpoints.fetchDateAlbum]: builder.query<IncompleteDatePhotosGroup, { id: string; page: number }>({
query: ({ id, page }) => `albums/date/${id}/?page=${page}`,
transformResponse: response => FetchDateAlbumResponseSchema.parse(response).results,
// Add results to fetchDateAlbums cache
async onQueryStarted({ id, page }, { dispatch, queryFulfilled }) {
const { data } = await queryFulfilled;

dispatch(
dateAlbumsApi.util.updateQueryData(Endpoints.fetchDateAlbums, undefined, draft => {
const indexToReplace = draft.findIndex(group => group.id === id);
const groupToChange = draft[indexToReplace];
if (!groupToChange) return;

const { items } = groupToChange;
groupToChange.items = items
.slice(0, (page - 1) * 100)
.concat(data.items)
.concat(items.slice(page * 100));

// eslint-disable-next-line no-param-reassign
draft[indexToReplace] = groupToChange;
})
);
},
}),
}),
})
.enhanceEndpoints<"DateAlbums" | "DateAlbum">({
addTagTypes: ["DateAlbums", "DateAlbum"],
endpoints: {
[Endpoints.fetchDateAlbums]: {
providesTags: ["DateAlbums"],
},
[Endpoints.fetchDateAlbum]: {
providesTags: ["DateAlbum"],
},
},
});

export const { useFetchDateAlbumsQuery, useFetchDateAlbumQuery } = dateAlbumsApi;
36 changes: 14 additions & 22 deletions src/layouts/photos/TimestampPhotos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,32 @@ import { IconPhoto as Photo } from "@tabler/icons-react";
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";

import { fetchAlbumDate, fetchAlbumDateList } from "../../actions/albumsActions";
import { PigPhoto } from "../../actions/photosActions.types";
import { useFetchDateAlbumQuery, useFetchDateAlbumsQuery } from "../../api_client/albums/date";
import { PhotoListView } from "../../components/photolist/PhotoListView";
import type { PhotosState } from "../../reducers/photosReducer";
import { PhotosetType } from "../../reducers/photosReducer";
import { useAppDispatch, useAppSelector } from "../../store/store";
import { getPhotosFlatFromGroupedByDate } from "../../util/util";
import type { PhotoGroup } from "./common";

export function TimestampPhotos() {
const { fetchedPhotosetType, photosFlat, photosGroupedByDate } = useAppSelector(state => state.photos as PhotosState);
const dispatch = useAppDispatch();
const { t } = useTranslation();
const [group, setGroup] = useState({} as PhotoGroup);
useEffect(() => {
if (group.id && group.page) {
fetchAlbumDate(dispatch, {
album_date_id: group.id,
page: group.page,
photosetType: PhotosetType.TIMESTAMP,
});
}
}, [dispatch, group.id, group.page]);
const [photosFlat, setPhotosFlat] = useState<PigPhoto[]>([]);

const { data: photosGroupedByDate, isLoading } = useFetchDateAlbumsQuery();

useEffect(() => {
if (fetchedPhotosetType !== PhotosetType.TIMESTAMP) {
fetchAlbumDateList(dispatch, { photosetType: PhotosetType.TIMESTAMP });
}
}, [dispatch]);
if (photosGroupedByDate) setPhotosFlat(getPhotosFlatFromGroupedByDate(photosGroupedByDate));
}, [photosGroupedByDate]);

const [group, setGroup] = useState({} as PhotoGroup);
useFetchDateAlbumQuery(group, { skip: !group.id });

const getAlbums = (visibleGroups: any) => {
visibleGroups.reverse().forEach((photoGroup: any) => {
const visibleImages = photoGroup.items;
if (visibleImages.filter((i: any) => i.isTemp).length > 0) {
const firstTempObject = visibleImages.filter((i: any) => i.isTemp)[0];
const page = Math.ceil((parseInt(firstTempObject.id, 10) + 1) / 100);

setGroup({ id: photoGroup.id, page });
}
});
Expand All @@ -44,9 +36,9 @@ export function TimestampPhotos() {
return (
<PhotoListView
title={t("photos.photos")}
loading={fetchedPhotosetType !== PhotosetType.TIMESTAMP}
loading={isLoading}
icon={<Photo size={50} />}
photoset={photosGroupedByDate}
photoset={photosGroupedByDate ?? []}
idx2hash={photosFlat}
updateGroups={getAlbums}
selectable
Expand Down

0 comments on commit 32c3810

Please sign in to comment.