jeudi 5 janvier 2023

React/Redux component with checkboxes does not update when click on checkbox even though I'm returning new state

I've been stuck for a while trying to make the re-render in the checkboxes to work, the variables are being updated but it's just the rendering that doesn't happen.

I'm receiving a response from the backend that contains an object with an array of steps, I'm going to render a checkbox for every step if it's from a specific type. As soon as I received the object, I add in every step a new property value to use it later for checking the checkboxes.

This is my reducer:

export const MyObject = (state: MyObject = defaultState, action: FetchMyObjectAction | UpdateStepsInMyObjectAction) => {
switch (action.type) {
    case "FETCH_MYOBJECT":
        return {
            ...action.payload, // MyObject
            steps: action.payload.steps.map((step) => {
                if (step.control.controlType === "1") { // "1" = checkbox
                    return {
                        ...step,
                        value: step.control.defaultValues[0] === "true" ? true : false, // Adding the property value
                    };
                }
                return step;
            }),
        };
    case "UPDATE_STEPS":
        return {
            ...state,
            steps: state.steps.map((step) => {
                if (step.id === action.payload.stepId) { // if this is the checkbox to update
                    return {
                        ...step,
                        value: action.payload.checked,
                    };
                }
                return step;
            }),
        };
    default:
        return state;
}

This is how I'm rendering the checkboxes:

 for (let step of steps) {
    if (step.control.controlType === "1") {
       controls.push(
            <Checkbox
                label={step.displayName}
                checked={step.value}
                onChange={(_ev, checked) => {
                    callback(step.id, checked);
                }}
                disabled={false}
                className={classNames.checkbox}
            />
        );
    }
}

callback is a function that calls the reducer above for the case "UPDATE_STEPS".

After inspecting the variables I can see that they are being updated properly, it's just that the re-render doesn't happen in the checkboxes, not even the first time I check the box, the check doesn't appear. If I move to a different component and then go back to the component with the checkboxes I can see now the checks. But if I check/uncheck within the same component, nothing happens visually.

As far as I know, I'm returning new objects for every update, so mutability is not happening. Can you see what I'm missing?

Thanks!




How to validate a checkbox is checked or not and then performing some action in Cypress?

<div class="ag-react-container"><div class="text-center"><input type="checkbox" class="" checked=""></div></div>
cy.("#checkbox").then(($ele) => {
   if($ele.next().is(':checked')){
      cy.("#checkbox").next().should("be.checked");
     }
   else {
     cy.("#checkbox").next().find("input").check();
     cy.("#checkbox").next().find("input").should("be.checked");
     }
  });

My problem is the code above is not going into if condition and keeps on executing else condition.

Learning cypress so code can be wrong and would expect correct full code along with some more examples of same if else condition check for buttons, checkboxes or elements.




mercredi 4 janvier 2023

Multiple userform checkbox values

I am trying to take the values passed from a userform that has the following checkbox options and write them to a single concatenated cell.

Scenario Options and Userform Output Cells

Scenario Options and Userform Output Cells

userform

userform

Thank you for your help. Any suggestions would be greatly appreciated. I currently have attempted this, but can only get one option to show up at a time.

Private Sub CheckBox1_Click()

If Me.CheckBox1.Value = True Then
    Me.Frame2.Visible = True
    Sheets("Project Analysis - Summary").Range("D7") = 1
Else
    Me.Frame2.Visible = False
End If

End Sub

Private Sub CheckBox2_Click()

If Me.CheckBox2.Value = True Then
    Me.Frame3.Visible = True
    Sheets("Project Analysis - Summary").Range("D8") = 1
Else
    Me.Frame3.Visible = False
End If

End Sub

Private Sub CheckBox3_Click()

If Me.CheckBox3.Value = True Then
    Me.Frame4.Visible = True
    Sheets("Project Analysis - Summary").Range("D9") = 1
Else
    Me.Frame4.Visible = False
End If

End Sub

Private Sub CheckBox5_Click()

If Me.CheckBox5.Value = True Then
    Me.Frame6.Visible = True
    Sheets("Project Analysis - Summary").Range("D10") = 1
Else
    Me.Frame6.Visible = False
End If

End Sub

Private Sub CheckBox6_Click()

If Me.CheckBox6.Value = True Then
    Me.Frame5.Visible = True
    Sheets("Project Analysis - Summary").Range("D11") = 1
Else
    Me.Frame5.Visible = False
End If

End Sub

Private Sub CommandButton1_Click()

Dim ctrl1 As Control

Set ctrl1 = Nothing
    
For Each ctrl1 In Me.Frame2.Controls
    If TypeName(ctrl1) = "OptionButton" And ctrl1.Value = True Then
        Sheets("Project Analysis - Summary").Range("E7") = ctrl1.Caption
    End If
Next

Dim ctrl2 As Control

Set ctrl2 = Nothing
    
For Each ctrl2 In Me.Frame3.Controls
    If TypeName(ctrl2) = "OptionButton" And ctrl2.Value = True Then
        Sheets("Project Analysis - Summary").Range("E8") = ctrl2.Caption
    End If
Next

Dim ctrl3 As Control

Set ctrl3 = Nothing
    
For Each ctrl3 In Me.Frame4.Controls
    If TypeName(ctrl3) = "OptionButton" And ctrl3.Value = True Then
        Sheets("Project Analysis - Summary").Range("E9") = ctrl3.Caption
    End If
Next

Dim ctrl4 As Control

Set ctrl4 = Nothing
    
For Each ctrl4 In Me.Frame5.Controls
    If TypeName(ctrl4) = "OptionButton" And ctrl4.Value = True Then
        Sheets("Project Analysis - Summary").Range("E10") = ctrl4.Caption
    End If
Next

Dim ctrl5 As Control

Set ctrl5 = Nothing
    
For Each ctrl5 In Me.Frame6.Controls
    If TypeName(ctrl5) = "OptionButton" And ctrl5.Value = True Then
        Sheets("Project Analysis - Summary").Range("E11") = ctrl5.Caption
    End If
Next

End Sub


Private Sub UserForm_Initialize()

Me.Frame2.Visible = False
Me.Frame3.Visible = False
Me.Frame4.Visible = False
Me.Frame5.Visible = False
Me.Frame6.Visible = False
Sheets("Project Analysis - Summary").Range("D7") = ""
Sheets("Project Analysis - Summary").Range("D8") = ""
Sheets("Project Analysis - Summary").Range("D9") = ""
Sheets("Project Analysis - Summary").Range("D10") = ""
Sheets("Project Analysis - Summary").Range("D11") = ""
Sheets("Project Analysis - Summary").Range("E7") = ""
Sheets("Project Analysis - Summary").Range("E8") = ""
Sheets("Project Analysis - Summary").Range("E9") = ""
Sheets("Project Analysis - Summary").Range("E10") = ""
Sheets("Project Analysis - Summary").Range("E11") = ""

End Sub



React native checkbox

I would like to use checkBox in my react nativem mobile app. I'm trying to using this community package :

https://github.com/react-native-checkbox/react-native-checkbox

But I got this error when I'm trying to use it in my app :

ERROR  [Invariant Violation: requireNativeComponent: "RNCCheckbox" was not found in the UIManager.]

I correctly install the package but there is probably another problem.. There is my code :

 <CheckBox
     disabled={false}
     value={selectedItems.includes(item)}
     onValueChange={() => toggleSelection(item)}
 />

If someone could explain me another solutions or explain me where is the problem.




mardi 3 janvier 2023

Checkbox or dropdown with Plotly.JS?

I know that Dash provides checkboxes / dropdowns (via dcc, Dash Core Components, see https://dash.plotly.com/dash-core-components).

But how to create a checkbox in the context of a pure Plotly.JS plot, without dash?




lundi 2 janvier 2023

Setting a future day of repair depending on warranty selection

First of all I'm really new to Web Dev, just learnt basics of JS last week.

I have a project in which I have a VIP status, and 3 warranty types. If the client is VIP -> fixing the issue will take up to 7 days. Then I have 3 types of warranties - None, Regular, Extended. None - up to a month of repair. Regular - up to 3 weeks of repair. Extended - up to 2 weeks of repair.

The solution must include ONLY javascript (no jQuery yet). since the VIP checkbox isn't a part of the warranty selection, I'm having trouble connecting them.

In case my explanation confuses you - if the client is VIP - warranty options should be DISABLED. since it automatically gets a faster service. if VIP checkbox isn't checked - I need to show the time of repair as shown above.

Thanks in advance

 <input type="checkbox" id="isVIP" />VIP?
<select name="warranty" id="warranty">
          <option disabled selected>Select warranty</option>
          <option value="none">None</option>
          <option value="regular">Regular</option>
          <option value="Extended">Extended</option>



Some problems about live data

In sign up fragment, I have term condition part. When you click this text, bottom sheets opens. I click the button that end of the page(Accept Button). I want checkbox in sign up should be clicked.

Repository

  var resultOk = MutableLiveData<Boolean>()
  fun checkResult(){
    resultOk.value = true
}

Sign Up View Model

`
private var _resultOk = MutableLiveData<Boolean>()
val resultOk: LiveData<Boolean>
    get() = _resultOk

Sign Up Fragment

      viewModel.resultOk.observe(viewLifecycleOwner, Observer {
        binding.cbTermsAndCondition.isChecked = it
    })

BottomSheetView Model

  init {
    checkResult()
}

fun checkResult() {
    fireBaseRepository.checkResult()
}

BottomSheet Fragment

     binding.btnTermAccept.setOnClickListener {
        //Constant.result.value = true
        //viewModel.resultOk.value
        viewModel.checkResult()
        dismiss()
    }

Why checkbox is not clicked, when I click "accept button" end of the bottomsheet.