Skip to content

Commit

Permalink
fix(extension): suppress 404 error
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 committed Jul 17, 2024
1 parent a2c4b2f commit 9df2aef
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
34 changes: 26 additions & 8 deletions extension/src/shared/api/clients/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,45 @@ export class PlateauAPIClient<V> {
}

handleError(obj: any) {
if (!obj) return [];
if (typeof obj === "object" && "error" in obj) return [];
if (!obj) return;
if (typeof obj !== "object") return;
if ("error" in obj) {
console.error(obj.error);
return;
}
return obj;
}

// Combine the data only in findAll.
async findAll(): Promise<V[]> {
async findAll(): Promise<V[] | undefined> {
return this.handleError(
await Promise.all<(Promise<V[]> | undefined)[]>([
fetchWithGet(this.baseUrl()),
this.cityOptions ? fetchWithGet(this.baseUrlForCity()) : undefined,
]).then(r => r.filter((v): v is V[] => !!v).flat()),
])
.then(r => r.filter((v): v is V[] => !!v).flat())
.catch(e => {
console.error(e);
return [];
}),
);
}

async findAllForCity(): Promise<V[]> {
return this.handleError(await fetchWithGet(this.baseUrlForCity()));
async findAllForCity(): Promise<V[] | undefined> {
return this.handleError(
await fetchWithGet(this.baseUrlForCity()).catch(e => {
console.error(e);
return [];
}),
);
}

async findById(id: string): Promise<V> {
return this.handleError(await fetchWithGet(this.urlWithId(id)));
async findById(id: string): Promise<V | undefined> {
return this.handleError(
await fetchWithGet(this.urlWithId(id)).catch(e => {
console.error(e);
}),
);
}

async save(data: V) {
Expand Down
2 changes: 1 addition & 1 deletion extension/src/shared/api/hooks/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default () => {
setIsSaving(true);

const settings = await client.findAll();
const existSetting = settings.find(
const existSetting = settings?.find(
s => s.datasetId === setting.datasetId && s.dataId === setting.dataId,
);

Expand Down
2 changes: 1 addition & 1 deletion extension/src/shared/api/hooks/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default () => {
useEffect(() => {
if (isCityProject) {
client.findAllForCity().then(data => {
setTemplates(data);
setTemplates(data ?? []);
});
} else {
setTemplates(templatesFromAtom);
Expand Down
5 changes: 3 additions & 2 deletions extension/src/shared/view-layers/rootLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const findComponentTemplate = (

// Default template
const templateWithName = dataName
? templates.find(t => [dataName, dataSubName].includes(t.name.split("/").slice(-1)[0]))
? templates.find(t => [dataName, dataSubName].includes(t.name?.split("/").slice(-1)[0]))
: undefined;

const template =
Expand All @@ -186,7 +186,8 @@ const findEmphasisProperties = (
const templateWithName = dataName
? templates.find(
t =>
t.type === "emphasis" && [dataName, dataSubName].includes(t.name.split("/").slice(-1)[0]),
t.type === "emphasis" &&
[dataName, dataSubName].includes(t.name?.split("/").slice(-1)[0]),
)
: undefined;

Expand Down
4 changes: 2 additions & 2 deletions extension/src/toolbar/containers/InitializeApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const InitializeApp: FC = () => {
settingClient.findAll(),
templateClient.findAll(),
]);
updateAllSetting(settings);
updateAllTemplate(Array.isArray(templates) ? templates : []);
updateAllSetting(settings ?? []);
updateAllTemplate(templates ?? []);
setIsAppReady(true);
};
fetch();
Expand Down

0 comments on commit 9df2aef

Please sign in to comment.