41 lines
1.1 KiB
TypeScript

import { useEffect, useState } from 'react';
import { Artist, ArtistResource } from '@/back-api';
import { useServiceContext } from '@/service/ServiceContext';
import { SessionServiceProps } from '@/service/session';
import { DataStoreType, useDataStore } from '@/utils/data-store';
export type ArtistServiceProps = {
store: DataStoreType<Artist>;
};
export const useArtistService = (): ArtistServiceProps => {
const { artist } = useServiceContext();
return artist;
};
export const useArtistServiceWrapped = (
session: SessionServiceProps
): ArtistServiceProps => {
const store = useDataStore<Artist>({
restApiName: 'ARTIST',
primaryKey: 'id',
getsCall: () => {
return ArtistResource.gets({
restConfig: session.getRestConfig(),
});
},
});
return { store };
};
export const useSpecificArtist = (id: number | undefined) => {
const [dataArtist, setDataArtist] = useState<Artist | undefined>(undefined);
const { store } = useArtistService();
useEffect(() => {
setDataArtist(store.get(id));
}, [store.data]);
return { dataArtist };
};