frankcollins3 / Next-Water-App

Happy, Healthy Water Cycling App that tracks user/human fluid intake.
https://next-water-app.vercel.app
1 stars 0 forks source link

Promise works 0%. After Page refresh works 100% [11:41am] #100

Closed frankcollins3 closed 1 year ago

frankcollins3 commented 1 year ago

attempting to do: retrieve today-water-data corresponding to logged in user (by cookie). GraphQL query creates data if there is none for the day.

error: first page refresh: doesn't work. second page refresh works. seen: { line: 173Promises.tsx } 1 query works and the query for same data same line of code few browser logs down doesn't work

Screen Shot 2023-07-28 at 11 37 51 AM

proposed approach: these are modular functions exported by Context. Might make an abstract function and work backwards.

frankcollins3 commented 1 year ago

index.tsx -> clientside invocation setDataStatePROMISE(true, true, true, true, true, true, true)

Contexts/Promises.tsx -> async function setDataStatePROMISE(date:boolean, hydro_data:boolean, hydro_schedule:boolean, hydro_intake:boolean, status:boolean, disabled:boolean, progress:boolean):Promise {
return getDailyDataPROMISE() .then(async(dailyData:any) => { return new Promise(async(resolve:any, reject:any) => { console.log('dailyData before endpoint breakdown 190', dailyData) console.log('dailyData from the promise', dailyData)
dailyData = dailyData.data.data.getDailyData
console.log('dailyData 192', dailyData) let date = dailyData.date
if (date) dispatch(SET_DATE(dailyData.date)) if (hydro_data) dispatch(SET_HYDRO_DATA(dailyData)) if (status) dispatch(SET_STATUS(dailyData.status)) if (progress) dispatch(SET_PROGRESS(dailyData.progress))

        if (hydro_intake) {
            const waterintake:any = await userSettingsIntakePROMISE()
            console.log('waterintake', waterintake)
            dispatch(SET_HYDRO_INTAKE(waterintake))
        }
        if (hydro_schedule) {
            const userDailyWaterSchedule = await userSettingsSchedulePROMISE()
            let scheduleLength:number = userDailyWaterSchedule.length;                
            dispatch(SET_HYDRO_SCHEDULE(userDailyWaterSchedule))

            if (disabled) {
                dispatch(SET_DISABLED(Array(scheduleLength).fill(false)))
            }
            // console.log('schedule in promise', userDailyWaterSchedule)
        }           
        resolve([{hydro_data: `${HYDRO_DATA}`, hydro_schedule: `${HYDRO_SCHEDULE}`, hydro_intake: `${HYDRO_INTAKE}`, date: `${DATE}`, status: `${STATUS}`, disabled: `${DISABLED}`}])            
        reject("error")
    })
})
}

current approach is manually calling those functions 1 by 1 which set @redux/toolkit state: SETTINGS, WATER_DATA, HYDRO_INTAKE, STATUS, etc...

[11:43am]

frankcollins3 commented 1 year ago

issue 100 🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂

frankcollins3 commented 1 year ago

this approach would essentially be a separation of concerns issue: can see the data being there and then the lower promise saying it isn't there.

Screen Shot 2023-07-28 at 11 51 40 AM

[11:51am]

frankcollins3 commented 1 year ago

can do the same thing with the clientside invocation with Promise.all() and have separate promises for each still expressed with 1 line of code.

The one that will be left out of that Promise.all() will be getDailyData because the endpoints are needed then and there.

oh and so the same for CURRENT_USER for the user icon needed immediately in that index.tsx effect

[11:53am]

frankcollins3 commented 1 year ago

"People hate going up the mountain. seeing the peak and realizing to get there they have to go all the way down."

// took some hours setting up Promises so true

frankcollins3 commented 1 year ago

hours until out of woods but this works in client:

    iPROMISEcookies()
    .then( (cookie) => {
      console.log('cookie', cookie)
      axios.post('/api/graphql', {
        query: `query {
          allUserData(users_id: ${cookie}) {
            id
            google_id
            date
            progress
            weekday
            status
            users_id
          }
        }`,
      }).then( (userdata) => {
        console.log('userdata', userdata)
      })

    })

cookie: chrome-dev-tools -> application -> cookies ->

Screen Shot 2023-07-28 at 12 10 32 PM

[12:10pm]

frankcollins3 commented 1 year ago

HUMAN ERROR! I was doing this when there was only piece of userdata, now that there's multiple, need a .find() to check for strict equality between date in psql data and current calendar date from JS constructor new Date() [12:23pm]

frankcollins3 commented 1 year ago

debating whether a new GraphQL query or a new Promise is better to handle this. // have done enough queries and want to be better at retrieving app PROMISES through useContext() [12:39pm

frankcollins3 commented 1 year ago

there already is such a query.. looking more into things. [12:42pm]

frankcollins3 commented 1 year ago

autoIncrement()