Skip to content

Commit 165615b

Browse files
committed
Fetch steading/fronts from map
1 parent d00da5e commit 165615b

4 files changed

Lines changed: 178 additions & 194 deletions

File tree

src/services/campaignService.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const parseCampaign = (raw: any): Campaign | null => {
2525
};
2626
};
2727

28-
export const getCampaigns = async (userId: string): Promise<Campaign[]> => {
28+
export const getCampaigns = async (userId: string): Promise<Campaign[] | string> => {
2929
try {
3030
const { data } = await client.graphql({
3131
query: queries.listCampaigns,
@@ -39,59 +39,66 @@ export const getCampaigns = async (userId: string): Promise<Campaign[]> => {
3939
return rawItems.map(parseCampaign).filter(Boolean) as Campaign[];
4040
} catch (error) {
4141
console.error('Error fetching campaigns:', error);
42-
return [];
42+
return 'Error fetching campaigns';
4343
}
4444
};
4545

46-
export const getCampaign = async (id: string): Promise<Campaign | null> => {
46+
export const getCampaign = async (id: string): Promise<Campaign | string> => {
4747
try {
4848
const { data } = await client.graphql({
4949
query: queries.getCampaign,
5050
variables: { id }
5151
});
5252

53-
return parseCampaign((data as any)?.getCampaign);
53+
const parsed = parseCampaign((data as any)?.getCampaign);
54+
if (!parsed) return 'Failed to parse campaign';
55+
56+
return parsed;
5457
} catch (error) {
5558
console.error('Error fetching campaign:', error);
56-
return null;
59+
return 'Error fetching campaign';
5760
}
5861
};
5962

60-
export const createCampaign = async (campaign: Omit<Campaign, 'id'>): Promise<Campaign | null> => {
63+
export const createCampaign = async (campaign: Omit<Campaign, 'id'>): Promise<Campaign | string> => {
6164
try {
6265
const input = {
6366
id: uuid.generate(),
6467
...campaign
6568
};
6669

67-
const { data, errors } = await client.graphql({
70+
const result = await client.graphql({
6871
query: mutations.createCampaign,
6972
variables: { input }
7073
});
7174

75+
const data = (result as any)?.data;
76+
const errors = (result as any)?.errors;
77+
7278
if (errors?.length) {
7379
console.error('GraphQL error(s):', errors);
74-
return null;
80+
return errors[0]?.message || 'Unknown error creating campaign';
7581
}
7682

77-
return parseCampaign((data as any)?.createCampaign);
83+
const createdCampaign = parseCampaign(data?.createCampaign);
84+
if (!createdCampaign) return 'Failed to parse created campaign';
85+
86+
return createdCampaign;
7887
} catch (error) {
7988
console.error('Error creating campaign:', error);
80-
return null;
89+
return 'Error creating campaign';
8190
}
8291
};
8392

84-
export const updateCampaign = async (id: string, updates: Partial<Campaign>): Promise<boolean> => {
93+
export const updateCampaign = async (id: string, updates: Partial<Campaign>): Promise<boolean | string> => {
8594
try {
8695
const cleanedInput = { ...updates };
8796

88-
// Strip unwanted fields from campaign-level properties
8997
delete (cleanedInput as any).createdAt;
9098
delete (cleanedInput as any).updatedAt;
9199
delete (cleanedInput as any).owner;
92100
delete (cleanedInput as any).__typename;
93101

94-
// 🧼 Clean up sessions array (remove any unexpected fields)
95102
if (cleanedInput.sessions) {
96103
cleanedInput.sessions = cleanedInput.sessions.map((s: any) => ({
97104
id: s.id,
@@ -106,29 +113,38 @@ export const updateCampaign = async (id: string, updates: Partial<Campaign>): Pr
106113
...cleanedInput,
107114
};
108115

109-
await client.graphql({
116+
const result = await client.graphql({
110117
query: mutations.updateCampaign,
111118
variables: { input },
112119
});
113120

121+
const errors = (result as any)?.errors;
122+
if (errors?.length) {
123+
return errors[0]?.message || 'Unknown error updating campaign';
124+
}
125+
114126
return true;
115127
} catch (error) {
116128
console.error('Error updating campaign:', error);
117-
return false;
129+
return 'Error updating campaign';
118130
}
119131
};
120132

121-
122-
export const deleteCampaign = async (id: string): Promise<boolean> => {
133+
export const deleteCampaign = async (id: string): Promise<boolean | string> => {
123134
try {
124-
await client.graphql({
135+
const result = await client.graphql({
125136
query: mutations.deleteCampaign,
126137
variables: { input: { id } }
127138
});
128139

140+
const errors = (result as any)?.errors;
141+
if (errors?.length) {
142+
return errors[0]?.message || 'Unknown error deleting campaign';
143+
}
144+
129145
return true;
130146
} catch (error) {
131147
console.error('Error deleting campaign:', error);
132-
return false;
148+
return 'Error deleting campaign';
133149
}
134150
};

src/stores/campaignStore.ts

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,107 @@ import {
77
updateCampaign as updateCampaignService,
88
deleteCampaign as deleteCampaignService
99
} from '@/services/campaignService';
10+
import { getMap } from '@/services/mapService';
11+
import { getSteading } from '@/services/steadingService';
12+
import { getFront } from '@/services/frontService';
1013

1114
export const useCampaignStore = defineStore('campaigns', {
1215
state: () => ({
1316
campaigns: [] as Campaign[],
1417
selectedCampaign: null as Campaign | null,
18+
linkedSteadings: [] as any[],
19+
linkedFronts: [] as any[],
1520
}),
1621
actions: {
17-
async fetchCampaigns(userId: string) {
18-
try {
19-
this.campaigns = await getCampaigns(userId);
20-
} catch (error) {
21-
console.error('Failed to fetch campaigns', error);
22+
async fetchCampaigns(userId: string): Promise<true | string> {
23+
const result = await getCampaigns(userId);
24+
if (typeof result === 'string') {
25+
console.error('Failed to fetch campaigns', result);
26+
return result;
2227
}
28+
this.campaigns = result;
29+
return true;
2330
},
2431

25-
async createCampaign(campaignData: Partial<Campaign>, userId: string) {
26-
try {
27-
const created = await createCampaignService({
28-
...campaignData,
29-
userId
30-
} as Omit<Campaign, 'id'>);
32+
async createCampaign(campaignData: Partial<Campaign>, userId: string): Promise<true | string> {
33+
const result = await createCampaignService({
34+
...campaignData,
35+
userId
36+
} as Omit<Campaign, 'id'>);
3137

32-
if (created) {
33-
this.campaigns.push(created);
34-
}
35-
} catch (error) {
36-
console.error('Failed to create campaign', error);
38+
if (typeof result === 'string') {
39+
console.error('Failed to create campaign', result);
40+
return result;
3741
}
42+
43+
this.campaigns.push(result);
44+
return true;
3845
},
3946

40-
async updateCampaign(campaignId: string, updates: Partial<Campaign>) {
41-
try {
42-
const success = await updateCampaignService(campaignId, updates);
43-
if (success) {
44-
await this.fetchCampaigns(updates.userId!); // assumes userId exists in updates
45-
}
46-
} catch (error) {
47-
console.error('Failed to update campaign', error);
47+
async updateCampaign(campaignId: string, updates: Partial<Campaign>): Promise<true | string> {
48+
const result = await updateCampaignService(campaignId, updates);
49+
50+
if (typeof result === 'string') {
51+
console.error('Failed to update campaign', result);
52+
return result;
4853
}
54+
55+
if (updates.userId) {
56+
await this.fetchCampaigns(updates.userId);
57+
}
58+
return true;
4959
},
5060

51-
async deleteCampaign(campaignId: string) {
52-
try {
53-
const success = await deleteCampaignService(campaignId);
54-
if (success) {
55-
this.campaigns = this.campaigns.filter(c => c.id !== campaignId);
56-
}
57-
} catch (error) {
58-
console.error('Failed to delete campaign', error);
61+
async deleteCampaign(campaignId: string): Promise<true | string> {
62+
const result = await deleteCampaignService(campaignId);
63+
if (typeof result === 'string') {
64+
console.error('Failed to delete campaign', result);
65+
return result;
5966
}
67+
this.campaigns = this.campaigns.filter(c => c.id !== campaignId);
68+
return true;
6069
},
6170

62-
async fetchCampaignById(id: string) {
63-
try {
64-
this.selectedCampaign = await getCampaign(id);
65-
} catch (error) {
66-
console.error('Failed to fetch campaign by ID', error);
71+
async fetchCampaignById(id: string): Promise<true | string> {
72+
const result = await getCampaign(id);
73+
if (typeof result === 'string') {
74+
console.error('Failed to fetch campaign by ID', result);
75+
return result;
6776
}
77+
this.selectedCampaign = result;
78+
await this.loadLinkedEntities();
79+
return true;
80+
},
81+
82+
async loadLinkedEntities() {
83+
if (!this.selectedCampaign) return;
84+
const mapIds = this.selectedCampaign.mapIds || [];
85+
86+
const loadedMaps = await Promise.all(mapIds.map(id => getMap(id)));
87+
88+
const frontIds = new Set<string>();
89+
const steadingIds = new Set<string>();
90+
91+
for (const map of loadedMaps.filter(Boolean)) {
92+
const locations = typeof map?.locations === 'string' ? JSON.parse(map.locations) : map?.locations;
93+
94+
for (const loc of locations || []) {
95+
if (loc.steading_id) steadingIds.add(loc.steading_id);
96+
if (Array.isArray(loc.fronts)) loc.fronts.forEach((fid: string) => frontIds.add(fid));
97+
}
98+
}
99+
100+
const [steadings, fronts] = await Promise.all([
101+
Promise.all(Array.from(steadingIds).map(id => getSteading(id))),
102+
Promise.all(Array.from(frontIds).map(id => getFront(id)))
103+
]);
104+
105+
this.linkedSteadings = steadings.filter(Boolean);
106+
this.linkedFronts = fronts.filter(Boolean);
68107
},
69108

70109
selectCampaign(id: string) {
71110
this.selectedCampaign = this.campaigns.find(c => c.id === id) || null;
72111
},
73112
},
74-
});
113+
});

0 commit comments

Comments
 (0)