1- import React , { useCallback , useState } from 'react' ;
1+ import React from 'react' ;
22import { observer } from 'mobx-react-lite' ;
3- import * as LIT from 'types/generated/lit-sessions_pb' ;
43import styled from '@emotion/styled' ;
54import { usePrefixedTranslation } from 'hooks' ;
6- import { MAX_DATE } from 'util/constants' ;
75import { useStore } from 'store' ;
86import { Button , Column , HeaderFour , Row } from 'components/base' ;
97import FormField from 'components/common/FormField' ;
@@ -42,31 +40,11 @@ interface Props {
4240
4341const AddSession : React . FC < Props > = ( { primary } ) => {
4442 const { l } = usePrefixedTranslation ( 'cmps.connect.AddSession' ) ;
45- const { sessionStore } = useStore ( ) ;
46-
47- const [ label , setLabel ] = useState ( '' ) ;
48- const [ permissions , setPermissions ] = useState ( 'admin' ) ;
49- const [ editing , setEditing ] = useState ( false ) ;
50-
51- const toggleEditing = useCallback ( ( ) => setEditing ( e => ! e ) , [ ] ) ;
52- const handleSubmit = useCallback ( async ( ) => {
53- const sessionType =
54- permissions === 'admin'
55- ? LIT . SessionType . TYPE_MACAROON_ADMIN
56- : LIT . SessionType . TYPE_MACAROON_READONLY ;
57-
58- const session = await sessionStore . addSession ( label , sessionType , MAX_DATE , true ) ;
59-
60- if ( session ) {
61- setLabel ( '' ) ;
62- setEditing ( false ) ;
63- }
64- } , [ label , permissions ] ) ;
65-
43+ const { addSessionView } = useStore ( ) ;
6644 const { Wrapper, FormHeader, FormInput, FormSelect } = Styled ;
67- if ( ! editing ) {
45+ if ( ! addSessionView . editing ) {
6846 return (
69- < PurpleButton tertiary = { ! primary } onClick = { toggleEditing } >
47+ < PurpleButton tertiary = { ! primary } onClick = { addSessionView . toggleEditing } >
7048 { l ( 'create' ) }
7149 </ PurpleButton >
7250 ) ;
@@ -85,24 +63,33 @@ const AddSession: React.FC<Props> = ({ primary }) => {
8563 < Row >
8664 < Column cols = { 6 } >
8765 < FormField >
88- < FormInput value = { label } onChange = { setLabel } placeholder = { l ( 'labelHint' ) } />
66+ < FormInput
67+ value = { addSessionView . label }
68+ onChange = { addSessionView . setLabel }
69+ placeholder = { l ( 'labelHint' ) }
70+ />
8971 </ FormField >
9072 </ Column >
9173 < Column >
9274 < FormField >
9375 < FormSelect
94- value = { permissions }
95- onChange = { setPermissions }
76+ value = { addSessionView . permissionType }
77+ onChange = { addSessionView . setPermissionType }
9678 options = { [
97- { label : 'Admin' , value : 'admin' } ,
98- { label : 'Read Only' , value : 'read-only' } ,
79+ { label : l ( 'admin' ) , value : 'admin' } ,
80+ { label : l ( 'readonly' ) , value : 'read-only' } ,
81+ { label : l ( 'custom' ) , value : 'custom' } ,
9982 ] }
10083 />
10184 </ FormField >
10285 </ Column >
10386 < Column >
104- < PurpleButton onClick = { handleSubmit } > { l ( 'common.submit' ) } </ PurpleButton >
105- < Button ghost borderless onClick = { toggleEditing } >
87+ < PurpleButton onClick = { addSessionView . handleSubmit } >
88+ { addSessionView . permissionType === 'custom'
89+ ? l ( 'common.next' )
90+ : l ( 'common.submit' ) }
91+ </ PurpleButton >
92+ < Button ghost borderless onClick = { addSessionView . toggleEditing } >
10693 { l ( 'common.cancel' ) }
10794 </ Button >
10895 </ Column >
0 commit comments