Skip to content

Commit

Permalink
MultiInput EdOrgs for the Application Form
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhammadasif committed Dec 19, 2024
1 parent 149c6b5 commit 9e0d3bc
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 52 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@chakra-ui/icons": "2.1.1",
"@chakra-ui/react": "2.8.2",
"@choc-ui/chakra-autocomplete": "^5.8.0",
"@edfi/admin-console-shared-sdk": "2.2.10-alpha.12",
"@emotion/react": "11.11.3",
"@emotion/styled": "11.11.0",
Expand Down
173 changes: 173 additions & 0 deletions src/components/MultiInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import {
Flex, FormControl,
FormLabel,
Tag
} from '@chakra-ui/react'
import {
AutoComplete,
AutoCompleteCreatable,
AutoCompleteInput,
AutoCompleteList,
AutoCompleteTag
} from '@choc-ui/chakra-autocomplete'
import {
isArray,
isFunction,
sortedUniq
} from 'lodash-es'
import {
KeyboardEventHandler,
useEffect, useReducer
} from 'react'

interface MultiInputProps<T> {
values: T[]
onChange: (vals: T[]) => void
transformText?: (text: string) => T
filterInput?: KeyboardEventHandler<HTMLInputElement>
label?: string
fieldName?: string
}

function MultiInput<T extends string | number>({ filterInput, fieldName, label, values: initialVals, onChange, transformText }: MultiInputProps<T>) {
const [ snapshot, setSnapshot ] = useReducer((state, action) => JSON.stringify(state) === JSON.stringify(action) ? state : action, {})

const [ values, dispatch ] = useReducer((state, action: {type: 'set' | 'add' | 'rm', data: string | string[]}) => {
let arr = [ ...state ]

let data = isFunction(transformText)
? isArray(action.data)
? action.data.map(p => transformText(p))
: transformText(action.data)
: action.data as T

if(action.type === 'rm') {
arr = arr.filter(e => e !== data)
onChange(arr)
}

if(data === 0) {
return arr
}

if(action.type === 'add') {
arr.push(data)
onChange(arr)
}


if(action.type === 'set') {
arr = isArray(data) ? [ ...data ] : [ data ]
onChange(arr)
}

arr = sortedUniq(arr)
return arr
}, [])

function addVal(data: string) {
dispatch({
type: 'add',
data
})

}

function rmVal(data: string) {
dispatch({
type: 'rm',
data
})
}

useEffect(() => {
console.log('🚘 BEFORE: setting initial vals', initialVals, snapshot)
if(snapshot === JSON.stringify(initialVals)) {
return
}

if(!isArray(initialVals)) {
return
}

if(initialVals.length === 0) {
return
}

console.log('🚘 setting initial vals', initialVals)


dispatch({
type: 'set',
data: initialVals as string[]
})

setSnapshot(JSON.stringify(initialVals))

}, [ initialVals ])

return (
<Flex
align="center"
direction="column"
justify="center"
w="full"
>
<FormControl
id={fieldName}
w="full"
>
<FormLabel
fontFamily='Poppins'
fontSize='14px'
fontWeight='700'
htmlFor={fieldName}
lineHeight='20px'
>{label}
</FormLabel>

<AutoComplete
closeOnSelect
creatable
focusInputOnSelect
multiple
defaultValues={values}
id={fieldName}
openOnFocus={false}
suggestWhenEmpty={false}
onSelectOption={({ item }) => addVal(item.value)}
onTagRemoved={rmVal}
>
<AutoCompleteInput
enterKeyHint='enter'
placeholder='Add EdOrg IDs...'
size='xs'
variant="filled"
onKeyUp={filterInput}
>
{values.map((tag, tid) => <AutoCompleteTag
key={tid}
label={tag}
variant='solid'
onRemove={() => rmVal(tag)}
/>)}
</AutoCompleteInput>


<AutoCompleteList fontSize={10}>
<AutoCompleteCreatable>
{({ value }) => (<Flex gap={2}>Add <Tag
paddingX={2}
size="xs"
>{isFunction(transformText)? transformText?.(value) : value}
</Tag> as option...
</Flex>)}
</AutoCompleteCreatable>
</AutoCompleteList>
</AutoComplete>
</FormControl>
</Flex>
)
}

export default MultiInput
45 changes: 35 additions & 10 deletions src/components/common/Instance/ApplicationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { Flex } from '@chakra-ui/react'
import {
CompleteFormErrorMessage, UserProfile, UserProfileContext
} from '@edfi/admin-console-shared-sdk'
import { useContext } from 'react'
import {
useContext, useEffect
} from 'react'
import { EdfiApplication } from '../../../core/Edfi/EdfiApplications'
import { ODSInstance } from '../../../core/ODSInstance.types'
import useApplicationForm from '../../../hooks/adminActions/edfi/useApplicationForm'
import useTenantInfo from '../../../hooks/useTenantInfo'
import MultiInput from '../../MultiInput'
import ApplicationAPIFormSection from './ApplicationAPIFormSection'
import ApplicationDetailsFormSection from './ApplicationDetailsFormSection'
import EdFiModalForm from './EdFiModalForm'
import LocalEducationAgenciesFormSection from './LocalEducationAgenciesFormSection'

interface ApplicationFormProps {
instance: ODSInstance | null
Expand All @@ -32,10 +34,12 @@ const ApplicationForm = ({ instance, mode, editApplicationData, onFinishSave }:
errors,
onRegenerateCredentials,
onChangeInput,
onToggleOrgId,
onSelectClaim,
onSelectVendor,
onSave
onSave,
transformText,
setEdorgs,
edOrgs
} = useApplicationForm({
instanceId: instance?.id ?? 0,
mode,
Expand Down Expand Up @@ -72,6 +76,27 @@ const ApplicationForm = ({ instance, mode, editApplicationData, onFinishSave }:
return '...'
}

useEffect(() => {
if(applicationData.educationOrganizationIds){
console.log('setting edorgs', applicationData.educationOrganizationIds)
setEdorgs(applicationData.educationOrganizationIds)
}
}, [ applicationData ])

// setEdorgs
function isNumberKey(evt) {
if(evt.target.value) {
evt.target.value = evt.target.value.replace(/[^0-9]/g, '')
}
// var charCode = (evt.which) ? evt.which : evt.keyCode

// if (charCode > 31 && (charCode < 48 || charCode > 57)) {
// return false
// }

// return true
}

return (
<EdFiModalForm
content={<Flex w='full'>
Expand All @@ -97,12 +122,12 @@ const ApplicationForm = ({ instance, mode, editApplicationData, onFinishSave }:
mt='32px'
w='full'
>
<LocalEducationAgenciesFormSection
description={getLocalEducationOrgId()}
mode={mode}
name={getCurrentTenantOrgName(userProfile)}
selected={true}
onToggleOrgId={() => onToggleOrgId()}
<MultiInput
filterInput={isNumberKey}
label='EdOrgs'
transformText={transformText}
values={edOrgs}
onChange={setEdorgs}
/>
</Flex>

Expand Down
2 changes: 1 addition & 1 deletion src/core/Edfi/EdfiApplications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface EdfiApplication {
id: number
applicationName?: string
claimSetName?: string
educationOrganizationIds?: Array<number>
educationOrganizationIds?: number[]
odsInstanceIds?: Array<number>
vendorId?: number
profileIds?: Array<number>
Expand Down
Loading

0 comments on commit 9e0d3bc

Please sign in to comment.