@@ -12,20 +12,29 @@ export const emptyBusinessLocation = (): BusinessLocationPath => ({
|
||||
locality_id: '',
|
||||
});
|
||||
|
||||
export function decodeBusinessLocations(value?: string): BusinessLocationPath[] {
|
||||
export function decodeBusinessLocations(
|
||||
value?: string,
|
||||
): BusinessLocationPath[] {
|
||||
if (!value) return [];
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(value);
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed.slice(0, 20).map((entry) => {
|
||||
const row = entry && typeof entry === 'object'
|
||||
? (entry as Record<string, unknown>)
|
||||
: {};
|
||||
const row =
|
||||
entry && typeof entry === 'object'
|
||||
? (entry as Record<string, unknown>)
|
||||
: {};
|
||||
return {
|
||||
province_id: typeof row.province_id === 'string' ? row.province_id : '',
|
||||
district_id: typeof row.district_id === 'string' ? row.district_id : '',
|
||||
admin_post_id: typeof row.admin_post_id === 'string' ? row.admin_post_id : '',
|
||||
locality_id: typeof row.locality_id === 'string' ? row.locality_id : '',
|
||||
province_id:
|
||||
typeof row.province_id === 'string' ? row.province_id : '',
|
||||
district_id:
|
||||
typeof row.district_id === 'string' ? row.district_id : '',
|
||||
admin_post_id:
|
||||
typeof row.admin_post_id === 'string'
|
||||
? row.admin_post_id
|
||||
: '',
|
||||
locality_id:
|
||||
typeof row.locality_id === 'string' ? row.locality_id : '',
|
||||
};
|
||||
});
|
||||
} catch {
|
||||
@@ -46,7 +55,9 @@ export function updateBusinessLocation(
|
||||
if (!rows[index]) return rows;
|
||||
const next = rows.map((row) => ({ ...row }));
|
||||
next[index][key] = value;
|
||||
const children: Partial<Record<keyof BusinessLocationPath, (keyof BusinessLocationPath)[]>> = {
|
||||
const children: Partial<
|
||||
Record<keyof BusinessLocationPath, (keyof BusinessLocationPath)[]>
|
||||
> = {
|
||||
province_id: ['district_id', 'admin_post_id', 'locality_id'],
|
||||
district_id: ['admin_post_id', 'locality_id'],
|
||||
admin_post_id: ['locality_id'],
|
||||
@@ -55,18 +66,28 @@ export function updateBusinessLocation(
|
||||
return next;
|
||||
}
|
||||
|
||||
export function hasDuplicateBusinessLocationPath(rows: BusinessLocationPath[]): boolean {
|
||||
const paths = rows.map((row) => [
|
||||
row.province_id,
|
||||
row.district_id,
|
||||
row.admin_post_id,
|
||||
row.locality_id,
|
||||
].filter(Boolean));
|
||||
return paths.some((path, index) => path.length > 0 && paths.slice(index + 1).some((other) => {
|
||||
if (other.length === 0) return false;
|
||||
const common = Math.min(path.length, other.length);
|
||||
return path.slice(0, common).every((id, part) => id === other[part]);
|
||||
}));
|
||||
export function hasDuplicateBusinessLocationPath(
|
||||
rows: BusinessLocationPath[],
|
||||
): boolean {
|
||||
const paths = rows.map((row) =>
|
||||
[
|
||||
row.province_id,
|
||||
row.district_id,
|
||||
row.admin_post_id,
|
||||
row.locality_id,
|
||||
].filter(Boolean),
|
||||
);
|
||||
return paths.some(
|
||||
(path, index) =>
|
||||
path.length > 0 &&
|
||||
paths.slice(index + 1).some((other) => {
|
||||
if (other.length === 0) return false;
|
||||
const common = Math.min(path.length, other.length);
|
||||
return path
|
||||
.slice(0, common)
|
||||
.every((id, part) => id === other[part]);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function locationPathFromPayload(
|
||||
|
||||
Reference in New Issue
Block a user