Skip to content

Commit

Permalink
Merge pull request #353 from italia/352-usedby-field-missing
Browse files Browse the repository at this point in the history
feat: add usedBy optional field
  • Loading branch information
valeriocomo authored Dec 6, 2024
2 parents 5d933d3 + 38de37e commit 9eaea12
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/app/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import { getPubliccodeYmlVersionList } from "../contents/publiccode-yml-version"
import { isMinorThanLatest, toSemVerObject } from "../semver";
import { resetPubliccodeYmlLanguages, setPubliccodeYmlLanguages } from "../store/publiccodeYmlLanguages";
import yamlSerializer from "../yaml-serializer";
import { removeDuplicate } from "../yaml-upload";
import EditorUsedBy from "./EditorUsedBy";

const validatorFn = async (values: PublicCode) => await validator({ publiccode: JSON.stringify(values), baseURL: values.url });

Expand Down Expand Up @@ -189,6 +191,11 @@ export default function Editor() {

if (publicCode) {
const values = { ...defaultValues, ...publicCode } as PublicCode;

if (publicCode.usedBy) {
values.usedBy = removeDuplicate(publicCode.usedBy)
}

setLanguages(publicCode);
reset(values);

Expand Down Expand Up @@ -372,7 +379,9 @@ export default function Editor() {
filter="contains"
/>
</Col>

<Col>
<EditorUsedBy />
</Col>
<Col>
<EditorSelect<"legal.license">
fieldName="legal.license"
Expand Down
78 changes: 78 additions & 0 deletions src/app/components/EditorUsedBy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Button, Icon, Input, InputGroup } from "design-react-kit";
import { useState } from "react";
import { useController, useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
import PublicCode from "../contents/publiccode";
import { removeDuplicate } from "../yaml-upload";

export default function EditorUsedBy(): JSX.Element {
const { control } = useFormContext<PublicCode>();
const {
field: { onChange, value },
} = useController<PublicCode>({
control,
name: `usedBy`,
shouldUnregister: true,
});
const { t } = useTranslation();

const usedBy: string[] = value ? (value as string[]) : [];
const [current, setCurrent] = useState<string>("");

const label = t(`publiccodeyml.usedBy.label`);
const description = t(`publiccodeyml.usedBy.description`);

const add = () => {
onChange(removeDuplicate([...usedBy, current.trim()]));
setCurrent("");
};

const remove = (feat: string) => {
onChange(usedBy.filter((elem) => elem !== feat));
};


return (
<div className="form-group">
<label
className="active"
htmlFor={`usedby`}
>{`${label} *`}</label>
<ul className="list-group list-group-flush">
{usedBy.map((feat) => (
<li
className="list-group-item d-flex justify-content-between align-items-center"
key={feat}
>
{feat}
<Button
color="link"
icon
onClick={() => remove(feat)}
size="xs"
>
<Icon icon="it-delete" size="sm" title="Remove" />
</Button>
</li>
))}
</ul>
<InputGroup>
<Input
value={current}
onChange={({ target }) => setCurrent(target.value)}
/>
<div className="input-group-append">
<Button
color="primary"
disabled={current.trim() === "" || usedBy.includes(current.trim())}
onClick={add}
>
Add PA
</Button>
</div>
</InputGroup>

<small className="form-text">{description}</small>
</div>
);
}
4 changes: 3 additions & 1 deletion src/app/yaml-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export const isYamlFile = (file?: File | null) => Boolean(file?.type && yamlMime
export const hasYamlFileExtension = (value?: string) => {
const ext = value?.split(/[. ]+/).pop();
return ext === "yml" || ext === "yaml";
}
}

export const removeDuplicate = <T>(array: Array<T>) => [...new Set(array)];

0 comments on commit 9eaea12

Please sign in to comment.