-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.ts
174 lines (148 loc) · 4.26 KB
/
query.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
169
170
171
172
173
174
import { Toast } from "native-base";
import { useQuery } from "react-query"; // for my API fetching AND caching needs.
import create from "zustand"; // for my UI state and general component communication needs. NO CONTEXT, REDUX, etc.
const moment = require("moment");
type PHOTO_URLS = {
full: string;
small: string;
thumb: string;
regular: string;
};
type UNSPLASH_USER = {
bio: string;
first_name: string;
id: string;
last_name: string;
profile_image: {
large: string;
medium: string;
small: string;
};
};
export type UNSPLASH_PHOTO = {
alt_description: string;
created_at: string;
id: string;
likes: number;
updated_at: string;
width: number;
height: number;
description: string;
urls: PHOTO_URLS;
user: UNSPLASH_USER;
};
interface AppStore {
displayedPhotos: UNSPLASH_PHOTO[] | [];
setDisplayedPhotos: Function;
favoritePhotos: UNSPLASH_PHOTO[];
updateFavoritesPhotos: Function;
searchedPhotos: UNSPLASH_PHOTO[] | [];
updateSearchedPhotos: Function;
page: number;
setPage: Function;
searchQuery: string;
setSearchQuery: Function;
selectedCard: number;
setSelectedCard: Function;
sortPreference: string;
setSortPreference: Function;
showModal: boolean;
setShowModal: Function;
}
export const useStore = create<AppStore>((set) => ({
displayedPhotos: [],
setDisplayedPhotos: (photos: UNSPLASH_PHOTO[]) => {
set((state) => {
if (state.sortPreference?.length > 0) {
const copy: UNSPLASH_PHOTO[] = Object.assign([], photos);
if (state.sortPreference === "Newest") {
copy?.sort((a, b) =>
moment.utc(b.created_at).diff(moment.utc(a.created_at))
);
} else {
copy?.sort((a, b) => {
if (a.likes < b.likes) return 1;
if (a.likes > b.likes) return -1;
return 0;
});
}
return { displayedPhotos: copy };
}
return { displayedPhotos: photos };
});
},
searchedPhotos: [],
updateSearchedPhotos: (photos: UNSPLASH_PHOTO[]) => {
set(() => ({
searchedPhotos: photos,
}));
},
favoritePhotos: [],
updateFavoritesPhotos: (photo: UNSPLASH_PHOTO) =>
set((state) => {
let currentPhotos = state.favoritePhotos;
const index = currentPhotos?.findIndex((item) => item.id === photo?.id);
if (index === -1) {
// photo not found, so add it
currentPhotos?.push(photo);
Toast.show({
description: "Photo added to favorites!",
backgroundColor: "black",
duration: 3000,
});
} else {
currentPhotos = currentPhotos?.filter((item) => item.id != photo?.id);
Toast.show({
description: "Photo removed from favorites.",
backgroundColor: "black",
duration: 3000,
});
}
return {
favoritePhotos: currentPhotos,
};
}),
page: 1,
setPage: (page: number) => set(() => ({ page })),
searchQuery: "",
setSearchQuery: (query: string) => set(() => ({ searchQuery: query })),
selectedCard: -1,
setSelectedCard: (index: number) =>
set(() => ({
selectedCard: index,
})),
sortPreference: "",
setSortPreference: (preference: string) =>
set((state) => {
const copy: UNSPLASH_PHOTO[] = Object.assign([], state.displayedPhotos);
if (preference === "Newest") {
copy?.sort((a, b) =>
moment.utc(b.created_at).diff(moment.utc(a.created_at))
);
} else {
copy?.sort((a, b) => {
if (a.likes < b.likes) return 1;
if (a.likes > b.likes) return -1;
return 0;
});
}
return {
sortPreference: preference,
displayedPhotos: copy,
};
}),
showModal: false,
setShowModal: (show: boolean) => set(() => ({ showModal: show })),
}));
export const useSearchForPhotos = (searchPhrase: string, page: number) => {
const SearchPhotos = async () => {
const results = await fetch(
`https://api.unsplash.com/search/photos?client_id=2dj2v7k4BkTZ3l9wH3uNO0lEI462mnEQIYdflynxp2k&count=10&page=${page}&query=${searchPhrase}`
);
return (await results.json()).results as UNSPLASH_PHOTO[];
};
return useQuery<UNSPLASH_PHOTO[], Error>(
["searched_photos", searchPhrase, page],
SearchPhotos
);
};