This repository has no description
0

Configure Feed

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

semble / src / webapp / features / collections / lib / dal.ts
6.4 kB 242 lines
1import { logoutUser, verifySessionOnClient } from '@/lib/auth/dal'; 2import { createSembleClient } from '@/services/client.apiClient'; 3import { 4 CardSortField, 5 CollectionAccessType, 6 CollectionSortField, 7 SortOrder, 8 UrlType, 9} from '@semble/types'; 10import { cache } from 'react'; 11 12interface PageParams { 13 page?: number; 14 limit?: number; 15 cardSortBy?: CardSortField; 16 collectionSortBy?: CollectionSortField; 17} 18 19interface SearchParams { 20 sortBy?: string; 21 sortOrder?: SortOrder; 22 searchText?: string; 23 urlType?: UrlType; 24 handleOrDid?: string; 25 accessType?: CollectionAccessType; 26 identifier?: string; 27} 28 29export const getCollectionsForUrl = cache( 30 async (url: string, params?: PageParams & { sortOrder?: SortOrder }) => { 31 const client = createSembleClient(); 32 const response = await client.getCollectionsForUrl({ 33 url, 34 page: params?.page, 35 limit: params?.limit, 36 sortBy: params?.collectionSortBy, 37 sortOrder: params?.sortOrder, 38 }); 39 40 return response; 41 }, 42); 43 44export const getCollections = cache( 45 async (handleOrDid: string, params?: PageParams & SearchParams) => { 46 const client = createSembleClient(); 47 const response = await client.getCollections({ 48 identifier: handleOrDid, 49 limit: params?.limit, 50 page: params?.page, 51 sortBy: params?.collectionSortBy, 52 sortOrder: params?.sortOrder, 53 searchText: params?.searchText, 54 }); 55 56 // Temp fix: filter out collections without uri 57 return { 58 ...response, 59 collections: response.collections.filter( 60 (collection) => collection.uri !== undefined, 61 ), 62 }; 63 }, 64); 65 66export const getMyCollections = cache( 67 async (params?: PageParams & SearchParams) => { 68 const session = await verifySessionOnClient({ redirectOnFail: true }); 69 if (!session) throw new Error('No session found'); 70 const client = createSembleClient(); 71 const response = await client.getMyCollections({ 72 page: params?.page, 73 limit: params?.limit, 74 sortBy: params?.collectionSortBy, 75 sortOrder: params?.sortOrder, 76 searchText: params?.searchText, 77 }); 78 79 // Temp fix: filter out collections without uri 80 return { 81 ...response, 82 collections: response.collections.filter( 83 (collection) => collection.uri !== undefined, 84 ), 85 }; 86 }, 87); 88 89export const getMyGemCollections = cache( 90 async (params?: PageParams & SearchParams) => { 91 const client = createSembleClient(); 92 const response = await client.getMyCollections({ 93 page: params?.page, 94 limit: params?.limit, 95 sortBy: params?.collectionSortBy, 96 sortOrder: params?.sortOrder, 97 searchText: params?.searchText, 98 }); 99 100 // Temp fix: filter out collections without uri 101 return { 102 ...response, 103 collections: response.collections.filter( 104 (collection) => collection.uri !== undefined, 105 ), 106 }; 107 }, 108); 109 110export const createCollection = cache( 111 async (newCollection: { 112 name: string; 113 description: string; 114 accessType: CollectionAccessType; 115 }) => { 116 const session = await verifySessionOnClient({ redirectOnFail: true }); 117 if (!session) throw new Error('No session found'); 118 const client = createSembleClient(); 119 120 try { 121 const response = await client.createCollection(newCollection); 122 123 return response; 124 } catch (error) { 125 await logoutUser(); 126 } 127 }, 128); 129 130export const deleteCollection = cache(async (id: string) => { 131 const session = await verifySessionOnClient({ redirectOnFail: true }); 132 if (!session) throw new Error('No session found'); 133 const client = createSembleClient(); 134 135 try { 136 const response = await client.deleteCollection({ collectionId: id }); 137 138 return response; 139 } catch (error) { 140 await logoutUser(); 141 } 142}); 143 144export const updateCollection = cache( 145 async (collection: { 146 collectionId: string; 147 rkey: string; 148 name: string; 149 description?: string; 150 accessType?: CollectionAccessType; 151 }) => { 152 const session = await verifySessionOnClient({ redirectOnFail: true }); 153 if (!session) throw new Error('No session found'); 154 const client = createSembleClient(); 155 156 try { 157 const response = await client.updateCollection(collection); 158 159 return response; 160 } catch (error) { 161 await logoutUser(); 162 } 163 }, 164); 165 166export const getCollectionPageByAtUri = cache( 167 async ({ 168 recordKey, 169 handle, 170 params, 171 }: { 172 recordKey: string; 173 handle: string; 174 params?: PageParams & SearchParams; 175 }) => { 176 const client = createSembleClient(); 177 const response = await client.getCollectionPageByAtUri({ 178 recordKey, 179 handle, 180 page: params?.page, 181 limit: params?.limit, 182 sortBy: params?.cardSortBy, 183 sortOrder: params?.sortOrder, 184 urlType: params?.urlType, 185 }); 186 187 return response; 188 }, 189); 190 191export const searchCollections = cache( 192 async (params?: PageParams & SearchParams) => { 193 const client = createSembleClient(); 194 const response = await client.searchCollections({ 195 page: params?.page, 196 limit: params?.limit, 197 sortBy: params?.collectionSortBy, 198 sortOrder: params?.sortOrder, 199 searchText: params?.searchText, 200 accessType: params?.accessType, 201 identifier: params?.identifier, 202 }); 203 204 // Temp fix: filter out collections without uri 205 return { 206 ...response, 207 collections: response.collections.filter( 208 (collection) => collection.uri !== undefined, 209 ), 210 }; 211 }, 212); 213 214export const getOpenCollectionsWithContributor = cache( 215 async (params?: PageParams & { identifier: string }) => { 216 const client = createSembleClient(); 217 const response = await client.getOpenCollectionsWithContributor({ 218 identifier: params?.identifier || '', 219 page: params?.page, 220 limit: params?.limit, 221 sortBy: params?.collectionSortBy, 222 }); 223 224 // temp fix: filter out collections without uri 225 return { 226 ...response, 227 collections: response.collections.filter((c) => !!c.uri), 228 }; 229 }, 230); 231 232export const getCollectionContributors = cache( 233 async (collectionId: string, params?: PageParams) => { 234 const client = createSembleClient(); 235 const response = await client.getCollectionContributors({ 236 collectionId, 237 page: params?.page, 238 limit: params?.limit, 239 }); 240 return response; 241 }, 242);