Skip to content

Commit

Permalink
Run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
xjunior committed Jun 4, 2024
1 parent f3aa247 commit d09ce22
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 72 deletions.
18 changes: 12 additions & 6 deletions audiences-react/src/AudienceForm/AllToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Button, Card, Flex } from "playbook-ui"
import { Header } from "./Header"

type MainProps = {
total: number,
name: string,
children: React.ReactNode,
total: number
name: string
children: React.ReactNode
}

export function AllToggle({ total, name, children }: MainProps) {
Expand All @@ -25,17 +25,23 @@ export function AllToggle({ total, name, children }: MainProps) {

<Flex justify="between" marginTop="md">
<Button
disabled={!formState.isDirty || !formState.isValid || formState.isSubmitting}
disabled={
!formState.isDirty ||
!formState.isValid ||
formState.isSubmitting
}
loading={formState.isSubmitting}
text="Save"
htmlType="submit" />
htmlType="submit"
/>

{formState.isDirty && (
<Button
marginLeft="sm"
text="Cancel"
variant="link"
htmlType="reset" />
htmlType="reset"
/>
)}
</Flex>
</Flex>
Expand Down
6 changes: 5 additions & 1 deletion audiences-react/src/AudienceForm/CriteriaDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export function CriteriaDescription({ groups }: CriteriaDescriptionProps) {
isEmpty(groups[key]) ? null : (
<React.Fragment key={`groups-${key}`}>
<Body tag="span" text={` ${prep} `} />
<Title tag="span" size={4} text={toSentence(map(groups[key], "displayName"))} />
<Title
tag="span"
size={4}
text={toSentence(map(groups[key], "displayName"))}
/>
</React.Fragment>
),
)}
Expand Down
7 changes: 2 additions & 5 deletions audiences-react/src/AudienceForm/CriteriaForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ export type CriteriaFormProps = {
current: string
onClose: () => void
}
export function CriteriaForm({
current,
onClose,
}: CriteriaFormProps) {
export function CriteriaForm({ current, onClose }: CriteriaFormProps) {
const { setValue, watch } = useFormContext()
const value = watch(`${current}.groups`)
const initialValue = useMemo(() => ({ ...value }), [current])
Expand All @@ -24,7 +21,7 @@ export function CriteriaForm({
setValue(`${current}.groups`, initialValue)
onClose()
}

return (
<Card>
<Card.Header headerColor="white">
Expand Down
30 changes: 17 additions & 13 deletions audiences-react/src/AudienceForm/CriteriaList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,28 @@ type CriteriaListFieldsProps = {
onRemoveCriteria: (index: number) => void
onEditCriteria: (index: number) => void
}
export function CriteriaList({ addCriteriaLabel, onAddCriteria, onRemoveCriteria, onEditCriteria }: CriteriaListFieldsProps) {
export function CriteriaList({
addCriteriaLabel,
onAddCriteria,
onRemoveCriteria,
onEditCriteria,
}: CriteriaListFieldsProps) {
const { watch, getFieldState } = useFormContext()
const currentCriteria = (watch("criteria") || []) as GroupCriterion[]
const isCriterionDirty = (index: number) => getFieldState(`criteria.${index}`).isDirty
const isCriterionDirty = (index: number) =>
getFieldState(`criteria.${index}`).isDirty

return (
<Flex orientation="column" align="stretch">
{currentCriteria.map(
(criterion, index: number) => (
<CriteriaCard
key={`criterion-${criterion.id}`}
criterion={criterion}
viewUsers={!isCriterionDirty(index)}
onRequestEdit={() => onEditCriteria(index)}
onRequestRemove={() => onRemoveCriteria(index)}
/>
),
)}
{currentCriteria.map((criterion, index: number) => (
<CriteriaCard
key={`criterion-${criterion.id}`}
criterion={criterion}
viewUsers={!isCriterionDirty(index)}
onRequestEdit={() => onEditCriteria(index)}
onRequestRemove={() => onRemoveCriteria(index)}
/>
))}

<FlexItem alignSelf="center">
<Button
Expand Down
2 changes: 1 addition & 1 deletion audiences-react/src/AudienceForm/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function Header({ name, total }: HeaderProps) {
return (
<Flex orientation="row" spacing="between" wrap>
<FlexItem>
<Caption text={`Members ${formState.isDirty ? '' : total}`} />
<Caption text={`Members ${formState.isDirty ? "" : total}`} />

{formState.isDirty ? (
<Caption
Expand Down
11 changes: 9 additions & 2 deletions audiences-react/src/AudienceForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export const AudienceForm = ({
}: AudienceFormProps) => {
const { context, update } = useAudienceContext()
const form = useForm<AudienceContext>({ values: context, mode: "onChange" })
const { currentEditing, addNewCriteria, editCriteria, removeCriteria, closeCriteria } = useCriteriaEditForm({ form, groupResources })
const {
currentEditing,
addNewCriteria,
editCriteria,
removeCriteria,
closeCriteria,
} = useCriteriaEditForm({ form, groupResources })

if (!context) {
return (
Expand All @@ -46,7 +52,8 @@ export const AudienceForm = ({
<ScimResourceTypeahead
label="Add Individuals"
name="extra_users"
resourceId={userResource} />
resourceId={userResource}
/>
)}

<CriteriaList
Expand Down
65 changes: 40 additions & 25 deletions audiences-react/src/AudienceForm/useCriteriaEditForm.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
import { useState } from "react";
import { useFieldArray, UseFormReturn } from "react-hook-form";
import { every, isEmpty, keyBy, mapValues, omitBy } from "lodash";
import { AudienceContext, GroupCriterion } from "../types";
import { useState } from "react"
import { useFieldArray, UseFormReturn } from "react-hook-form"
import { every, isEmpty, keyBy, mapValues, omitBy } from "lodash"
import { AudienceContext, GroupCriterion } from "../types"

const validateCriteria = (criteria: GroupCriterion[]) => every(criteria, validateCriterion);
const validateCriterion = ({ groups }: GroupCriterion) => !isEmpty(omitBy(groups, isEmpty));
const validateCriteria = (criteria: GroupCriterion[]) =>
every(criteria, validateCriterion)
const validateCriterion = ({ groups }: GroupCriterion) =>
!isEmpty(omitBy(groups, isEmpty))
type UseCriteriaEditFormType = {
form: UseFormReturn<AudienceContext>;
groupResources: string[];
};
form: UseFormReturn<AudienceContext>
groupResources: string[]
}

export function useCriteriaEditForm({ form, groupResources }: UseCriteriaEditFormType) {
const [currentEditing, setCurrentEditing] = useState<number | undefined>();
const { remove, append: appendCriteria, fields: currentCriteria } = useFieldArray({
export function useCriteriaEditForm({
form,
groupResources,
}: UseCriteriaEditFormType) {
const [currentEditing, setCurrentEditing] = useState<number | undefined>()
const {
remove,
append: appendCriteria,
fields: currentCriteria,
} = useFieldArray({
name: "criteria",
control: form.control,
rules: { validate: validateCriteria },
});
})

const addNewCriteria = () => {
const emptyCriteria = mapValues(keyBy(groupResources), () => []);
appendCriteria({ groups: emptyCriteria });
setCurrentEditing(currentCriteria.length);
};
const emptyCriteria = mapValues(keyBy(groupResources), () => [])
appendCriteria({ groups: emptyCriteria })
setCurrentEditing(currentCriteria.length)
}
const removeCriteria = (index: number) => {
if (confirm("Remove criteria?")) {
remove(index);
remove(index)
}
};
}
const editCriteria = (index: number) => {
setCurrentEditing(index);
};
setCurrentEditing(index)
}
const closeCriteria = () => {
if (!form.formState.isValid) {
remove(currentEditing);
remove(currentEditing)
}
setCurrentEditing(undefined);
};
setCurrentEditing(undefined)
}

return { currentEditing, addNewCriteria, editCriteria, removeCriteria, closeCriteria };
return {
currentEditing,
addNewCriteria,
editCriteria,
removeCriteria,
closeCriteria,
}
}
19 changes: 0 additions & 19 deletions audiences-react/src/audiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,6 @@ export function useAudience(uri: string): ContextProps {
[uri],

Check warning on line 28 in audiences-react/src/audiences.ts

View workflow job for this annotation

GitHub Actions / node / Node 21

React Hook useEffect has a missing dependency: 'fetch'. Either include it or remove the dependency array
)

async function fetchUsers(
criterion?: GroupCriterion,
search?: string,
offset?: number,
) {
return fetch.get(
`/users/${criterion?.id || ""}?offset=${offset}&search=${search}`,
)
}

async function update(attrs: AudienceContext) {
try {
const updatedContext = await fetch.put(attrs)
setContext(updatedContext)
} catch (e) {
console.log(attrs, e)
}
}

return { context, setContext, ...fetch }
}

Expand Down

0 comments on commit d09ce22

Please sign in to comment.