LCOV - code coverage report
Current view: top level - pkg/users - shell-dialog.js Coverage Total Hit
Test: cockpit Lines: 93.8 % 65 61
Test Date: 2026-06-25 09:20:42

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2023 Red Hat, Inc.
       3              :  * SPDX-License-Identifier: LGPL-2.1-or-later
       4              :  */
       5              : 
       6           19 : import cockpit from 'cockpit';
       7           19 : import React from 'react';
       8              : import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js";
       9              : import { FormSelect, FormSelectOption } from "@patternfly/react-core/dist/esm/components/FormSelect/index.js";
      10              : 
      11              : import { has_errors } from "./dialog-utils.js";
      12              : import { show_modal_dialog, apply_modal_dialog } from "cockpit-components-dialog.jsx";
      13              : 
      14           19 : const _ = cockpit.gettext;
      15              : 
      16            1 : function ShellDialogBody({ state, errors, change, shells }) {
      17            1 :     const { shell } = state;
      18            1 :     return (
      19            1 :         <Form className="shell-modal" onSubmit={apply_modal_dialog}>
      20            1 :             <FormGroup fieldId="edit-user-shell">
      21            1 :                 <FormSelect
      22            1 :                         data-selected={shell}
      23            1 :                         id="edit-user-shell"
      24            1 :                         onChange={(_, selection) => { change("shell", selection) }}
      25            1 :                         value={shell}>
      26            1 :                     { shells.map(shell_path => <FormSelectOption key={shell_path} value={shell_path} label={shell_path} />) }
      27            1 :                 </FormSelect>
      28            1 :             </FormGroup>
      29            1 :         </Form>
      30              :     );
      31            1 : }
      32              : 
      33            1 : export function account_shell_dialog(account, shells) {
      34            1 :     let dlg = null;
      35              : 
      36            1 :     const state = {
      37            1 :         shell: account.shell,
      38            1 :     };
      39              : 
      40            1 :     let errors = { };
      41              : 
      42            1 :     function change(field, value) {
      43            1 :         state[field] = value;
      44            1 :         update();
      45            1 :     }
      46              : 
      47            1 :     function validate() {
      48            1 :         errors = { };
      49              : 
      50            1 :         return !has_errors(errors);
      51            1 :     }
      52              : 
      53            1 :     function update() {
      54            1 :         const props = {
      55            1 :             id: "shell-dialog",
      56            1 :             title: _("Change shell"),
      57            1 :             body: <ShellDialogBody state={state} errors={errors} change={change} shells={shells} />,
      58            1 :             variant: "small"
      59            1 :         };
      60              : 
      61            1 :         const footer = {
      62            1 :             actions: [
      63            1 :                 {
      64            1 :                     caption: _("Change"),
      65            1 :                     style: "primary",
      66            1 :                     clicked: () => {
      67            1 :                         if (validate()) {
      68            1 :                             return cockpit.spawn(["usermod", "--shell", state.shell, account.name],
      69            1 :                                                  { superuser: "require", err: "message" });
      70            0 :                         } else {
      71            0 :                             update();
      72            0 :                             return Promise.reject();
      73            0 :                         }
      74            1 :                     }
      75            1 :                 }
      76            1 :             ]
      77            1 :         };
      78              : 
      79            1 :         if (!dlg)
      80            1 :             dlg = show_modal_dialog(props, footer);
      81            1 :         else {
      82            1 :             dlg.setProps(props);
      83            1 :             dlg.setFooterProps(footer);
      84            1 :         }
      85            1 :     }
      86              : 
      87            1 :     update();
      88            1 : }
        

Generated by: LCOV version 2.0-1