Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial action of Datasource and custom code #380

Closed
nimaforoughi opened this issue Mar 23, 2024 · 3 comments
Closed

Initial action of Datasource and custom code #380

nimaforoughi opened this issue Mar 23, 2024 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@nimaforoughi
Copy link
Sponsor

Hello,
Is there any sample code for initial ‘custom code’ section to read data from Postgres db table(the payload is taken from ‘initial Request Payload) and update the panel elements values accordingly?

Your configuration.sql example would be my preference.

The documentations only have custom code for ‘Query’ as the initial acton and nothing for ‘Datasource’

regards
Nima

@mikhail-vl mikhail-vl added the question Further information is requested label Apr 1, 2024
@vitPinchuk
Copy link
Contributor

Hi, @nimaforoughi
In documentation we have description for Datasource witth initial custom code
1)https://volkovlabs.io/plugins/volkovlabs-form-panel/data-flow/#data-source
2) https://volkovlabs.io/plugins/volkovlabs-form-panel/data-flow/#initial-request-custom-code
and reference to code snipets here
Let us know if this solves your problem or not.

@nimaforoughi
Copy link
Sponsor Author

thank you Vit,
But the page you are talking about only has a screenshot for my type of question which is not working. That’s why I am looking for a working snip of the code.

I have written the below code to read the response from postgres Datasource and parses the id and values then put it the payload.

my payload finally will be like an object with key and values.
But nothing shows on the panel elements, but they turn red only.
The keys of the payload are exactly matching the elements ids

the elements in the panel are multi select.

Can you please assist to diagnose it.

const payload = {};


elements.forEach((element) => {
  if (!element.value) {
    console.log("no value", element.id)
    payload[element.id] = element.value;
    return;
  }
  console.log('found')
  payload[element.id] = element.value;
})


console.log('payload', payload)
// setInitial(payload)


// Get the first frame from the response
let frame = response.data.results.A.frames[0];
// Iterate over the fields in the response
frame.schema.fields.forEach((field, index) => {
  // If the field name exists in the payload
  if (payload.hasOwnProperty(field.name)) {
    // Assign the corresponding values to the payload
    payload[field.name] = frame.data.values[index];
  }
});


console.log('payload after analysis', payload);


setInitial(payload)
console.log('done')
return;

@vitPinchuk vitPinchuk self-assigned this May 13, 2024
@vitPinchuk
Copy link
Contributor

Hi, @nimaforoughi
setInitial(payload) sets the initial value but does not change the value in the element.
Therefore, when updating and using setInitial, the values in the elements are not changed, but the initial state is changed.

Exmaple:
image

and Custom Code
I hardcoded testPayload for an example of what happens (in your code sample the same thing happens)

image

After that my form elements highlights in red color, which shows that the value of the element is different from the initialial value

image

when I click on submit button I see my initial values

image

But I believe your goal is to set values for the elements
I made a new panel. I added three multi select elements. Added options for the sample. And set up an initial query.

image

Put some code for initial code and use onChangeElements. onChangeElements - set up values on elements.
After adding the code you will see that the values obtained from response will be set.

const contextElements = context.panel.elements

if (context.panel.response) {
  const frame = context.panel.response
    .data.results.A.frames[0]

  // map values from response 
  const framevalues = frame.schema.fields.map((value, index) => ({
    field: value.name,
    value: frame.data.values[index]
  }))

// new Elements with values
  const newElements = contextElements.map(element => {
    const frameElement = framevalues.find(frElem => frElem.field === element.id)
    return ({
      ...element,
      value: frameElement.value[0]
    })

    
  })
  console.log('newElements ', newElements)
  // use onChangeElements
  context.panel.onChangeElements(newElements)
}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

4 participants