UPDATE
This commit is contained in:
@@ -23,6 +23,7 @@ export interface ApiResponse<T> {
|
||||
}
|
||||
|
||||
export interface FarmDashboardConfigResponse {
|
||||
farm_uuid?: string;
|
||||
disabled_card_ids: string[];
|
||||
row_order: string[];
|
||||
enable_drag_reorder?: boolean;
|
||||
@@ -48,7 +49,7 @@ export interface FarmDashboardCardsResponse {
|
||||
}
|
||||
|
||||
interface FarmDashboardCardsTaskResult {
|
||||
sensor_id?: string;
|
||||
farm_uuid?: string;
|
||||
all_cards?: FarmDashboardCardsResponse;
|
||||
}
|
||||
|
||||
@@ -58,7 +59,11 @@ interface FarmDashboardCardsTaskData {
|
||||
result?: FarmDashboardCardsTaskResult;
|
||||
}
|
||||
|
||||
const STORAGE_KEY = "farm_dashboard_config";
|
||||
const STORAGE_KEY_PREFIX = "farm_dashboard_config";
|
||||
|
||||
function getStorageKey(farmUuid: string): string {
|
||||
return `${STORAGE_KEY_PREFIX}:${farmUuid}`;
|
||||
}
|
||||
|
||||
function isCardId(value: string): value is CardId {
|
||||
return (CARD_IDS as readonly string[]).includes(value);
|
||||
@@ -149,34 +154,38 @@ function toApiRequest(
|
||||
/**
|
||||
* localStorage fallback when backend is not ready
|
||||
*/
|
||||
function getLocalConfig(): FarmDashboardConfig | null {
|
||||
function getLocalConfig(farmUuid: string): FarmDashboardConfig | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
const stored = localStorage.getItem(getStorageKey(farmUuid));
|
||||
return stored ? (JSON.parse(stored) as FarmDashboardConfig) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function setLocalConfig(config: FarmDashboardConfig): void {
|
||||
function setLocalConfig(farmUuid: string, config: FarmDashboardConfig): void {
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
|
||||
localStorage.setItem(getStorageKey(farmUuid), JSON.stringify(config));
|
||||
} catch (e) {
|
||||
console.error("Failed to save farm dashboard config to localStorage", e);
|
||||
}
|
||||
}
|
||||
|
||||
function buildFarmQuery(farmUuid: string): string {
|
||||
return `farm_uuid=${encodeURIComponent(farmUuid)}`;
|
||||
}
|
||||
|
||||
export const farmDashboardService = {
|
||||
/**
|
||||
* Get farm dashboard config for current user
|
||||
* Get farm dashboard config for the selected farm
|
||||
*/
|
||||
async getConfig(): Promise<FarmDashboardConfig> {
|
||||
async getConfig(farmUuid: string): Promise<FarmDashboardConfig> {
|
||||
try {
|
||||
const response = await apiClient.get<
|
||||
ApiResponse<FarmDashboardConfigResponse> | FarmDashboardConfigResponse
|
||||
>("/api/farm-dashboard-config/");
|
||||
>(`/api/farm-dashboard-config/?${buildFarmQuery(farmUuid)}`);
|
||||
const raw = response && "data" in response ? response.data : response;
|
||||
if (
|
||||
raw &&
|
||||
@@ -187,7 +196,7 @@ export const farmDashboardService = {
|
||||
}
|
||||
throw new Error("Invalid response");
|
||||
} catch {
|
||||
const local = getLocalConfig();
|
||||
const local = getLocalConfig(farmUuid);
|
||||
if (local) return local;
|
||||
return { disabledCardIds: [], rowOrder: [], enableDragReorder: true };
|
||||
}
|
||||
@@ -197,12 +206,16 @@ export const farmDashboardService = {
|
||||
* Update farm dashboard config
|
||||
*/
|
||||
async updateConfig(
|
||||
farmUuid: string,
|
||||
data: Partial<FarmDashboardConfig>,
|
||||
): Promise<FarmDashboardConfig> {
|
||||
try {
|
||||
const response = await apiClient.patch<
|
||||
ApiResponse<FarmDashboardConfigResponse> | FarmDashboardConfigResponse
|
||||
>("/api/farm-dashboard-config/", toApiRequest(data));
|
||||
>("/api/farm-dashboard-config/", {
|
||||
farm_uuid: farmUuid,
|
||||
...toApiRequest(data),
|
||||
});
|
||||
const raw = response && "data" in response ? response.data : response;
|
||||
if (
|
||||
raw &&
|
||||
@@ -210,12 +223,12 @@ export const farmDashboardService = {
|
||||
("disabled_card_ids" in raw || "row_order" in raw)
|
||||
) {
|
||||
const config = fromApiResponse(raw as FarmDashboardConfigResponse);
|
||||
setLocalConfig(config);
|
||||
setLocalConfig(farmUuid, config);
|
||||
return config;
|
||||
}
|
||||
throw new Error("Update failed");
|
||||
} catch (err) {
|
||||
const local = getLocalConfig();
|
||||
const local = getLocalConfig(farmUuid);
|
||||
if (local) {
|
||||
const merged: FarmDashboardConfig = {
|
||||
disabledCardIds: data.disabledCardIds ?? local.disabledCardIds,
|
||||
@@ -223,7 +236,7 @@ export const farmDashboardService = {
|
||||
enableDragReorder:
|
||||
data.enableDragReorder ?? local.enableDragReorder ?? true,
|
||||
};
|
||||
setLocalConfig(merged);
|
||||
setLocalConfig(farmUuid, merged);
|
||||
return merged;
|
||||
}
|
||||
throw err;
|
||||
@@ -234,7 +247,9 @@ export const farmDashboardService = {
|
||||
* Get all dashboard card data from API
|
||||
* Response: { code: 200, msg: "OK", data: { farmOverviewKpis, farmWeatherCard, ... } }
|
||||
*/
|
||||
async getAllCards(): Promise<
|
||||
async getAllCards(
|
||||
farmUuid: string,
|
||||
): Promise<
|
||||
Partial<Record<CardId, Record<string, unknown>>>
|
||||
> {
|
||||
try {
|
||||
@@ -243,7 +258,7 @@ export const farmDashboardService = {
|
||||
| ApiResponse<FarmDashboardCardsTaskData>
|
||||
| FarmDashboardCardsResponse
|
||||
| FarmDashboardCardsTaskData
|
||||
>("/api/farm-dashboard/");
|
||||
>(`/api/farm-dashboard/?${buildFarmQuery(farmUuid)}`);
|
||||
return extractCardsPayload(response);
|
||||
} catch {
|
||||
return {};
|
||||
|
||||
Reference in New Issue
Block a user