Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEB-64] Exclude custom crew positions from dropdown, take two #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions app/(authenticated)/calendar/[eventID]/AddEditSignUpSheetForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ function CrewPositionField(props: { parentName: string }) {
const selectController = useController({
name: `${props.parentName}.position_id`,
});
// Track the initial selected ID so that, if it's a custom field (but one that existed
// before), we can still display the correct value.
const [initialSelectedID] = useState(() => selectController.field.value);
const customController = useController({
name: `${props.parentName}.custom_position_name`,
});
Expand All @@ -162,14 +165,20 @@ function CrewPositionField(props: { parentName: string }) {
return [selectController.field.value.toString(10), false];
}, [selectController.field.value, customController.field.value]);

const filteredProcessedVals = useMemo(
() =>
vals
.filter((x) => !x.is_custom || x.position_id === initialSelectedID)
.map((v) => ({
label: v.name,
value: v.position_id.toString(10),
})),
[vals, initialSelectedID],
);

return (
<SelectWithCustomOption
data={[
...vals.map((val) => ({
label: val.name,
value: val.position_id.toString(10),
})),
].filter(Boolean)}
data={filteredProcessedVals}
value={value}
isCustomValue={isCustom}
onChange={(newValue, isNew) => {
Expand Down
4 changes: 2 additions & 2 deletions app/(authenticated)/calendar/[eventID]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EventObjectType, getEvent } from "@/features/calendar/events";
import {
canManage,
canManageAnySignupSheet,
getAllNonCustomCrewPositions,
getAllCrewPositions,
} from "@/features/calendar";
import {
CrewPositionsProvider,
Expand Down Expand Up @@ -90,7 +90,7 @@ async function ShowView({
// TODO(WEB-40): this pre-loads quite a bit of information that we don't actually need until you go to edit a sheet.
// Would be better to either load it on-demand dynamically, or move the edit view to a sub-page.
const [positions, members] = await Promise.all([
getAllNonCustomCrewPositions(),
getAllCrewPositions(),
getAllUsers(),
]);
return (
Expand Down
6 changes: 2 additions & 4 deletions features/calendar/crew_positions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ export interface CrewPositionType {
admin: boolean;
brief_description: string;
full_description: string;
is_custom: boolean;
}

export function getAllNonCustomCrewPositions(): Promise<CrewPositionType[]> {
export function getAllCrewPositions(): Promise<CrewPositionType[]> {
return prisma.position.findMany({
orderBy: {
position_id: "asc",
},
where: {
is_custom: false,
},
});
}