|
|
@@ -9,7 +9,6 @@ import {useProfileAccessStore} from "~/store/profile/access";
|
|
|
import ApiResource from "~/models/ApiResource";
|
|
|
import {MyProfile} from "~/models/Access/MyProfile";
|
|
|
import { v4 as uuid4 } from 'uuid';
|
|
|
-import {useOrmStore} from "~/store/orm";
|
|
|
|
|
|
/**
|
|
|
* Entity manager: make operations on the models defined with the Pinia-Orm library
|
|
|
@@ -17,6 +16,8 @@ import {useOrmStore} from "~/store/orm";
|
|
|
* @see https://pinia-orm.codedredd.de/
|
|
|
*/
|
|
|
class EntityManager {
|
|
|
+ private CLONE_PREFIX = '_clone_'
|
|
|
+
|
|
|
private apiRequestService: ApiRequestService;
|
|
|
|
|
|
public constructor(apiRequestService: ApiRequestService) {
|
|
|
@@ -50,12 +51,12 @@ class EntityManager {
|
|
|
}
|
|
|
repository.save(entity)
|
|
|
|
|
|
- EntityManager.saveInitialState(model, entity)
|
|
|
+ this.saveInitialState(model, entity)
|
|
|
|
|
|
return entity
|
|
|
}
|
|
|
|
|
|
- private update(entity: ApiResource, newEntity: ApiResource) {
|
|
|
+ private reactiveUpdate(entity: ApiResource, newEntity: ApiResource) {
|
|
|
// On met à jour l'entité par référence, pour maintenir la réactivité lorsque l'entité est réactive
|
|
|
// @see http://underscorejs.org/#extend
|
|
|
useExtend(entity, newEntity)
|
|
|
@@ -125,7 +126,7 @@ class EntityManager {
|
|
|
const hydraResponse = await HydraDenormalizer.denormalize(response)
|
|
|
const returnedEntity = this.new(model, hydraResponse.data)
|
|
|
|
|
|
- EntityManager.saveInitialState(model, returnedEntity)
|
|
|
+ this.saveInitialState(model, returnedEntity)
|
|
|
|
|
|
// Save data into the store
|
|
|
repository.save(returnedEntity)
|
|
|
@@ -134,7 +135,7 @@ class EntityManager {
|
|
|
await this.refreshProfile()
|
|
|
}
|
|
|
|
|
|
- this.update(entity, returnedEntity)
|
|
|
+ this.reactiveUpdate(entity, returnedEntity)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -152,7 +153,7 @@ class EntityManager {
|
|
|
await this.apiRequestService.delete(url)
|
|
|
}
|
|
|
|
|
|
- // update the store
|
|
|
+ // reactiveUpdate the store
|
|
|
repository.destroy(entity.id)
|
|
|
}
|
|
|
|
|
|
@@ -163,21 +164,19 @@ class EntityManager {
|
|
|
* @param entity
|
|
|
*/
|
|
|
public reset(model: typeof ApiResource, entity: ApiResource) {
|
|
|
- const initialEntity = EntityManager.getInitialStateOf(model, entity.id)
|
|
|
+ const initialEntity = this.getInitialStateOf(model, entity.id)
|
|
|
if (initialEntity === null) {
|
|
|
throw new Error('no initial state recorded for this object - abort [' + model.entity + '/' + entity.id + ']')
|
|
|
}
|
|
|
|
|
|
- EntityManager.saveInitialState(model, initialEntity)
|
|
|
-
|
|
|
const repository = this.getRepository(model)
|
|
|
repository.save(initialEntity)
|
|
|
|
|
|
- this.update(entity, initialEntity)
|
|
|
+ this.reactiveUpdate(entity, initialEntity)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Re-fetch the user profile and update the store
|
|
|
+ * Re-fetch the user profile and reactiveUpdate the store
|
|
|
*/
|
|
|
public async refreshProfile() {
|
|
|
const response = await this.apiRequestService.get('api/my_profile')
|
|
|
@@ -226,8 +225,14 @@ class EntityManager {
|
|
|
* @param entity
|
|
|
* @private
|
|
|
*/
|
|
|
- private static saveInitialState(model: typeof ApiResource, entity: ApiResource) {
|
|
|
- useOrmStore().storeInitialValue(model, entity)
|
|
|
+ private saveInitialState(model: typeof ApiResource, entity: ApiResource) {
|
|
|
+ const repository = this.getRepository(model)
|
|
|
+
|
|
|
+ // Clone and prefix id
|
|
|
+ const clone = useCloneDeep(entity)
|
|
|
+ clone.id = this.CLONE_PREFIX + clone.id
|
|
|
+
|
|
|
+ repository.save(clone)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -237,16 +242,18 @@ class EntityManager {
|
|
|
* @param id
|
|
|
* @private
|
|
|
*/
|
|
|
- private static getInitialStateOf(model: typeof ApiResource, id: number): ApiResource | null {
|
|
|
- const ormStore = useOrmStore()
|
|
|
+ private getInitialStateOf(model: typeof ApiResource, id: string | number): ApiResource | null {
|
|
|
+ const repository = this.getRepository(model)
|
|
|
|
|
|
- //@ts-ignore
|
|
|
- if (!ormStore.initialValues.has(model.entity) || !ormStore.initialValues.get(model.entity).has(id)) {
|
|
|
+ // Find the clone by id
|
|
|
+ const entity = repository.find(this.CLONE_PREFIX + id)
|
|
|
+ if (entity === null) {
|
|
|
return null
|
|
|
}
|
|
|
|
|
|
- //@ts-ignore
|
|
|
- return ormStore.initialValues.get(model.entity).get(id)
|
|
|
+ // Restore the initial id
|
|
|
+ entity.id = id
|
|
|
+ return entity
|
|
|
}
|
|
|
}
|
|
|
|