-
Notifications
You must be signed in to change notification settings - Fork 7
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
[DEMO] Delete assignment/location #31
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,14 @@ import { | |
Input, | ||
EditableInput, | ||
} from '@chakra-ui/react'; | ||
import { CheckIcon, CloseIcon, EditIcon } from '@chakra-ui/icons'; | ||
import { CheckIcon, CloseIcon, DeleteIcon, EditIcon } from '@chakra-ui/icons'; | ||
import { Assignment, Location } from '@prisma/client'; | ||
import { UseTRPCMutationResult } from '@trpc/react/shared'; | ||
|
||
interface AdminCardProps { | ||
assignmentOrLocation: Assignment | Location; | ||
editMutation: UseTRPCMutationResult<any, any, any, any>; | ||
handleDelete: (id: number) => Promise<void>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every component has props that are typed. Try to avoid using |
||
} | ||
|
||
const EditableControls = () => { | ||
|
@@ -40,7 +41,7 @@ const EditableControls = () => { | |
* Component which represents a single assignment or location | ||
*/ | ||
const AdminCard = (props: AdminCardProps) => { | ||
const { assignmentOrLocation, editMutation } = props; | ||
const { assignmentOrLocation, editMutation, handleDelete } = props; | ||
const boxColor = useColorModeValue('gray.100', 'gray.700'); | ||
const [isChecked, setIsChecked] = useState(assignmentOrLocation.active); | ||
|
||
|
@@ -73,6 +74,7 @@ const AdminCard = (props: AdminCardProps) => { | |
Active? | ||
</Text> | ||
<Switch onChange={handleActiveChange} mt={2.5} ml={3} isChecked={isChecked} /> | ||
<DeleteIcon ml={5} mt={2} onClick={() => handleDelete(assignmentOrLocation.id)} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Render out the |
||
</Flex> | ||
</> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ const AdminView = () => { | |
const createLocationMutation = trpc.admin.createLocation.useMutation(); | ||
const editLocationMutation = trpc.admin.editLocation.useMutation(); | ||
const setSiteSettingsMutation = trpc.admin.setSiteSettings.useMutation(); | ||
const deleteAssignmentMutation = trpc.admin.deleteAssignment.useMutation(); | ||
const deleteLocationMutation = trpc.admin.deleteLocation.useMutation(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initialize the mutations that we created in |
||
|
||
useEffect(() => { | ||
if (siteSettings) { | ||
|
@@ -56,6 +58,17 @@ const AdminView = () => { | |
setLocations(prev => [...prev!, data]); | ||
}; | ||
|
||
const handleDeleteAssignment = async (id: number) => { | ||
await deleteAssignmentMutation.mutateAsync({ id }); | ||
// We can also just invalidate the query, but this makes the UI feel more responsive | ||
setAssignments(prev => prev!.filter(assignment => assignment.id !== id)); | ||
} | ||
|
||
const handleDeleteLocation = async (id: number) => { | ||
await deleteLocationMutation.mutateAsync({ id }); | ||
setLocations(prev => prev!.filter(location => location.id !== id)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two are essentially identical, but one is for assignments and one is for locations. The key here is that we call the |
||
// Sets the pending stage to enabled or disabled depending on the current state | ||
const handleTogglePendingStageEnabled = async () => { | ||
setIsPendingStageEnabled(prev => !prev); | ||
|
@@ -91,7 +104,7 @@ const AdminView = () => { | |
</Flex> | ||
</Flex> | ||
{assignments.map(assignment => ( | ||
<AdminCard key={assignment.id} assignmentOrLocation={assignment} editMutation={editAssignmentMutation} /> | ||
<AdminCard key={assignment.id} assignmentOrLocation={assignment} editMutation={editAssignmentMutation} handleDelete={handleDeleteAssignment} /> | ||
))} | ||
|
||
<Flex direction='column' w='50%' mt={10} mb={3}> | ||
|
@@ -106,7 +119,7 @@ const AdminView = () => { | |
</Flex> | ||
</Flex> | ||
{locations.map(location => ( | ||
<AdminCard key={location.id} assignmentOrLocation={location} editMutation={editLocationMutation} /> | ||
<AdminCard key={location.id} assignmentOrLocation={location} editMutation={editLocationMutation} handleDelete={handleDeleteLocation} /> | ||
))} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass in the approprite handler as a prop. The top is for assignments and the bottom is for locations. |
||
|
||
<Flex direction='column' mt={10} mb={3}> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,34 @@ export const adminRouter = router({ | |
}); | ||
}), | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is our backend. It's split into separate files (routers) such as Ignore the weird formatting: |
||
deleteLocation: protectedStaffProcedure | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the client we initialized this with "trpc.admin.deleteLocation.useMutation();" For queries we use |
||
.input( | ||
z.object({ | ||
id: z.number(), | ||
}), | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The input to this mutation is the ID that we want to delete. |
||
.mutation(async ({ input, ctx }) => { | ||
return ctx.prisma.location.delete({ | ||
where: { | ||
id: input.id, | ||
}, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is equivalent to writing raw SQL to delete from the database but no one wants to do that so we use the |
||
}), | ||
|
||
deleteAssignment: protectedStaffProcedure | ||
.input( | ||
z.object({ | ||
id: z.number(), | ||
}), | ||
) | ||
.mutation(async ({ input, ctx }) => { | ||
return ctx.prisma.assignment.delete({ | ||
where: { | ||
id: input.id, | ||
}, | ||
}); | ||
}), | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing for deleteAssignment |
||
setSiteSettings: protectedStaffProcedure | ||
.input( | ||
z.object({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trash icon is imported from Chakra (the component library we use)