generated from pagevamp/copilot-custom-app-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
301 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,169 @@ | ||
'use client'; | ||
|
||
import { FooterSave } from '@/components/footerSave/FooterSave'; | ||
import { MultiSelect } from '@/components/multiSelect/MultiSelect'; | ||
import { StyledTextInput } from '@/components/styled/StyledTextInput'; | ||
import { apiUrl } from '@/config'; | ||
import { Stack, Typography, styled } from '@mui/material'; | ||
import { useMemo, useState } from 'react'; | ||
|
||
export const ManagePageContainer = ({ | ||
customFieldAccess, | ||
client, | ||
token, | ||
clientId, | ||
companyId, | ||
portalId, | ||
}: { | ||
customFieldAccess: any; | ||
client: any; | ||
token: string; | ||
clientId: string; | ||
companyId: string; | ||
portalId: string; | ||
}) => { | ||
const [_customFieldAccess, setCustomFieldAccess] = useState<any>(customFieldAccess); | ||
|
||
const [loading, setLoading] = useState(false); | ||
|
||
const [allowedCustomField, setAllowedCustomField] = useState<any>(null); | ||
|
||
const customFieldsValue: any | null = client.customFields; | ||
|
||
const [profileData, setProfileData] = useState<any>({}); | ||
|
||
const [originalProfileData, setOriginalProfileData] = useState<any>({}); | ||
|
||
useMemo(() => { | ||
if (customFieldAccess.length > 0) { | ||
const allowedFields = customFieldAccess.filter((field: any) => field.permission?.length > 0); | ||
const getSelectedValuesForMultiSelect = (key: string) => { | ||
const values = customFieldsValue[key]; | ||
if (!values) { | ||
return []; | ||
} | ||
const customFieldObject = allowedFields.find((el: any) => el.key === key); | ||
const selectedValues = customFieldObject.options.filter((el: any) => values.includes(el.key)); | ||
|
||
return selectedValues; | ||
}; | ||
allowedFields.map((field: any) => { | ||
profileData[field.key] = | ||
field.type === 'multiSelect' | ||
? [...getSelectedValuesForMultiSelect(field.key)] | ||
: customFieldsValue[field.key] || ''; | ||
}); | ||
setOriginalProfileData(profileData); | ||
setAllowedCustomField(allowedFields); | ||
} else { | ||
setAllowedCustomField([]); | ||
} | ||
}, [_customFieldAccess]); | ||
Check warning on line 61 in src/app/manage/views/ManagePageContainer.tsx
|
||
|
||
const handleCancel = () => { | ||
setProfileData({ | ||
...originalProfileData, | ||
}); | ||
}; | ||
|
||
const handleSave = async () => { | ||
setLoading(true); | ||
function transformObject(obj: any) { | ||
const result: any = {}; | ||
for (const key in obj) { | ||
if (Array.isArray(obj[key])) { | ||
result[key] = obj[key].map((item: any) => item.key); | ||
} else { | ||
result[key] = obj[key]; | ||
} | ||
} | ||
return result; | ||
} | ||
const form = transformObject(profileData); | ||
|
||
await fetch('/api/client-profile-updates', { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
token: token, | ||
companyId: companyId, | ||
clientId: clientId, | ||
portalId: portalId, | ||
form: form, | ||
}), | ||
}); | ||
|
||
const res = await fetch(`/api/custom-field-access?token=${token}&portalId=${portalId}`); | ||
|
||
const { data } = await res.json(); | ||
|
||
setCustomFieldAccess(data); | ||
|
||
setLoading(false); | ||
}; | ||
|
||
export const ManagePageContainer = () => { | ||
return ( | ||
<Stack | ||
direction="column" | ||
sx={{ | ||
padding: { xs: 4, sm: 6 }, | ||
rowGap: 6, | ||
border: (theme) => `1px solid ${theme.color.borders.border}`, | ||
borderRadius: (theme) => theme.shape.radius100, | ||
mt: 4, | ||
}} | ||
> | ||
<InputContainer> | ||
<Typography variant="md">Phone number</Typography> | ||
<StyledTextInput variant="outlined" padding="8px 12px" /> | ||
</InputContainer> | ||
<InputContainer> | ||
<Typography variant="md">Website</Typography> | ||
<StyledTextInput variant="outlined" padding="8px 12px" /> | ||
</InputContainer> | ||
<InputContainer> | ||
<Typography variant="md">Website</Typography> | ||
<StyledTextInput variant="outlined" padding="8px 12px" /> | ||
</InputContainer> | ||
<InputContainer> | ||
<MultiSelect<{ name: string }> data={data} chipColor="rgb(0, 0, 255)" nameField={(item) => item.name} /> | ||
</InputContainer> | ||
</Stack> | ||
<> | ||
<Stack | ||
direction="column" | ||
sx={{ | ||
padding: { xs: 4, sm: 6 }, | ||
rowGap: 6, | ||
border: (theme) => `1px solid ${theme.color.borders.border}`, | ||
borderRadius: (theme) => theme.shape.radius100, | ||
mt: 4, | ||
}} | ||
> | ||
{allowedCustomField && | ||
allowedCustomField.map((field: any, key: number) => { | ||
if (field.type !== 'multiSelect') { | ||
return ( | ||
<InputContainer key={key}> | ||
<Typography variant="md">{field.name}</Typography> | ||
<StyledTextInput | ||
value={profileData && profileData[field.key]} | ||
variant="outlined" | ||
padding="8px 12px" | ||
disabled={!field.permission.includes('EDIT')} | ||
key={key} | ||
onChange={(e) => { | ||
setProfileData((prev: any) => { | ||
return { ...prev, [field.key]: e.target.value }; | ||
}); | ||
}} | ||
type="text" | ||
/> | ||
</InputContainer> | ||
); | ||
} | ||
if (field.type === 'multiSelect') { | ||
return ( | ||
<InputContainer key={key}> | ||
<Typography variant="md">{field.name}</Typography> | ||
<MultiSelect<{ label: string }> | ||
key={key} | ||
data={field.options} | ||
chipColor="rgb(0, 0, 255)" | ||
nameField={(item) => item.label} | ||
value={profileData && profileData[field.key]} | ||
getSelectedValue={(value) => { | ||
setProfileData((prev: any) => { | ||
return { ...prev, [field.key]: value }; | ||
}); | ||
}} | ||
/> | ||
</InputContainer> | ||
); | ||
} | ||
})} | ||
</Stack> | ||
{JSON.stringify(originalProfileData) === JSON.stringify(profileData) ? null : ( | ||
<FooterSave type="2" loading={loading} handleSave={handleSave} handleCancel={handleCancel} /> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
const InputContainer = styled(Stack)({ | ||
flexDirection: 'column', | ||
rowGap: 1.33, | ||
}); | ||
|
||
const data: { name: string }[] = [ | ||
{ name: 'Option 1' }, | ||
{ name: 'Option 2' }, | ||
{ name: 'Option 3' }, | ||
{ name: 'Option 4' }, | ||
{ name: 'Option 5' }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Stack, Typography } from '@mui/material'; | ||
import { VariantButton1, VariantButton2 } from '../styled/VariantButton'; | ||
|
||
export const FooterSave = ({ | ||
loading, | ||
handleSave, | ||
handleCancel, | ||
type, | ||
}: { | ||
loading: boolean; | ||
handleSave: () => void; | ||
handleCancel: () => void; | ||
type: '1' | '2'; | ||
}) => { | ||
return ( | ||
<Stack | ||
sx={{ | ||
width: '100%', | ||
position: 'fixed', | ||
bottom: 0, | ||
left: 0, | ||
borderTop: (theme) => `1px solid ${theme.color.borders.border}`, | ||
padding: '16px 24px', | ||
background: '#fff', | ||
alignItems: 'flex-end', | ||
zIndex: 9999, | ||
}} | ||
> | ||
<Stack direction="row" columnGap={5}> | ||
<VariantButton1 onClick={handleCancel}> | ||
<Typography variant="md">Cancel</Typography> | ||
</VariantButton1> | ||
<VariantButton2 | ||
onClick={handleSave} | ||
sx={{ | ||
background: type === '1' ? '#212B36' : '#5e57e3', | ||
border: `1px solid ${type === '1' ? '#212B36' : '#5e57e3'}`, | ||
}} | ||
> | ||
{loading ? ( | ||
<Typography | ||
variant="md" | ||
sx={{ | ||
color: '#fff', | ||
}} | ||
> | ||
Saving... | ||
</Typography> | ||
) : ( | ||
<Typography | ||
variant="md" | ||
sx={{ | ||
color: '#fff', | ||
}} | ||
> | ||
Save changes | ||
</Typography> | ||
)} | ||
</VariantButton2> | ||
</Stack> | ||
</Stack> | ||
); | ||
}; |
Oops, something went wrong.