-
-
Notifications
You must be signed in to change notification settings - Fork 312
feat: User preferences storage & admin: sorted billing plans by populality #3266
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package io.tolgee.dtos.request | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties | ||
| import io.swagger.v3.oas.annotations.media.Schema | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| data class UserStorageResponse( | ||
| @field:Schema(description = "The data stored for the field") | ||
| var data: Any? = null, | ||
| ) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,8 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { T } from '@tolgee/react'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { Box } from '@mui/material'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { BoxLoading } from 'tg.component/common/BoxLoading'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { useUserPreferenceStorage } from 'tg.hooks/useUserPreferenceStorage'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| type GenericPlanType = { id: number; name: string }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -15,6 +17,7 @@ export interface GenericPlanSelector<T extends GenericPlanType> { | |||||||||||||||||||||||||||||||||||||||||||||||
| onChange?: (value: number) => void; | ||||||||||||||||||||||||||||||||||||||||||||||||
| selectProps?: React.ComponentProps<typeof SearchSelect>[`SelectProps`]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| plans?: T[]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| loading: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| export const GenericPlanSelector = <T extends GenericPlanType>({ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -23,7 +26,12 @@ export const GenericPlanSelector = <T extends GenericPlanType>({ | |||||||||||||||||||||||||||||||||||||||||||||||
| selectProps, | ||||||||||||||||||||||||||||||||||||||||||||||||
| onPlanChange, | ||||||||||||||||||||||||||||||||||||||||||||||||
| plans, | ||||||||||||||||||||||||||||||||||||||||||||||||
| loading, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }: GenericPlanSelector<T>) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (loading) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return <BoxLoading />; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+34
Contributor
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. Fix conditional hook usage (violates Rules of Hooks). Hooks are called after early returns (loading/no plans) and indirectly via useSortPlans. Move hooks to top level. export const GenericPlanSelector = <T extends GenericPlanType>({
onChange,
value,
selectProps,
onPlanChange,
plans,
loading,
}: GenericPlanSelector<T>) => {
- if (loading) {
- return <BoxLoading />;
- }
+ const { incrementPlanWithId } = usePreferredPlans();
+ const sortedPlans = useSortPlans(plans || []);
+
+ if (loading) {
+ return <BoxLoading />;
+ }
if (!plans) {
return (
<Box>
<T keyName="administration-assign-plan-no-plans-to-assign" />
</Box>
);
}
- const { incrementPlanWithId } = usePreferredPlans();
- const sortedPlans = useSortPlans(plans);Also applies to: 51-53, 85-95, 100-118 |
||||||||||||||||||||||||||||||||||||||||||||||||
| if (!plans) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||
| <Box> | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,12 +48,16 @@ export const GenericPlanSelector = <T extends GenericPlanType>({ | |||||||||||||||||||||||||||||||||||||||||||||||
| } satisfies SelectItem<number>) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const { incrementPlanWithId } = usePreferredPlans(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const sortedPlans = useSortPlans(plans); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| function handleChange(planId: number) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (plans) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const plan = plans.find((plan) => plan.id === planId); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const plan = sortedPlans.find((plan) => plan.id === planId); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (plan) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| onChange?.(planId); | ||||||||||||||||||||||||||||||||||||||||||||||||
| onPlanChange?.(plan); | ||||||||||||||||||||||||||||||||||||||||||||||||
| onPlanChange?.(plan as T); | ||||||||||||||||||||||||||||||||||||||||||||||||
| incrementPlanWithId(planId); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -65,3 +77,42 @@ export const GenericPlanSelector = <T extends GenericPlanType>({ | |||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||
| * Sorts plans by user's preferred plans. | ||||||||||||||||||||||||||||||||||||||||||||||||
| * The purpose of this is to put the user's popular plans to the top. | ||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||
| function useSortPlans(plans: GenericPlanType[]) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const { preferredPlansLoadable } = usePreferredPlans(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return React.useMemo(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return [...plans].sort( | ||||||||||||||||||||||||||||||||||||||||||||||||
| (a, b) => | ||||||||||||||||||||||||||||||||||||||||||||||||
| (preferredPlansLoadable.data?.data?.[b.id] || 0) - | ||||||||||||||||||||||||||||||||||||||||||||||||
| (preferredPlansLoadable.data?.data?.[a.id] || 0) | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, [plans, preferredPlansLoadable.data]); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns user's preferred plans and a function to increment a plan's count. | ||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||
| function usePreferredPlans() { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const { loadable, update } = useUserPreferenceStorage( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 'billingAdminPreferredPlans' | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||
| preferredPlansLoadable: loadable, | ||||||||||||||||||||||||||||||||||||||||||||||||
| incrementPlanWithId: async (planId: number) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const refetched = await loadable.refetch(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| const current = refetched.data?.data[planId] ?? 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
| const newValue = { | ||||||||||||||||||||||||||||||||||||||||||||||||
| ...refetched.data, | ||||||||||||||||||||||||||||||||||||||||||||||||
| [planId]: current + 1, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| update(newValue); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+109
to
+118
Contributor
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. Bug: newValue spreads the whole response, not the stored map. You should spread the inner return {
preferredPlansLoadable: loadable,
incrementPlanWithId: async (planId: number) => {
const refetched = await loadable.refetch();
- const current = refetched.data?.data[planId] ?? 0;
- const newValue = {
- ...refetched.data,
- [planId]: current + 1,
- };
+ const currentMap = (refetched.data?.data ?? {}) as Record<string, number>;
+ const newValue = {
+ ...currentMap,
+ [planId]: (currentMap[planId] ?? 0) + 1,
+ };
update(newValue);
},
};📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { useApiMutation, useApiQuery } from 'tg.service/http/useQueryApi'; | ||
| import { UseQueryResult } from 'react-query'; | ||
|
|
||
| export function useUserPreferenceStorage(fieldName: string) { | ||
| const loadable = useApiQuery({ | ||
| url: '/v2/user-preferences/storage/{fieldName}', | ||
| method: 'get', | ||
| path: { fieldName }, | ||
| }) as UseQueryResult<{ data: Record<number, number> }>; | ||
|
|
||
| const mutation = useApiMutation({ | ||
| url: '/v2/user-preferences/storage/{fieldName}', | ||
| method: 'put', | ||
| }); | ||
|
|
||
| return { | ||
| loadable, | ||
| update: (value: Record<string, any>) => { | ||
| mutation.mutate({ | ||
| path: { fieldName }, | ||
| content: { | ||
| 'application/json': value, | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.