Skip to content
Snippets Groups Projects
Commit 2c4204b6 authored by Yoan VALLET's avatar Yoan VALLET
Browse files

feat: add fluidStatus to context

parent c8df5edb
Branches
Tags
3 merge requests!103Support,!102Dev,!84Fetures/us160 konnector rework
......@@ -7,24 +7,18 @@ import InitDataManager from 'services/initDataManagerService'
import UserProfileDataManager from 'services/userProfileDataManagerService'
import { UserChallenge, UserProfile } from 'services/dataChallengeContracts'
import { TimeStep } from 'services/dataConsumptionContracts'
import { IFluidStatus } from 'services/fluidService'
interface AppContextProps {
isIndexesLoading: boolean
isIndexesLoadingSuccess: boolean | null
isDataLoading: boolean
isDataLoadingSuccess: boolean | null
isFluidTypesLoading: boolean
isFluidTypesLoadingSuccess: boolean | null
isContextLoaded: boolean
isError: boolean
fluidTypes: FluidType[]
fluidStatus: IFluidStatus[]
refreshFluidTypes: Function
isCurrentChallengeUpdateLoading: boolean
isCurrentChallengeUpdateLoadingSuccess: boolean | null
currentChallenge: UserChallenge | null
challengeNotification: boolean
setNotificationEcogesture: Function
refreshCurrentChallenge: Function
isContextLoaded: boolean
isError: boolean
screenType: ScreenType
previousTimeStep: TimeStep
setPreviousTimeStep: Function
......@@ -39,22 +33,15 @@ interface AppContextProps {
}
export const AppContext = React.createContext<AppContextProps>({
isIndexesLoading: false,
isIndexesLoadingSuccess: null,
isDataLoading: false,
isDataLoadingSuccess: null,
isFluidTypesLoading: false,
isFluidTypesLoadingSuccess: null,
isContextLoaded: false,
isError: false,
fluidTypes: [],
fluidStatus: [],
refreshFluidTypes: () => null,
isCurrentChallengeUpdateLoading: false,
isCurrentChallengeUpdateLoadingSuccess: null,
currentChallenge: null,
challengeNotification: false,
setNotificationEcogesture: () => null,
refreshCurrentChallenge: () => Promise,
isContextLoaded: false,
isError: false,
screenType: ScreenType.MOBILE,
previousTimeStep: TimeStep.DAY,
userProfile: null,
......@@ -78,27 +65,24 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
client,
}: AppContextProviderProps) => {
const [feedbackIsOpened, setFeedbackOpened] = useState<boolean>(false)
const [isIndexesLoading, setIndexesLoading] = useState<boolean>(false)
const [maxLoads, setMaxLoads] = useState<object>({})
const [isIndexesLoadingSuccess, setIndexesLoadingSuccess] = useState<
boolean | null
>(null)
const [isDataLoading, setDataLoading] = useState<boolean>(false)
const [isDataLoadingSuccess, setDataLoadingSuccess] = useState<
boolean | null
>(null)
const [isFluidTypesLoading, setFluidTypesLoading] = useState<boolean>(false)
const [fluidTypes, setFluidTypes] = useState<FluidType[]>([])
const [isFluidTypesLoadingSuccess, setFluidTypesLoadingSuccess] = useState<
boolean | null
>(null)
const [fluidTypes, setFluidTypes] = useState<FluidType[]>([])
const [fluidStatus, setFluidStatus] = useState<IFluidStatus[]>([])
const [isFluidStatusLoadingSuccess, setFluidStatusLoadingSuccess] = useState<
boolean | null
>(null)
const [
isCurrentChallengeUpdateLoading,
setCurrentChallengeUpdateLoading,
] = useState<boolean>(false)
const [
isCurrentChallengeUpdateLoadingSuccess,
setCurrentChallengeUpdateLoadingSuccess,
......@@ -222,10 +206,8 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
const updm = new UserProfileDataManager(client)
async function loadData() {
// Create missing indexes
setIndexesLoading(true)
const resultIndexConsoData = await im.initIndex()
if (subscribed && !resultIndexConsoData) {
setIndexesLoadingSuccess(false)
setError(true)
}
if (subscribed && resultIndexConsoData) {
......@@ -235,10 +217,8 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
)
setIndexesLoadingSuccess(true)
}
setIndexesLoading(false)
// Load challenges, ecogesture and user profile data
setDataLoading(true)
const resultInitData = await im.initData()
if (subscribed && !resultInitData) {
setDataLoadingSuccess(false)
......@@ -247,10 +227,8 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
if (subscribed && resultInitData) {
setDataLoadingSuccess(true)
}
setDataLoading(false)
// Load configured fluidTypes
setFluidTypesLoading(true)
const resultFluidTypes = await im.checkFluidTypes()
if (subscribed && resultFluidTypes) {
console.log(
......@@ -260,10 +238,20 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
setFluidTypes(resultFluidTypes)
setFluidTypesLoadingSuccess(true)
}
setFluidTypesLoading(false)
// Get fluid status
const resultFluidStatus = await im.getFluidStatus()
if (subscribed && resultFluidStatus) {
console.log(resultFluidStatus)
console.log(
'%c Context: Fluid status loaded ',
'background: #222; color: white'
)
setFluidStatus(resultFluidStatus)
setFluidStatusLoadingSuccess(true)
}
// Update current challenge if exists
setCurrentChallengeUpdateLoading(true)
try {
const resultUpdateChallenge = await im.checkCurrentChallenge()
if (subscribed && resultUpdateChallenge) {
......@@ -288,8 +276,6 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
} catch {
setCurrentChallengeUpdateLoadingSuccess(false)
setError(true)
} finally {
setCurrentChallengeUpdateLoading(false)
}
//Retrieve the UserProfile
......@@ -318,6 +304,7 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
isIndexesLoadingSuccess &&
isDataLoadingSuccess &&
isFluidTypesLoadingSuccess &&
isFluidStatusLoadingSuccess &&
isCurrentChallengeUpdateLoadingSuccess &&
isUserProfileLoadingSuccess &&
!isError
......@@ -330,6 +317,7 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
isIndexesLoadingSuccess,
isDataLoadingSuccess,
isFluidTypesLoadingSuccess,
isFluidStatusLoadingSuccess,
isCurrentChallengeUpdateLoadingSuccess,
isUserProfileLoadingSuccess,
isError,
......@@ -338,21 +326,14 @@ const AppContextProvider: React.FC<AppContextProviderProps> = ({
return (
<AppContext.Provider
value={{
isIndexesLoading,
isIndexesLoadingSuccess,
isDataLoading,
isDataLoadingSuccess,
isFluidTypesLoading,
isFluidTypesLoadingSuccess,
isContextLoaded,
isError,
fluidTypes,
fluidStatus,
refreshFluidTypes,
isCurrentChallengeUpdateLoading,
isCurrentChallengeUpdateLoadingSuccess,
currentChallenge,
challengeNotification,
refreshCurrentChallenge,
isContextLoaded,
isError,
screenType,
userProfile,
setUserProfile,
......
......@@ -330,6 +330,23 @@ export default class ConsumptionDataManager implements IConsumptionDataManager {
return lastDay
}
public async fetchAllLastDateData(
fluidTypes: FluidType[]
): Promise<(DateTime | null)[]> {
let lastDay = null
const lastDays = []
if (fluidTypes.length === 1) {
lastDay = (await this._queryRunner.getLastDateData(fluidTypes[0])) || null
lastDays.push(lastDay)
} else if (fluidTypes.length > 1) {
for (const fluidType of fluidTypes) {
lastDay = (await this._queryRunner.getLastDateData(fluidType)) || null
lastDays.push(lastDay)
}
}
return lastDays
}
public async checkDoctypeEntries(
fluideType: FluidType,
timeStep: TimeStep
......
import { FluidType } from 'enum/fluid.enum'
import { Client } from 'cozy-client'
import { DateTime } from 'luxon'
import FluidConfigService from 'services/fluidConfigService'
import KonnectorService from 'services/konnectorService'
import ConsumptionDataManager from 'services/consumptionDataManagerService'
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
export interface IFluidStatus {
fluidType: FluidType
status: string | null
lastDataDate: DateTime | null
}
export class FluidService {
private _client: Client
constructor(_client: Client) {
this._client = _client
}
public getFluidStatus = async () => {
const fluidConfig = new FluidConfigService().getFluidConfig()
const [
elecStatus,
waterStatus,
gasStatus,
lastDataDates,
] = await Promise.all([
new KonnectorService(
this._client,
fluidConfig[FluidType.ELECTRICITY].konnectorConfig.slug
).getKonnectorLastStatus(),
new KonnectorService(
this._client,
fluidConfig[FluidType.WATER].konnectorConfig.slug
).getKonnectorLastStatus(),
new KonnectorService(
this._client,
fluidConfig[FluidType.GAS].konnectorConfig.slug
).getKonnectorLastStatus(),
new ConsumptionDataManager(this._client).fetchAllLastDateData([
FluidType.ELECTRICITY,
FluidType.WATER,
FluidType.GAS,
]),
])
const result: IFluidStatus[] = [
{
fluidType: FluidType.ELECTRICITY,
status: elecStatus,
lastDataDate: lastDataDates[FluidType.ELECTRICITY],
},
{
fluidType: FluidType.WATER,
status: waterStatus,
lastDataDate: lastDataDates[FluidType.WATER],
},
{
fluidType: FluidType.GAS,
status: gasStatus,
lastDataDate: lastDataDates[FluidType.GAS],
},
]
return result
}
}
......@@ -36,6 +36,7 @@ import {
} from 'services/dataChallengeContracts'
import { FluidType } from 'enum/fluid.enum'
import { DateTime } from 'luxon'
import { FluidService, IFluidStatus } from 'services/fluidService'
export default class InitDataManager {
private readonly _client: Client
......@@ -350,6 +351,25 @@ export default class InitDataManager {
}
}
/*
* For each fluid get the trigger status and the last data date
* sucess return: FluidStatus[]
* failure return: null
*/
public async getFluidStatus(): Promise<IFluidStatus[] | null> {
const fs = new FluidService(this._client)
try {
const fluidStatus = await fs.getFluidStatus()
if (!fluidStatus) {
return null
}
return fluidStatus
} catch (error) {
console.log('Context error: ', error)
return null
}
}
/*
* Call a query with where clause to create the index if not exist
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment