Skip to content

Commit

Permalink
Able to Edit the Vendor
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhammadasif committed Dec 17, 2024
1 parent 7fa63b7 commit 149c6b5
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 80 deletions.
106 changes: 58 additions & 48 deletions src/components/common/Instance/PartnerForm.tsx
Original file line number Diff line number Diff line change
@@ -1,90 +1,100 @@
import {
Flex, FormControl
Flex, FormControl,
Spinner
} from '@chakra-ui/react'
import {
CompleteFormErrorMessage, CustomFormLabel, CustomInput
CompleteFormErrorMessage, CustomFormLabel, CustomInput
} from '@edfi/admin-console-shared-sdk'
import { EdfiVendor } from '../../../core/Edfi/EdfiVendors'
import usePartnerForm from '../../../hooks/adminActions/edfi/usePartnerForm'
import EdFiModalForm from './EdFiModalForm'

interface PartnerFormProps {
schoolYear: number
mode: 'add' | 'edit'
onFinishSave: () => void
initialData: EdfiVendor | undefined
}

const PartnerForm = ({ schoolYear, mode, onFinishSave }: PartnerFormProps) => {
const PartnerForm = ({ initialData, schoolYear, mode, onFinishSave }: PartnerFormProps) => {
const {
partnerData,
isSaving,
setIsSaving,
errors,
hasTriedSubmit,
onChangeParnerData,
onSave
} = usePartnerForm({
mode,
schoolYear,
onFinishSave
onFinishSave,
initialData
})

function onPClose() {
setIsSaving(true)
onFinishSave()
}

function onPSave() {
onSave()
onFinishSave()
}

return (
<EdFiModalForm
content={<Flex w='full'>
return (isSaving ? <Spinner /> : <EdFiModalForm
content={<Flex w='full'>
<Flex
flexDir='column'
w='full'
>
{ Object.keys(errors).length > 0 && hasTriedSubmit && <CompleteFormErrorMessage /> }

<Flex
flexDir='column'
w='full'
>
{ Object.keys(errors).length > 0 && hasTriedSubmit && <CompleteFormErrorMessage /> }
<FormControl>
<CustomFormLabel
htmlFor="partnerName"
text="Vendor Name"
/>

<Flex
flexDir='column'
w='full'
>
<FormControl>
<CustomFormLabel
htmlFor="partnerName"
text="Vendor Name"
/>
<CustomInput
error={errors && errors['partnerName'] && errors['partnerName'].message}
id="partnerName"
value={partnerData.company}
onChange={onChangeParnerData}
/>
</FormControl>

<FormControl mt='24px'>
<CustomFormLabel
htmlFor="namespacePrefixes"
text="Namespace Prefixes"
/>

<Flex
flexDir='column'
w='full'
>
<CustomInput
error={errors && errors['partnerName'] && errors['partnerName'].message}
id="partnerName"
value={partnerData.company}
error={errors && errors['namespacePrefixes'] && errors['namespacePrefixes'].message}
id="namespacePrefixes"
value={partnerData.namespacePrefixes}
onChange={onChangeParnerData}
/>
</FormControl>

<FormControl mt='24px'>
<CustomFormLabel
htmlFor="namespacePrefixes"
text="Namespace Prefixes"
/>

<Flex
flexDir='column'
w='full'
>
<CustomInput
error={errors && errors['namespacePrefixes'] && errors['namespacePrefixes'].message}
id="namespacePrefixes"
value={partnerData.namespacePrefixes}
onChange={onChangeParnerData}
/>
</Flex>
</FormControl>
</Flex>
</Flex>
</FormControl>
</Flex>
</Flex>}
actionText="save"
headerText={mode === 'add'? 'Add Vendor' : 'Edit Vendor'}
isSaving={isSaving}
onClose={onFinishSave}
onSave={onPSave}
/>
</Flex>
</Flex>}
actionText="Save"
headerText={mode === 'add'? 'Add Vendor' : 'Edit Vendor'}
isSaving={isSaving}
onClose={onPClose}
onSave={onPSave}
/>
)
}

Expand Down
19 changes: 14 additions & 5 deletions src/components/common/Instance/PartnersAndApplicationAccordion.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CloseIcon } from '@chakra-ui/icons'
import {
Accordion,
AccordionButton,
Expand Down Expand Up @@ -26,7 +25,7 @@ interface PartnersAndApplicationAccordionProps {
isDeletingVendor: DeletingState
isDeletingApplication: DeletingState
onSelectVendor: (partnerId: number) => void
onEditVendor: () => void
onEditVendor: (partner: EdfiVendorWithApplications) => void
onEditApplication: (application: EdfiApplication) => void
onDeleteVendor: (partnerId: string) => void
onDeleteApplication: (applicationId: string) => void
Expand Down Expand Up @@ -129,12 +128,22 @@ const PartnersAndApplicationAccordion = ({ vendorsWithApplicationsList, isDeleti
</Text>
</Flex>

{ adminConfig && adminConfig.showEdfiPartnerDelete && <Flex
<Flex
justifyContent='center'
ml='auto'
w='150px'
>
<Button
// borderRadius='4px 0px 0px 4px'
minW='39px'
size='xs'
variant='primaryBlue600'
onClick={() => onEditVendor(partner)}
>
Edit
</Button>

{/* <Button
_hover={{ background: 'red.600' }}
aria-labelledby="delete-partner-btn"
bg='red.600'
Expand All @@ -156,8 +165,8 @@ const PartnersAndApplicationAccordion = ({ vendorsWithApplicationsList, isDeleti
aria-hidden="true"
focusable="false"
/>
</Button>
</Flex> }
</Button> */}
</Flex>
</Flex>

<AccordionPanel pb={4}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { adminConsoleContext } from '../../../context/adminConsoleContext'
import { DeletingState } from '../../../core/deletingState.types'
import { EdfiApplication } from '../../../core/Edfi/EdfiApplications'
import { EdfiVendor } from '../../../core/Edfi/EdfiVendors'
import { ODSInstance } from '../../../core/ODSInstance.types'
import usePartnersAndApplicationsAccordion from '../../../hooks/adminActions/edfi/usePartnersAndApplicationsAccordion'
import useEDXToast from '../../../hooks/common/useEDXToast'
Expand All @@ -26,6 +27,7 @@ const PartnersAndApplicationTabContent = ({ instance, schoolYear }: PartnersAndA
const [ selectedApplication, setSelectedApplication ] = useState<EdfiApplication | undefined>()
const adminConfig = useContext(adminConsoleContext)
const { deleteVendorForSchoolYear } = useEdfiVendorsService()
const [ isLoading, setisLoading ] = useState(false)

const [ isDeletingVendor, setIsDeletingVendor ] = useState<DeletingState>({
deleting: false,
Expand All @@ -47,7 +49,14 @@ const PartnersAndApplicationTabContent = ({ instance, schoolYear }: PartnersAndA

const { successToast, errorToast } = useEDXToast()
const onAddVendor = () => setElementToShow('add vendor')
const onEditVendor = () => setElementToShow('edit vendor')
const [ selectedVendor, setSelectedVendor ] = useState<EdfiVendor | undefined>()

const onEditVendor = (vendor: EdfiVendor) => {
console.log('edit vendor', vendor)
setSelectedVendor(vendor)
setElementToShow('edit vendor')
}

const onAddApplication = () => setElementToShow('add application')

const onEditApplication = (application: EdfiApplication) => {
Expand Down Expand Up @@ -110,8 +119,11 @@ const PartnersAndApplicationTabContent = ({ instance, schoolYear }: PartnersAndA
const onReturnToAccordion = async () => {
console.log('return to accordion')
setElementToShow('accordion')
setSelectedVendor(undefined)
setisLoading(true)
await onRefreshVendorsWithApplications()
forceUpdate()
setisLoading(false)
}

console.log('rendering...')
Expand Down Expand Up @@ -151,6 +163,7 @@ const PartnersAndApplicationTabContent = ({ instance, schoolYear }: PartnersAndA

<ConsoleModal
content={<PartnerForm
initialData={undefined}
mode="add"
schoolYear={schoolYear}
onFinishSave={onReturnToAccordion}
Expand All @@ -161,6 +174,7 @@ const PartnersAndApplicationTabContent = ({ instance, schoolYear }: PartnersAndA

<ConsoleModal
content={<PartnerForm
initialData={selectedVendor}
mode="edit"
schoolYear={schoolYear}
onFinishSave={onReturnToAccordion}
Expand Down
7 changes: 0 additions & 7 deletions src/context/tenantContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ export const TenantsContextProvider: FC<TenantsContextProviderProps> = ({ childr
return
}

if (!config.app.multiTenancy) {

setTenants([])
return
}



// Fetch tenants from the API
const tenants = await apiService?.tenants?.getAll?.()
Expand Down
8 changes: 4 additions & 4 deletions src/core/validation/validators/PartnerValidator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CreateEdfiVendorRequest } from '../../../services/AdminActions/Edfi/Vendors/EdfiVendorsService.requests'
import { EdfiVendor } from '../../Edfi/EdfiVendors'
import {
FieldError, FormDataErrors, ValidateFieldParams
FieldError, FormDataErrors, ValidateFieldParams
} from '../FormValidations.types'
import partnerNameSchema from '../schemas/Vendors/nameSchema'
import namespacePrefixesSchema from '../schemas/Vendors/namespacePrefixesSchema'
Expand All @@ -9,9 +9,9 @@ import ValidationErrorsMapper from './ValidationErrorsMapper'
export type PartnerValidatorFields = 'partnerName' | 'namespacePrefixes'

export class PartnerValidator {
public static validateField({ data, field }: ValidateFieldParams<CreateEdfiVendorRequest, PartnerValidatorFields>): FieldError | null {
public static validateField({ data, field }: ValidateFieldParams<EdfiVendor, PartnerValidatorFields>): FieldError | null {
if (field === 'partnerName') {
return this.validateName(data.contactName)
return this.validateName(data.contactName ?? '')
} else if (field === 'namespacePrefixes') {
return this.validateNamespacePrefixes(data.namespacePrefixes)
}
Expand Down
53 changes: 50 additions & 3 deletions src/hooks/adminActions/edfi/usePartnerForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,54 @@ import {
} from '@edfi/admin-console-shared-sdk'
import {
ChangeEvent, useContext,
useEffect,
useState
} from 'react'
import { adminConsoleContext } from '../../../context/adminConsoleContext'
import { EdfiVendor } from '../../../core/Edfi/EdfiVendors'
import { usePluginContext } from '../../../plugins/BasePlugin'
import { CreateEdfiVendorRequest } from '../../../services/AdminActions/Edfi/Vendors/EdfiVendorsService.requests'
import useEDXToast from '../../common/useEDXToast'
import initialPartnerData from './initialPartnerData'
import usePartnerFormValidation from './usePartnerFormValidation'

interface UsePartnerFormProps {
schoolYear: number
onFinishSave: () => void
mode?: 'add' | 'edit' | undefined
initialData?: EdfiVendor | undefined
}

const usePartnerForm = ({ schoolYear, onFinishSave }: UsePartnerFormProps) => {
const usePartnerForm = ({ schoolYear, onFinishSave, initialData, mode }: UsePartnerFormProps) => {
const { edxAppConfig, auth } = useContext(TEEAuthDataContext)
const adminConfig = useContext(adminConsoleContext)
// const { createVendorForSchoolYear, getVendorsListForSchoolYear } = useEdfiVendorService()
const [ partnerData, setPartnerData ] = useState<CreateEdfiVendorRequest>({ ...initialPartnerData })
const [ partnerData, setPartnerData ] = useState<EdfiVendor>({ ...initialPartnerData })
const [ isSaving, setIsSaving ] = useState(false)
const [ hasTriedSubmit, setHasTriedSubmit ] = useState(false)
const { errors, validPartnerData, validateInputChange } = usePartnerFormValidation()
const { successToast, errorToast } = useEDXToast(7000)
const { config } = useConfig()
const { functionalities } = usePluginContext()
const api = functionalities.ApiService?.(config, useApiService)
const [ isEditing, setIsEditing ] = useState(false)

// check if initialData is passed and set it to partnerData
useEffect(() => {
if (mode === 'edit') {
setIsEditing(true)
} else {
setIsEditing(false)
}

if (initialData) {
setPartnerData({
...partnerData,
id: initialData.id ?? undefined,
company: initialData.company ?? '',
namespacePrefixes: initialData.namespacePrefixes ?? '',
})
}
}, [ initialData, mode ])

const onChangeParnerData = (e: ChangeEvent<HTMLInputElement>) => {
const nparnerData = { ...partnerData }
Expand All @@ -51,6 +73,10 @@ const usePartnerForm = ({ schoolYear, onFinishSave }: UsePartnerFormProps) => {
}
}

if(partnerData.id) {
nparnerData.id = partnerData.id
}

setPartnerData(nparnerData)
}

Expand All @@ -62,6 +88,26 @@ const usePartnerForm = ({ schoolYear, onFinishSave }: UsePartnerFormProps) => {
// const vendorList = await getVendorsListForSchoolYear(adminConfig.actionParams, schoolYear)
const vendorList = await api?.vendors.getAll() ?? []

if(isEditing) {
if(!partnerData.id) {
console.log('Vendor ID is missing', partnerData)
errorToast('Failed to Update Vendor')
return
}

try {
await api?.vendors.update(partnerData.id, partnerData)
successToast('Updated Vendor')
} catch (e) {
errorToast('Failed to Update Vendor')
} finally {
setIsSaving(false)
onFinishSave()
}

return
}

if(vendorList.some(vendor => vendor.contactName === partnerData.contactName)) {
errorToast('A Vendor with this name already exists. Please choose a unique name and try again.')
setIsSaving(false)
Expand All @@ -87,6 +133,7 @@ const usePartnerForm = ({ schoolYear, onFinishSave }: UsePartnerFormProps) => {
return {
partnerData,
isSaving,
setIsSaving,
errors,
hasTriedSubmit,
onChangeParnerData,
Expand Down
Loading

0 comments on commit 149c6b5

Please sign in to comment.