Tracardi / tracardi-gui

Tracardi ReactJs Graphical User Interface
Other
19 stars 25 forks source link

If don't have some resource show none not loading #245

Closed ryomahan closed 1 year ago

ryomahan commented 1 year ago

When we select some resource from backend, if es don't have the resource, the selector UI well always show loading that will make user think it is a network problem.

image

Replace [Loading] with [None of resource] or [an add button], maybe a good choice.

atompie commented 1 year ago

Yes I am aware o this issue. Have to figure it out as this is material-ui component.

ryomahan commented 1 year ago

@atompie I have tried some methods and now it do like the gif

Nov-11-2022 17-30-59

// 1. make loading as React State
// - const loading = open && typeof options !== "undefined" && options?.length >= 0
const [loading, setLoading] = React.useState(false)

// 2. manually control loading value in handleLoading
const handleLoading = async () => {
    if (mounted.current) {
      if (isObject(endpoint)) {
        setProgress(true)
        try {
          setOpen(true)
          // open loading before the first request
          setLoading(true)
          // simulated request delay
          await new Promise(r => setTimeout(r, 2000))
          const response = await asyncRemote(endpoint, token)
          if (response && mounted.current) {
            let options = convertResponseToAutoCompleteOptions(response)
            if (Array.isArray(defaultValueSet) && defaultValueSet.length > 0) {
              options = defaultValueSet.concat(options)
            }

            if (typeof options !== "undefined" && options !== null) {
              setOptions(options)
            } else {
              setOptions([])
            }
            setError(null)
          }
        } catch (e) {
          if (mounted.current && e) {
            const errors = getError(e)
            setError(errors[0]?.msg)
          }
        } finally {
          // close loading when all is done
          setLoading(false)
          if (mounted.current) {
            setProgress(false)
          }
        }
      } else {
        if (mounted.current && Array.isArray(defaultValueSet) && defaultValueSet.length > 0) {
          setOptions(defaultValueSet)
          setOpen(true)
        }
      }
    }
  }

// 3. add noOptionsText prop for Autocomplete component to custom no data message

Hope it's useful.

atompie commented 1 year ago

@ryomahan IT look really good. I like the option when it says no data found. Could you make it into PR. We have to go through PR process to make sure that you agree to MIT license your code.

ryomahan commented 1 year ago

Of course, I'll submit it later.

ryomahan commented 1 year ago

@atompie I submit it on #246

atompie commented 1 year ago

Done in 0.7.3