-
Notifications
You must be signed in to change notification settings - Fork 15
/
garden.ts
168 lines (142 loc) · 4.8 KB
/
garden.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import type { User } from '$lib/models/User';
import { CAMPSITES } from './collections';
import {
collection,
query,
where,
getDocs,
doc,
setDoc,
updateDoc,
getDocFromCache,
getDocFromServer,
getDoc,
CollectionReference
} from 'firebase/firestore';
import { getUser } from '$lib/stores/auth';
import { isUploading, uploadProgress, allGardens, isFetchingGardens } from '$lib/stores/garden';
import { db, storage } from './firebase';
import { getDownloadURL, ref, uploadBytesResumable } from 'firebase/storage';
import type { Garden, GardenFacilities } from '$lib/types/Garden';
/**
* Get a single garden, if it exists and is listed. Returns `null` otherwise.
* @param id the garden id
*/
export const getGarden = async (id: string) => {
const gardenDoc = await getDoc(
doc(collection(db(), CAMPSITES) as CollectionReference<Garden>, id)
);
let data = gardenDoc.data()!;
if (gardenDoc.exists() && data.listed) {
return {
id: gardenDoc.id,
...data
};
} else {
return null;
}
};
export const getAllListedGardens = async () => {
isFetchingGardens.set(true);
const q = query(collection(db(), CAMPSITES), where('listed', '==', true));
const querySnapshot = await getDocs(q);
const gardens: { [id: string]: Garden } = {};
querySnapshot.forEach((doc) => {
gardens[doc.id] = { id: doc.id, ...doc.data() };
});
allGardens.set(gardens);
isFetchingGardens.set(false);
return gardens;
};
const doUploadGardenPhoto = async (photo: File, currentUser: User) => {
const extension = photo.name.split('.').pop();
const photoLocation = `gardens/${currentUser.id}/garden.${extension}`;
isUploading.set(true);
const photoRef = ref(storage(), photoLocation);
const uploadTask = uploadBytesResumable(photoRef, photo, { contentType: photo.type });
uploadTask.on('state_changed', (snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
uploadProgress.set(progress);
});
// TODO: handle errors
await uploadTask;
isUploading.set(false);
uploadProgress.set(0);
return `garden.${extension}`;
};
export const addGarden = async ({
photo,
facilities,
...rest
}: {
photo: File;
facilities: GardenFacilities;
}) => {
const currentUser = getUser();
let uploadedName = null;
if (photo) uploadedName = await doUploadGardenPhoto(photo, currentUser);
// Copy the facilities object, converting any falsy value to false
// TODO: is this conversion necessary?
const facilitiesCopy = Object.fromEntries(
Object.entries(facilities).map(([k, v]) => [k, v || false])
);
const garden = {
...rest,
facilities: facilitiesCopy,
listed: true,
photo: uploadedName
};
await setDoc(doc(db(), CAMPSITES, currentUser.id), garden);
const gardenWithId = { ...garden, id: currentUser.id };
currentUser.setGarden(gardenWithId);
return gardenWithId;
};
export const updateGarden = async ({ photo, ...rest }: { photo: File; facilities }) => {
const currentUser = getUser();
if (!currentUser.id) throw new Error('User is not logged in.');
let uploadedName = null;
if (photo) uploadedName = await doUploadGardenPhoto(photo, currentUser);
const facilities = Object.keys(rest.facilities).reduce((all, facility) => {
all[facility] = rest.facilities[facility] || false;
return all;
}, {});
const garden = {
...rest,
facilities,
previousPhotoId: null
};
if (uploadedName || rest.photo) garden.photo = uploadedName || rest.photo;
const docRef = doc(db(), CAMPSITES, currentUser.id);
await updateDoc(docRef, garden);
const gardenWithId = { ...garden, id: currentUser.id };
getUser().setGarden(gardenWithId);
return gardenWithId;
};
export const changeListedStatus = async (shouldBeListed: boolean) => {
const currentUser = getUser();
if (!currentUser.id) throw new Error('User is not logged in.');
const docRef = doc(db(), CAMPSITES, currentUser.id);
await updateDoc(docRef, { listed: shouldBeListed });
};
const getPhotoBySize = async (size: string, garden: Garden & { photo: string }) => {
const photoLocation = `gardens/${garden.id}/garden_${size}.${garden.photo.split('.').pop()}`;
const photoRef = ref(storage(), photoLocation);
return await getDownloadURL(photoRef);
};
export const getGardenPhotoSmall = async (garden: Garden & { photo: string }) => {
return await getPhotoBySize('360x360', garden);
};
export const getGardenPhotoBig = async (garden: Garden & { photo: string }) => {
return await getPhotoBySize('1920x1080', garden);
};
export const hasGarden = async (userId: string) => {
let snapshot;
const docRef = doc(db(), CAMPSITES, userId);
try {
snapshot = await getDocFromCache(docRef);
} catch (error) {
// probably not cached
snapshot = await getDocFromServer(docRef);
}
return snapshot ? snapshot.exists() && snapshot.data().listed : false;
};