"Fossies" - the Fresh Open Source Software Archive

Member "lens-5.3.4/src/renderer/components/+namespaces/add-namespace-dialog.tsx" (19 Jan 2022, 3384 Bytes) of package /linux/misc/lens-5.3.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TSX (TypeScript with React) source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * Copyright (c) 2021 OpenLens Authors
    3  *
    4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
    5  * this software and associated documentation files (the "Software"), to deal in
    6  * the Software without restriction, including without limitation the rights to
    7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    8  * the Software, and to permit persons to whom the Software is furnished to do so,
    9  * subject to the following conditions:
   10  *
   11  * The above copyright notice and this permission notice shall be included in all
   12  * copies or substantial portions of the Software.
   13  *
   14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   20  */
   21 
   22 import "./add-namespace-dialog.scss";
   23 
   24 import React from "react";
   25 import { observable, makeObservable } from "mobx";
   26 import { observer } from "mobx-react";
   27 import { Dialog, DialogProps } from "../dialog";
   28 import { Wizard, WizardStep } from "../wizard";
   29 import { namespaceStore } from "./namespace.store";
   30 import type { Namespace } from "../../../common/k8s-api/endpoints";
   31 import { Input } from "../input";
   32 import { systemName } from "../input/input_validators";
   33 import { Notifications } from "../notifications";
   34 
   35 interface Props extends DialogProps {
   36   onSuccess?(ns: Namespace): void;
   37   onError?(error: any): void;
   38 }
   39 
   40 const dialogState = observable.object({
   41   isOpen: false,
   42 });
   43 
   44 @observer
   45 export class AddNamespaceDialog extends React.Component<Props> {
   46   @observable namespace = "";
   47 
   48   constructor(props: Props) {
   49     super(props);
   50     makeObservable(this);
   51   }
   52 
   53   static open() {
   54     dialogState.isOpen = true;
   55   }
   56 
   57   static close() {
   58     dialogState.isOpen = false;
   59   }
   60 
   61   reset = () => {
   62     this.namespace = "";
   63   };
   64 
   65   addNamespace = async () => {
   66     const { namespace } = this;
   67     const { onSuccess, onError } = this.props;
   68 
   69     try {
   70       const created = await namespaceStore.create({ name: namespace });
   71 
   72       onSuccess?.(created);
   73       AddNamespaceDialog.close();
   74     } catch (err) {
   75       Notifications.error(err);
   76       onError?.(err);
   77     }
   78   };
   79 
   80   render() {
   81     const { ...dialogProps } = this.props;
   82     const { namespace } = this;
   83     const header = <h5>Create Namespace</h5>;
   84 
   85     return (
   86       <Dialog
   87         {...dialogProps}
   88         className="AddNamespaceDialog"
   89         isOpen={dialogState.isOpen}
   90         onOpen={this.reset}
   91         close={AddNamespaceDialog.close}
   92       >
   93         <Wizard header={header} done={AddNamespaceDialog.close}>
   94           <WizardStep
   95             contentClass="flex gaps column"
   96             nextLabel="Create"
   97             next={this.addNamespace}
   98           >
   99             <Input
  100               required autoFocus
  101               iconLeft="layers"
  102               placeholder="Namespace"
  103               trim
  104               validators={systemName}
  105               value={namespace} onChange={v => this.namespace = v.toLowerCase()}
  106             />
  107           </WizardStep>
  108         </Wizard>
  109       </Dialog>
  110     );
  111   }
  112 }