2023-02-05 05:19:01 -08:00
|
|
|
import { MouseEvent, useState } from 'react';
|
2023-01-06 00:39:49 -08:00
|
|
|
import type { ICellRendererParams } from '@ag-grid-community/core';
|
|
|
|
|
import { Rating } from '/@/renderer/components/rating';
|
|
|
|
|
import { CellContainer } from '/@/renderer/components/virtual-table/cells/generic-cell';
|
2023-02-05 05:19:01 -08:00
|
|
|
import { useUpdateRating } from '/@/renderer/components/virtual-table/hooks/use-rating';
|
2023-01-06 00:39:49 -08:00
|
|
|
|
|
|
|
|
export const RatingCell = ({ value }: ICellRendererParams) => {
|
2023-02-05 05:19:01 -08:00
|
|
|
const updateRatingMutation = useUpdateRating();
|
|
|
|
|
const [ratingValue, setRatingValue] = useState(value?.userRating);
|
|
|
|
|
|
|
|
|
|
const handleUpdateRating = (rating: number) => {
|
|
|
|
|
if (!value) return;
|
|
|
|
|
|
|
|
|
|
updateRatingMutation.mutate({
|
|
|
|
|
_serverId: value?.serverId,
|
|
|
|
|
query: {
|
|
|
|
|
item: [value],
|
|
|
|
|
rating,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setRatingValue(rating);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleClearRating = (e: MouseEvent<HTMLDivElement>) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
updateRatingMutation.mutate({
|
|
|
|
|
_serverId: value?.serverId,
|
|
|
|
|
query: {
|
|
|
|
|
item: [value],
|
|
|
|
|
rating: 0,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setRatingValue(0);
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-06 00:39:49 -08:00
|
|
|
return (
|
|
|
|
|
<CellContainer position="center">
|
|
|
|
|
<Rating
|
2023-02-05 05:19:01 -08:00
|
|
|
defaultValue={value?.userRating || 0}
|
2023-01-06 00:39:49 -08:00
|
|
|
size="xs"
|
2023-02-05 05:19:01 -08:00
|
|
|
value={ratingValue}
|
|
|
|
|
onChange={handleUpdateRating}
|
|
|
|
|
onClick={handleClearRating}
|
2023-01-06 00:39:49 -08:00
|
|
|
/>
|
|
|
|
|
</CellContainer>
|
|
|
|
|
);
|
|
|
|
|
};
|