refactor(client-side): create utilities
mainly intended to avoid repitition in long lists of data like stories, authors, and/or bands
This commit is contained in:
		
							parent
							
								
									00d4467b87
								
							
						
					
					
						commit
						ba05134015
					
				
							
								
								
									
										78
									
								
								lib/client/listActions.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								lib/client/listActions.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,78 @@ | ||||
| import { ListGridType } from "ant-design-vue/es/list"; | ||||
| import { FavPayload, HidePayload, SubPayload } from "./types/form/favSub"; | ||||
| 
 | ||||
| const base = `/user/me`; | ||||
| 
 | ||||
| export const favourites = ( | ||||
| 	values: (any & { _id: number })[], | ||||
| 	id: number, | ||||
| 	remove: boolean, | ||||
| 	type: "story" | "author", | ||||
| ) => { | ||||
| 	values?.splice( | ||||
| 		values!.findIndex((a) => a._id == id), | ||||
| 		1, | ||||
| 	); | ||||
| 	const key = type === "story" ? "stories" : "authors"; | ||||
| 	const todo = [id]; | ||||
| 	useApiFetch(`${base}/favs`, { | ||||
| 		method: "put", | ||||
| 		body: { | ||||
| 			[key]: { | ||||
| 				pull: remove ? todo : [], | ||||
| 				push: !remove ? todo : [], | ||||
| 			}, | ||||
| 		} as FavPayload, | ||||
| 	}); | ||||
| }; | ||||
| 
 | ||||
| export const subscriptions = ( | ||||
| 	values: (any & { _id: number })[], | ||||
| 	id: number, | ||||
| 	action: "hide" | "subscribe" | "unsubscribe", | ||||
| 	type: "bands" | "authors", | ||||
| ) => { | ||||
| 	values?.splice( | ||||
| 		values!.findIndex((a) => a._id == id), | ||||
| 		1, | ||||
| 	); | ||||
| 	if (action == "hide") { | ||||
| 		useApiFetch(`${base}/${action}`, { | ||||
| 			body: { | ||||
| 				push: { | ||||
| 					[type]: [id], | ||||
| 				}, | ||||
| 				pull: {}, | ||||
| 			} as HidePayload, | ||||
| 			method: "put", | ||||
| 		}); | ||||
| 	} else if (action == "subscribe") { | ||||
| 		useApiFetch(`${base}/subscriptions`, { | ||||
| 			body: { | ||||
| 				push: { | ||||
| 					[type]: [id], | ||||
| 				}, | ||||
| 				pull: {}, | ||||
| 			} as SubPayload, | ||||
| 			method: "put", | ||||
| 		}); | ||||
| 	} else if (action == "unsubscribe") { | ||||
| 		useApiFetch(`${base}/subscriptions`, { | ||||
| 			body: { | ||||
| 				pull: { | ||||
| 					[type]: [id], | ||||
| 				}, | ||||
| 				push: {}, | ||||
| 			} as SubPayload, | ||||
| 			method: "put", | ||||
| 		}); | ||||
| 	} | ||||
| }; | ||||
| export const bp: ListGridType = { | ||||
| 	gutter: 1, | ||||
| 	xs: 1, | ||||
| 	sm: 2, | ||||
| 	md: 3, | ||||
| 	lg: 4, | ||||
| 	xl: 5, | ||||
| }; | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user