[READ-ONLY] Mirror of https://github.com/danielroe/dwaring87-webdev-wedding. Marissa & David's Wedding Website!
6.1 kB
230 lines
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 () => {
9 const 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 the metadata (title and slug) for all Pages
22 * @returns The metadata of all Pages
23 */
24 const getPages = async () => {
25 const { data } = await useAsyncData('pages', async () => {
26 return await getItems({
27 collection: 'pages',
28 params: {
29 fields: ['slug', 'title', 'description']
30 }
31 });
32 });
33 return data;
34 }
35
36 /**
37 * Get all information for the specified Page
38 * @param slug The page slug
39 * @returns The properties of a Page
40 */
41 const getPage = async (slug) => {
42 const { data } = await useAsyncData(`page-${slug}`, async () => {
43 const pages = await getItems({
44 collection: 'pages',
45 params: {
46 fields: ['slug', 'title', 'content', 'date_updated'],
47 filter: {
48 slug: { "_eq": slug }
49 }
50 }
51 })
52 if ( pages && pages.length === 1 ) {
53 return pages[0];
54 }
55 });
56 return data;
57 }
58
59 /**
60 * Get the key/value pairs of the requested details
61 * @param keys The array of keys to get (return all details, if not defined)
62 * @returns The value or an object of key/value pairs
63 */
64 const getDetails = async (keys) => {
65 keys = typeof keys === 'string' ? [keys] : keys;
66 const { data } = await useAsyncData(`details-${keys ? keys.sort().join('|') : ''}`, async () => {
67 const details = await getItems({
68 collection: 'details',
69 params: {
70 fields: ['key', 'value'],
71 filter: {
72 key: keys ? { "_in": keys } : undefined
73 }
74 }
75 });
76
77 if ( details && details.length === 1 ) {
78 return details[0].value;
79 }
80 else if ( details && details.length > 1 ) {
81 let rtn = {};
82 details.forEach((detail) => {
83 rtn[detail.key] = detail.value;
84 });
85 return rtn;
86 }
87 else {
88 return {};
89 }
90 });
91 return data;
92 }
93
94 /**
95 * Get the key/image id pairs of the requested photos
96 * @param keys The array of keys to get (return all photos, if not defined)
97 * @returns The image id or an object of key/image id pairs
98 */
99 const getPhotos = async (keys) => {
100 keys = typeof keys === 'string' ? [keys] : keys;
101 const { data } = await useAsyncData(`photos-${keys ? keys.sort().join('|') : ''}`, async () => {
102 const photos = await getItems({
103 collection: 'photos',
104 params: {
105 fields: ['key', 'image'],
106 filter: {
107 key: keys ? { "_in": keys } : undefined
108 }
109 }
110 });
111
112 if ( photos && photos.length === 1 ) {
113 return photos[0].image;
114 }
115 else if ( photos && photos.length > 1 ) {
116 let rtn = {};
117 photos.forEach((photo) => {
118 rtn[photo.key] = photo.image;
119 });
120 return rtn;
121 }
122 else {
123 return {};
124 }
125 });
126 return data;
127 }
128
129
130 /**
131 * Get the invitation name and associated Guests of the specified Invitation
132 * @param code Invite Code
133 * @returns Invitation and Guests
134 */
135 const getInvitation = async (code) => {
136 const { data } = await useAsyncData(`invitation-${code}`, async () => {
137 const invitations = await getItems({
138 collection: 'invitations',
139 params: {
140 fields: ['name', 'invite_code', 'guests.id', 'guests.name', 'guests.email', 'guests.rsvp_welcome', 'guests.rsvp', 'guests.dietary_restrictions', 'guests.notes'],
141 filter: {
142 invite_code: { '_eq': code }
143 }
144 }
145 });
146 if ( invitations && invitations.length === 1 ) {
147 return invitations[0];
148 }
149 });
150 return data;
151 }
152
153
154 /**
155 * Update the Guest Properties
156 * @param id The Guest ID
157 * @param properties The Updated Guest Properties
158 * @returns { success, error }
159 */
160 const updateGuest = async (id, properties) => {
161 try {
162 if ( !id || id === '' ) {
163 console.log("ERROR: Guest ID is required!");
164 return { success: false, error: "Guest ID not provided" };
165 }
166
167 for ( const key in properties ) {
168 if ( properties.hasOwnProperty(key) ) {
169 let value = properties[key];
170 if ( typeof value === 'undefined' ) {
171 delete properties[key];
172 }
173 }
174 }
175
176 const resp = await updateItem({
177 collection: "guests",
178 id: id,
179 item: properties,
180 });
181 const success = resp && resp.id === id;
182 return success ? { success: true } : { success: false, error: `Invalid API Response: ${JSON.stringify(resp)}` }
183 }
184 catch (e) {
185 console.log("ERROR: Could not update Guest!");
186 console.log(e);
187 return { success: false, error: e }
188 }
189 }
190
191
192 /**
193 * Get all of the recommendataions, grouped by category
194 * @returns Recommendations
195 */
196 const getRecommendations = async () => {
197 const { data } = await useAsyncData('recommendations', async () => {
198 const recommendations = await getItems({
199 collection: 'recommendations',
200 params: {
201 fields: ['category', 'name', 'location', 'website.url', 'website.title', 'map.title', 'map.url', 'description', 'image']
202 }
203 });
204
205 // Group by category
206 let rtn = {};
207 if ( recommendations ) {
208 recommendations.forEach((rec) => {
209 if ( !rtn.hasOwnProperty(rec.category) ) rtn[rec.category] = [];
210 rtn[rec.category].push(rec);
211 })
212 }
213
214 return rtn;
215 });
216 return data;
217 }
218
219
220 return {
221 getAlert,
222 getPages,
223 getPage,
224 getDetails,
225 getPhotos,
226 getInvitation,
227 updateGuest,
228 getRecommendations
229 }
230}