feat(appstate): Refactor data fetching to use helper for stripping class instances
This commit is contained in:
		| @@ -2,6 +2,11 @@ import * as plugins from './plugins.js'; | ||||
| import * as domtools from '@design.estate/dees-domtools'; | ||||
|  | ||||
| const appstate = new plugins.deesDomtools.plugins.smartstate.Smartstate(); | ||||
|  | ||||
| // Helper: strip class instances (with circular refs) to plain objects | ||||
| type IdData<T> = { id: string; data: T }; | ||||
| const toPlain = <D>(items: Array<{ id: string; data: D }> = []): Array<IdData<D>> => | ||||
|   items.map(({ id, data }) => ({ id, data })); | ||||
| export interface ILoginState { | ||||
|   identity: plugins.interfaces.data.IIdentity; | ||||
| } | ||||
| @@ -16,7 +21,7 @@ export const loginAction = loginStatePart.createAction<{ username: string; passw | ||||
|     const currentState = statePartArg.getState(); | ||||
|     let identity: plugins.interfaces.data.IIdentity = null; | ||||
|     try { | ||||
|       identity = await apiClient.loginWithUsernameAndPassword(payloadArg.username, payloadArg.password) as any; | ||||
|       identity = await apiClient.loginWithUsernameAndPassword(payloadArg.username, payloadArg.password); | ||||
|     } catch (err) { | ||||
|       console.log(err); | ||||
|     } | ||||
| @@ -102,7 +107,7 @@ export const getAllDataAction = dataState.createAction(async (statePartArg) => { | ||||
|     const secretGroups = await apiClient.secretgroup.getSecretGroups(); | ||||
|     currentState = { | ||||
|       ...currentState, | ||||
|       secretGroups: (secretGroups as any[]).map((sg: any) => ({ id: sg.id, data: sg.data })), | ||||
|       secretGroups: toPlain(secretGroups), | ||||
|     }; | ||||
|   } catch (err) { | ||||
|     console.error('Failed to fetch secret groups:', err); | ||||
| @@ -118,7 +123,7 @@ export const getAllDataAction = dataState.createAction(async (statePartArg) => { | ||||
|     const responseSecretBundles = await apiClient.secretbundle.getSecretBundles(); | ||||
|     currentState = { | ||||
|       ...currentState, | ||||
|       secretBundles: (responseSecretBundles as any[]).map((sb: any) => ({ id: sb.id, data: sb.data })), | ||||
|       secretBundles: toPlain(responseSecretBundles), | ||||
|     }; | ||||
|   } catch (err) { | ||||
|     console.error('Failed to fetch secret bundles:', err); | ||||
| @@ -134,7 +139,7 @@ export const getAllDataAction = dataState.createAction(async (statePartArg) => { | ||||
|     const images = await apiClient.image.getImages(); | ||||
|     currentState = { | ||||
|       ...currentState, | ||||
|       images: (images as any[]).map((im: any) => ({ id: im.id, data: im.data })), | ||||
|       images: toPlain(images), | ||||
|     }; | ||||
|   } catch (err) { | ||||
|     console.error('Failed to fetch images:', err); | ||||
| @@ -150,7 +155,7 @@ export const getAllDataAction = dataState.createAction(async (statePartArg) => { | ||||
|     const clusters = await apiClient.cluster.getClusters(); | ||||
|     currentState = { | ||||
|       ...currentState, | ||||
|       clusters: (clusters as any[]).map((cl: any) => ({ id: cl.id, data: cl.data })), | ||||
|       clusters: toPlain(clusters), | ||||
|     } | ||||
|   } catch (err) { | ||||
|     console.error('Failed to fetch clusters:', err); | ||||
| @@ -166,7 +171,7 @@ export const getAllDataAction = dataState.createAction(async (statePartArg) => { | ||||
|     const registries = await apiClient.externalRegistry.getRegistries(); | ||||
|     currentState = { | ||||
|       ...currentState, | ||||
|       externalRegistries: (registries as any[]).map((r: any) => ({ id: r.id, data: r.data })), | ||||
|       externalRegistries: toPlain(registries), | ||||
|     }; | ||||
|   } catch (error) { | ||||
|     console.error('Failed to fetch external registries:', error); | ||||
| @@ -182,7 +187,7 @@ export const getAllDataAction = dataState.createAction(async (statePartArg) => { | ||||
|     const services = await apiClient.services.getServices(); | ||||
|     currentState = { | ||||
|       ...currentState, | ||||
|       services: (services as any[]).map((s: any) => ({ id: s.id, data: s.data })), | ||||
|       services: toPlain(services), | ||||
|     }; | ||||
|   } catch (error) { | ||||
|     console.error('Failed to fetch services:', error); | ||||
| @@ -324,7 +329,7 @@ export const createImageAction = dataState.createAction( | ||||
|   async (statePartArg, payloadArg: { imageName: string, description: string }) => { | ||||
|     let currentState = statePartArg.getState(); | ||||
|     apiClient.identity = loginStatePart.getState().identity; | ||||
|     await apiClient.image.createImage({ name: payloadArg.imageName, description: payloadArg.description } as any); | ||||
|     await apiClient.image.createImage({ name: payloadArg.imageName, description: payloadArg.description }); | ||||
|     currentState = await dataState.dispatchAction(getAllDataAction, null); | ||||
|     return currentState; | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user