[READ-ONLY] Mirror of https://github.com/danielroe/dwaring87-webdev-wedding. Marissa & David's Wedding Website!
2.4 kB
104 lines
1export const useCMS = () => {
2 const { getItems, getSingletonItem } = useDirectusItems();
3
4 /**
5 * Get the key/value pairs of the requested details
6 * @param keys The array of keys to get (return all details, if not defined)
7 * @returns The value or an object of key/value pairs
8 */
9 const getDetails = async (keys?: string|string[]): Promise<string|Details> => {
10 keys = typeof keys === 'string' ? [keys] : keys;
11 const details: Detail[] = await getItems({
12 collection: 'details',
13 params: {
14 fields: ['key', 'value'],
15 filter: {
16 key: keys ? { "_in": keys } : undefined
17 }
18 }
19 });
20
21 if ( details && details.length === 1 ) {
22 return details[0].value;
23 }
24 else if ( details && details.length > 1 ) {
25 let rtn: Details = {};
26 details.forEach((detail) => {
27 rtn[detail.key] = detail.value;
28 });
29 return rtn;
30 }
31 else {
32 return {};
33 }
34 }
35
36 /**
37 * Get the key/image id pairs of the requested photos
38 * @param keys The array of keys to get (return all photos, if not defined)
39 * @returns The image id or an object of key/image id pairs
40 */
41 const getPhotos = async(keys?: string|string[]): Promise<string|Photos> => {
42 keys = typeof keys === 'string' ? [keys] : keys;
43 const photos: Photos[] = await getItems({
44 collection: 'photos',
45 params: {
46 fields: ['key', 'image'],
47 filter: {
48 key: keys ? { "_in": keys } : undefined
49 }
50 }
51 });
52
53 if ( photos && photos.length === 1 ) {
54 return photos[0].image;
55 }
56 else if ( photos && photos.length > 1 ) {
57 let rtn: Photos = {};
58 photos.forEach((photo) => {
59 rtn[photo.key] = photo.image;
60 });
61 return rtn;
62 }
63 else {
64 return {};
65 }
66 }
67
68
69 return {
70 getDetails,
71 getPhotos
72 }
73}
74
75
76/**
77 * A key/value pair from the Details collection
78 */
79type Detail = {
80 key: string,
81 value: string
82}
83
84/**
85 * A collection of Details (key/value pairs)
86 */
87type Details = {
88 [key: Detail["key"]]: Detail["value"];
89}
90
91/**
92 * A key/id pair from the Photos collection
93 */
94type Photo = {
95 key: string,
96 id: string
97}
98
99/**
100 * A collection of Photos (key/id pairs)
101 */
102type Photos = {
103 [key: Photo["key"]]: Photo["id"];
104}