[READ-ONLY] Mirror of https://github.com/danielroe/dwaring87-webdev-wedding. Marissa & David's Wedding Website!
0

Configure Feed

Select the types of activity you want to include in your feed.

1export const useCMS = () => { 2 const { getItems, getSingletonItem, updateItem } = useDirectusItems(); 3 4 /** 5 * Get the currently set Alert from the database 6 * @returns The title and message of an Alert 7 */ 8 const getAlert = async(): Promise<Alert|undefined> => { 9 const alert: Alert = await getSingletonItem({ 10 collection: 'alert', 11 params: { 12 fields: ['title', 'message'] 13 } 14 }); 15 if ( alert && alert.title && alert.message ) { 16 return alert; 17 } 18 } 19 20 /** 21 * Get a Page 22 * @returns The properties of a Page 23 */ 24 const getPage = async(slug: string): Promise<Page|undefined> => { 25 let pages: Page[] = await getItems({ 26 collection: 'pages', 27 params: { 28 fields: ['slug', 'title', 'content', 'date_updated'], 29 filter: { 30 slug: { "_eq": slug } 31 } 32 } 33 }) 34 if ( pages && pages.length === 1 ) { 35 return pages[0]; 36 } 37 } 38 39 /** 40 * Get the key/value pairs of the requested details 41 * @param keys The array of keys to get (return all details, if not defined) 42 * @returns The value or an object of key/value pairs 43 */ 44 const getDetails = async (keys?: string|string[]): Promise<string|Details> => { 45 keys = typeof keys === 'string' ? [keys] : keys; 46 const details: Detail[] = await getItems({ 47 collection: 'details', 48 params: { 49 fields: ['key', 'value'], 50 filter: { 51 key: keys ? { "_in": keys } : undefined 52 } 53 } 54 }); 55 56 if ( details && details.length === 1 ) { 57 return details[0].value; 58 } 59 else if ( details && details.length > 1 ) { 60 let rtn: Details = {}; 61 details.forEach((detail) => { 62 rtn[detail.key] = detail.value; 63 }); 64 return rtn; 65 } 66 else { 67 return {}; 68 } 69 } 70 71 /** 72 * Get the key/image id pairs of the requested photos 73 * @param keys The array of keys to get (return all photos, if not defined) 74 * @returns The image id or an object of key/image id pairs 75 */ 76 const getPhotos = async(keys?: string|string[]): Promise<string|Photos> => { 77 keys = typeof keys === 'string' ? [keys] : keys; 78 const photos: Photos[] = await getItems({ 79 collection: 'photos', 80 params: { 81 fields: ['key', 'image'], 82 filter: { 83 key: keys ? { "_in": keys } : undefined 84 } 85 } 86 }); 87 88 if ( photos && photos.length === 1 ) { 89 return photos[0].image; 90 } 91 else if ( photos && photos.length > 1 ) { 92 let rtn: Photos = {}; 93 photos.forEach((photo) => { 94 rtn[photo.key] = photo.image; 95 }); 96 return rtn; 97 } 98 else { 99 return {}; 100 } 101 } 102 103 104 /** 105 * Get the invitation name and associated Guests of the specified Invitation 106 * @param code Invite Code 107 * @returns Invitation and Guests 108 */ 109 const getInvitation = async(code: string): Promise<Invitation|undefined> => { 110 const invitations: Invitation[] = await getItems({ 111 collection: 'invitations', 112 params: { 113 fields: ['name', 'invite_code', 'guests.id', 'guests.name', 'guests.email', 'guests.rsvp_welcome', 'guests.rsvp', 'guests.transportation', 'guests.dietary_restrictions', 'guests.notes'], 114 filter: { 115 invite_code: { '_eq': code } 116 } 117 } 118 }); 119 if ( invitations && invitations.length === 1 ) { 120 return invitations[0]; 121 } 122 } 123 124 125 /** 126 * Update the Guest Properties 127 * @param guest The Guest to Update, with new properties set (id is required) 128 * @returns success flag 129 */ 130 const updateGuest = async(guest: Guest): Promise<boolean> => { 131 try { 132 if ( !guest || !guest.id ) { 133 console.log("ERROR: Guest ID is required!"); 134 return false; 135 } 136 137 console.log("==> UPDATE GUEST:"); 138 console.log(guest); 139 return false; 140 141 await updateItem<Guest>({ 142 collection: "guests", 143 id: guest.id, 144 item: guest, 145 }); 146 return true; 147 } 148 catch (e) { 149 console.log("ERROR: Could not update Guest!"); 150 console.log(e); 151 return false; 152 } 153 } 154 155 156 return { 157 getAlert, 158 getPage, 159 getDetails, 160 getPhotos, 161 getInvitation, 162 updateGuest 163 } 164} 165 166 167/** 168 * An Alert 169 */ 170type Alert = { 171 title: string, 172 message: string 173} 174 175/** 176 * A Page 177 */ 178type Page = { 179 slug: string, 180 title: string, 181 content: string, 182 date_updated: Date 183} 184 185/** 186 * A Guest 187 */ 188type Guest = { 189 id: string, 190 name: string, 191 email: string, 192 rsvp_welcome: boolean, 193 rsvp: boolean, 194 transportation: boolean, 195 dietary_restrictions: String[], 196 notes: string 197} 198 199/** 200 * An Invitation and its associated Guests 201 */ 202type Invitation = { 203 name: string, 204 invite_code: string, 205 guests: Guest[] 206} 207 208/** 209 * A key/value pair from the Details collection 210 */ 211type Detail = { 212 key: string, 213 value: string 214} 215 216/** 217 * A collection of Details (key/value pairs) 218 */ 219type Details = { 220 [key: Detail["key"]]: Detail["value"]; 221} 222 223/** 224 * A key/id pair from the Photos collection 225 */ 226type Photo = { 227 key: string, 228 id: string 229} 230 231/** 232 * A collection of Photos (key/id pairs) 233 */ 234type Photos = { 235 [key: Photo["key"]]: Photo["id"]; 236} 237 238export type { 239 Guest, 240 Invitation, 241 Page, 242 Detail, 243 Photo 244}