Skip to content

Commit 3760bd3

Browse files
committed
Added HWNG shortcut suffixes to settings
1 parent 9a4d86f commit 3760bd3

File tree

8 files changed

+190
-29
lines changed

8 files changed

+190
-29
lines changed

components/compound/CompoundDetails.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import useSWR from "swr"
55
import { IngredientDetails } from "components/compound/ingredient/IngredientDetails"
66
import { Button, Spinner } from "components/ui"
77
import { Fieldset, FormGroup } from "components/ui/forms"
8+
import { SettingsFields } from "lib/fields"
89
import { CompoundWithIngredients, MfrAll } from "types/models"
910

10-
import { getHwngShortcutString, shortcutSuffixMap } from "./helpers"
11+
import { getHwngShortcutString } from "./helpers"
1112

1213
type Props = {
1314
data: CompoundWithIngredients
@@ -28,6 +29,15 @@ const CompoundDetails = (props: Props) => {
2829
[data.id, shortcutVariations, data.shortcutSuffix],
2930
)
3031

32+
const { data: settings, error: settingsError } =
33+
useSWR<SettingsFields>("/api/settings")
34+
35+
if (settingsError) {
36+
console.error(settingsError)
37+
}
38+
39+
const shortcutSuffixes = useMemo(() => settings?.shortcutSuffixes, [settings])
40+
3141
return (
3242
<div className="compound-details">
3343
<FormGroup row className="name-shortcut-group">
@@ -62,12 +72,18 @@ const CompoundDetails = (props: Props) => {
6272
<span>
6373
{data.shortcutSuffix ? `${data.shortcutSuffix}` : "N/A"}
6474
</span>
65-
6675
{data.shortcutSuffix &&
67-
shortcutSuffixMap.has(data.shortcutSuffix) && (
76+
shortcutSuffixes?.find(
77+
(s) => s.code === data.shortcutSuffix,
78+
) && (
6879
<span>
6980
{" "}
70-
- {shortcutSuffixMap.get(data.shortcutSuffix)}
81+
-{" "}
82+
{
83+
shortcutSuffixes?.find(
84+
(s) => s.code === data.shortcutSuffix,
85+
)?.description
86+
}
7187
</span>
7288
)}
7389
</FormGroup>

components/compound/CompoundEntry/CompoundEntry.tsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useMemo } from "react"
22
import { UseFormReturn, useFieldArray, useWatch } from "react-hook-form"
3+
import useSWR from "swr"
34

45
import { Button } from "components/ui"
56
import {
@@ -12,10 +13,11 @@ import {
1213
import {
1314
NullPartialCompoundFields,
1415
NullPartialIngredientFields,
16+
SettingsFields,
1517
} from "lib/fields"
1618
import { nestedForm } from "lib/rhf/nestedForm"
1719

18-
import { getHwngShortcutString, shortcutSuffixMap } from "../helpers"
20+
import { getHwngShortcutString } from "../helpers"
1921
import IngredientEntry from "../ingredient/IngredientEntry"
2022
import ShortcutVariationsEntry from "./ShortcutVariationsEntry"
2123

@@ -86,6 +88,15 @@ const CompoundEntry = (props: Props) => {
8688
[compoundId, shortcutSuffix, shortcutVariations],
8789
)
8890

91+
const { data: settings, error: settingsError } =
92+
useSWR<SettingsFields>("/api/settings")
93+
94+
if (settingsError) {
95+
console.error(settingsError)
96+
}
97+
98+
const shortcutSuffixes = useMemo(() => settings?.shortcutSuffixes, [settings])
99+
89100
register("id")
90101
return (
91102
<>
@@ -153,20 +164,26 @@ const CompoundEntry = (props: Props) => {
153164
{...register("shortcut.suffix")}
154165
list={`shortcut-suffixes-list`}
155166
/>
156-
{shortcutSuffix && shortcutSuffixMap.has(shortcutSuffix) && (
157-
<span>{shortcutSuffixMap.get(shortcutSuffix)}</span>
158-
)}
159-
</FormGroup>
160-
<datalist id={`shortcut-suffixes-list`}>
161-
{Array.from(shortcutSuffixMap.entries()).map(
162-
([code, label]) => (
167+
{shortcutSuffix &&
168+
shortcutSuffixes?.find((s) => s.code === shortcutSuffix) !==
169+
undefined && (
170+
<span>
171+
{
172+
shortcutSuffixes.find(
173+
(s) => s.code === shortcutSuffix,
174+
)?.description
175+
}
176+
</span>
177+
)}
178+
<datalist id={`shortcut-suffixes-list`}>
179+
{shortcutSuffixes?.map(({ code, description }) => (
163180
<option
164181
value={code}
165182
key={code}
166-
>{`${code} - ${label}`}</option>
167-
),
168-
)}
169-
</datalist>
183+
>{`${code} - ${description}`}</option>
184+
))}
185+
</datalist>
186+
</FormGroup>
170187
</FormGroup>
171188
<FormGroup row className="shortcut-preview">
172189
<span className="label">Shortcut preview:</span>

components/compound/helpers.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
export const shortcutSuffixMap = new Map([
2-
["HC", "Hydrocortisone powder"],
3-
["SA", "Salicylic acid"],
4-
["CL", "Clindamycin powder"],
5-
["MC", "Menthol & camphor"],
6-
["ER", "Erythromycin powder"],
7-
["MZ", "Miconazole crystals"],
8-
["CZ", "Clotrimazole powder"],
9-
["AA", "Half & half"],
10-
])
11-
121
export const getHwngShortcutString = (
132
compoundId: number | undefined | null,
143
shortcutVariations: { code: string; name: string }[] | undefined | null,

components/settings/SettingsEntry/SettingsEntry.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { UseFormReturn } from "react-hook-form"
33
import { NullPartialSettingsFields } from "lib/fields"
44

55
import MfrPresetFieldset from "./field-presets/MfrPresetFieldset/MfrPresetFieldset"
6+
import ShortcutSuffixesFieldset from "./ShortcutSuffixesFieldset"
67

78
type SettingsEntryProps = {
89
formMethods: UseFormReturn<NullPartialSettingsFields>
@@ -14,6 +15,7 @@ const SettingsEntry = (props: SettingsEntryProps) => {
1415
return (
1516
<>
1617
<MfrPresetFieldset formMethods={formMethods} />
18+
<ShortcutSuffixesFieldset formMethods={formMethods} />
1719
</>
1820
)
1921
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import _ from "lodash"
2+
import { UseFormReturn, useFieldArray } from "react-hook-form"
3+
4+
import { Button } from "components/ui"
5+
import { Fieldset, FormGroup, Input } from "components/ui/forms"
6+
import { NullPartialSettingsFields } from "lib/fields"
7+
8+
import FieldArrayActions from "./FieldArrayActions"
9+
10+
type ShortcutSuffixesFieldsetProps = {
11+
formMethods: UseFormReturn<NullPartialSettingsFields>
12+
}
13+
const ShortcutSuffixesFieldset = (props: ShortcutSuffixesFieldsetProps) => {
14+
const {
15+
formMethods: { control, register },
16+
} = props
17+
18+
const arrayMethods = useFieldArray({
19+
control,
20+
name: "shortcutSuffixes",
21+
})
22+
23+
return (
24+
<Fieldset legend="HWNG shortcut suffixes:" className="shortcut-suffixes">
25+
<div className="shortcut-suffixes-list">
26+
<FormGroup className="shortcut-suffixes-header" row>
27+
<div>Code</div>
28+
<div>Description</div>
29+
</FormGroup>
30+
{arrayMethods.fields.map((field, index) => (
31+
<FormGroup className="shortcut-suffix-item" row key={field.id}>
32+
<Input
33+
className="shortcut-suffix-code"
34+
{...register(`shortcutSuffixes.${index}.code`, {
35+
deps: _.range(arrayMethods.fields.length)
36+
.filter((i2) => i2 !== index)
37+
.map((i2) => `shortcutSuffixes.${i2}.code`),
38+
})}
39+
fullWidth
40+
/>
41+
<Input
42+
{...register(`shortcutSuffixes.${index}.description`)}
43+
fullWidth
44+
/>
45+
46+
<FieldArrayActions
47+
arrayMethods={arrayMethods}
48+
field={field}
49+
index={index}
50+
/>
51+
</FormGroup>
52+
))}
53+
</div>
54+
<div className="actions">
55+
<Button
56+
size="small"
57+
onClick={() =>
58+
arrayMethods.append({
59+
code: null,
60+
description: null,
61+
})
62+
}
63+
>
64+
Add new shortcut suffix
65+
</Button>
66+
</div>
67+
<style jsx global>{`
68+
.shortcut-suffixes-list {
69+
display: grid;
70+
grid-template-columns: 1fr 5fr auto;
71+
column-gap: 1rem;
72+
73+
> .shortcut-suffixes-header {
74+
font-weight: 600;
75+
76+
&::after {
77+
content: "";
78+
}
79+
}
80+
81+
> .shortcut-suffix-item {
82+
.shortcut-suffix-code {
83+
text-transform: uppercase;
84+
}
85+
86+
> .form-group {
87+
display: contents !important;
88+
}
89+
}
90+
91+
> div.form-group {
92+
display: contents !important;
93+
width: 100%;
94+
}
95+
}
96+
97+
.shortcut-suffixes > .actions {
98+
margin-top: 1rem;
99+
}
100+
`}</style>
101+
</Fieldset>
102+
)
103+
}
104+
105+
export default ShortcutSuffixesFieldset

lib/fields/fields.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,24 @@ export const settingsSchema = z.object({
654654
labelling: createFieldArrayPresetsSchema(mfrSchema.shape.labelling),
655655
references: createFieldArrayPresetsSchema(mfrSchema.shape.references),
656656
}),
657+
shortcutSuffixes: z
658+
.object({
659+
code: z
660+
.string()
661+
.trim()
662+
.min(1)
663+
.transform((arg) => arg.toUpperCase()),
664+
description: z.string().trim().min(1),
665+
})
666+
.array()
667+
.superRefine((arg, ctx) =>
668+
refineNoDuplicates(
669+
arg,
670+
ctx,
671+
"code",
672+
(v: GetElementType<typeof arg>) => v.code,
673+
),
674+
),
657675
})
658676

659677
export type SettingsFields = z.output<typeof settingsSchema>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- AlterTable
2+
ALTER TABLE "settings" ADD COLUMN "shortcutSuffixes" JSONB NOT NULL DEFAULT '[]';
3+
4+
UPDATE "settings" SET "shortcutSuffixes" = '[
5+
{ "code": "HC", "description": "Hydrocortisone powder" },
6+
{ "code": "SA", "description": "Salicylic acid" },
7+
{ "code": "CL", "description": "Clindamycin powder" },
8+
{ "code": "MC", "description": "Menthol & camphor" },
9+
{ "code": "ER", "description": "Erythromycin powder" },
10+
{ "code": "MZ", "description": "Miconazole crystals" },
11+
{ "code": "CZ", "description": "Clotrimazole powder" },
12+
{ "code": "AA", "description": "Half & half" }
13+
]' WHERE "id" = 0;

prisma/schema.prisma

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ model Link {
290290
}
291291

292292
model Settings {
293-
id Int @id @default(autoincrement())
294-
mfrFieldPresets Json //{[fieldName]: {label?: String, value: any}|{label?: String, value: any}[]}
293+
id Int @id @default(autoincrement())
294+
mfrFieldPresets Json //{[fieldName]: {label?: String, value: any}|{label?: String, value: any}[]}
295+
shortcutSuffixes Json @default("[]") //{code: String, description: String}[]
295296
296297
@@map("settings")
297298
}

0 commit comments

Comments
 (0)