|
|
|
@ -10,7 +10,7 @@ import {
|
|
|
|
|
LockCargoBikeGQL,
|
|
|
|
|
LockCargoBikeMutationVariables,
|
|
|
|
|
UnlockCargoBikeGQL,
|
|
|
|
|
UnlockCargoBikeMutationVariables
|
|
|
|
|
UnlockCargoBikeMutationVariables,
|
|
|
|
|
} from 'src/generated/graphql';
|
|
|
|
|
import { DeepExtractTypeSkipArrays } from 'ts-deep-extract-types';
|
|
|
|
|
|
|
|
|
@ -24,6 +24,7 @@ export type CargoBikeResult = DeepExtractTypeSkipArrays<
|
|
|
|
|
})
|
|
|
|
|
export class BikesService {
|
|
|
|
|
bikes: BehaviorSubject<CargoBikeResult[]> = new BehaviorSubject([]);
|
|
|
|
|
loadingRowIds: BehaviorSubject<string[]> = new BehaviorSubject([]);
|
|
|
|
|
groupEnum: BehaviorSubject<string[]> = new BehaviorSubject<string[]>([]);
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@ -32,58 +33,94 @@ export class BikesService {
|
|
|
|
|
private updateCargoBikeGQL: UpdateCargoBikeGQL,
|
|
|
|
|
private lockCargoBikeGQL: LockCargoBikeGQL,
|
|
|
|
|
private unlockCargoBikeGQL: UnlockCargoBikeGQL
|
|
|
|
|
) { }
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
addLoadingRowId(id: string) {
|
|
|
|
|
this.loadingRowIds.next([...this.loadingRowIds.value, id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeLoadingRowId(id: string) {
|
|
|
|
|
this.loadingRowIds.value.forEach((item, index) => {
|
|
|
|
|
if (item === id) {
|
|
|
|
|
this.loadingRowIds.value.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadBikes() {
|
|
|
|
|
this.getCargoBikesGQL.fetch().subscribe((result) => {
|
|
|
|
|
this.bikes.next(result.data.cargoBikes);
|
|
|
|
|
let enumValues = result.data.__type.enumValues.map(value => value.name);
|
|
|
|
|
let enumValues = result.data.__type.enumValues.map((value) => value.name);
|
|
|
|
|
this.groupEnum.next(enumValues);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reloadBike(variables: GetCargoBikeByIdQueryVariables) {
|
|
|
|
|
this.getCargoBikeByIdGQL.fetch(variables).subscribe((result) => {
|
|
|
|
|
const newBike = result.data.cargoBikeById;
|
|
|
|
|
this.bikes.next(
|
|
|
|
|
this.bikes.value.map((bike) =>
|
|
|
|
|
newBike.id === bike.id ? newBike : bike
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
this.addLoadingRowId(variables.id);
|
|
|
|
|
this.getCargoBikeByIdGQL
|
|
|
|
|
.fetch(variables)
|
|
|
|
|
.subscribe((result) => {
|
|
|
|
|
const newBike = result.data.cargoBikeById;
|
|
|
|
|
this.bikes.next(
|
|
|
|
|
this.bikes.value.map((bike) =>
|
|
|
|
|
newBike.id === bike.id ? newBike : bike
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.add(() => {
|
|
|
|
|
this.removeLoadingRowId(variables.id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateBike(variableValues: UpdateCargoBikeMutationVariables) {
|
|
|
|
|
this.updateCargoBikeGQL.mutate(variableValues).subscribe((result) => {
|
|
|
|
|
const newBike = result.data.updateCargoBike;
|
|
|
|
|
this.bikes.next(
|
|
|
|
|
this.bikes.value.map((bike) =>
|
|
|
|
|
newBike.id === bike.id ? newBike : bike
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
updateBike(variables: UpdateCargoBikeMutationVariables) {
|
|
|
|
|
this.addLoadingRowId(variables.bike.id);
|
|
|
|
|
this.updateCargoBikeGQL
|
|
|
|
|
.mutate(variables)
|
|
|
|
|
.subscribe((result) => {
|
|
|
|
|
const newBike = result.data.updateCargoBike;
|
|
|
|
|
this.bikes.next(
|
|
|
|
|
this.bikes.value.map((bike) =>
|
|
|
|
|
newBike.id === bike.id ? newBike : bike
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.add(() => {
|
|
|
|
|
this.removeLoadingRowId(variables.bike.id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lockBike(variables: LockCargoBikeMutationVariables) {
|
|
|
|
|
this.lockCargoBikeGQL.mutate(variables).subscribe((result) => {
|
|
|
|
|
const lockedBike = result.data.lockCargoBike;
|
|
|
|
|
this.bikes.next(
|
|
|
|
|
this.bikes.value.map((bike) =>
|
|
|
|
|
lockedBike.id === bike.id ? lockedBike : bike
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
this.addLoadingRowId(variables.id);
|
|
|
|
|
this.lockCargoBikeGQL
|
|
|
|
|
.mutate(variables)
|
|
|
|
|
.subscribe((result) => {
|
|
|
|
|
const lockedBike = result.data.lockCargoBike;
|
|
|
|
|
this.bikes.next(
|
|
|
|
|
this.bikes.value.map((bike) =>
|
|
|
|
|
lockedBike.id === bike.id ? lockedBike : bike
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.add(() => {
|
|
|
|
|
this.removeLoadingRowId(variables.id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unlockBike(variables: UnlockCargoBikeMutationVariables) {
|
|
|
|
|
this.unlockCargoBikeGQL.mutate(variables).subscribe((result) => {
|
|
|
|
|
const unlockedBike = result.data.unlockCargoBike;
|
|
|
|
|
this.bikes.next(
|
|
|
|
|
this.bikes.value.map((bike) =>
|
|
|
|
|
unlockedBike.id === bike.id ? unlockedBike : bike
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
this.addLoadingRowId(variables.id);
|
|
|
|
|
this.unlockCargoBikeGQL
|
|
|
|
|
.mutate(variables)
|
|
|
|
|
.subscribe((result) => {
|
|
|
|
|
const unlockedBike = result.data.unlockCargoBike;
|
|
|
|
|
this.bikes.next(
|
|
|
|
|
this.bikes.value.map((bike) =>
|
|
|
|
|
unlockedBike.id === bike.id ? unlockedBike : bike
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.add(() => {
|
|
|
|
|
this.removeLoadingRowId(variables.id);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
relockBike(variables: LockCargoBikeMutationVariables) {
|
|
|
|
|