Skip to content

Commit

Permalink
added api manager error handing
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Dec 14, 2024
1 parent 1aa8354 commit 9621d8b
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 55 deletions.
5 changes: 2 additions & 3 deletions src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const Users = () => {
const [userQuery, setQuery] = useState<string>('');

useEffect(() => {
ApiManager.getUsers(userQuery).then(setUsers);
ApiManager.getUsers(userQuery).then(setUsers).catch(console.error);
}, [userQuery]);

const updateUser = (user: UserAdmins, data: { banned?: boolean, skip_ppl_check?: boolean }): Promise<void> => {
Expand Down Expand Up @@ -147,9 +147,8 @@ const Admin = () => {
router.replace('/');
return;
}

setUser(data);
})
}).catch(console.error);
}, [])

const updateUsers = user && (user.permissions.includes('updateusers') || user.permissions.includes('superadmin'));
Expand Down
2 changes: 1 addition & 1 deletion src/app/me/notifications/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Notifications = () => {

useEffect(() => {
if (page < 0) return;
ApiManager.getMeNotifications({ page }).then(setNotifications);
ApiManager.getMeNotifications({ page }).then(setNotifications).catch(console.error);
}, [page]);

const notifications_el = notifications?.data.map((notification) => {
Expand Down
4 changes: 2 additions & 2 deletions src/app/me/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const Main = () => {
}, []);

useEffect(() => {
isLogged && ApiManager.getMeWorks().then(setData);
isLogged && ApiManager.getMeWorks().then(setData).catch(console.error);
}, [isLogged]);

return (
Expand Down Expand Up @@ -112,7 +112,7 @@ const Login = () => {
})

useEffect(() => {
ApiManager.getRoles().then(setRoles);
ApiManager.getRoles().then(setRoles).catch(console.error);
}, [])

return (
Expand Down
12 changes: 7 additions & 5 deletions src/app/me/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const Page = () => {
queryKey: ["userConnections"],
retry: false,
queryFn: async () => {
const res = await ApiManager.getMeSettings();
const res = await ApiManager.getMeSettings().catch(console.error);
setLoaded(true);
return res;
},
Expand Down Expand Up @@ -144,7 +144,7 @@ const Connections = ({ data, refetch }: { data: SettingsResponse, refetch(): voi
const confirmed = confirm("Отвязать учётную запись Minecraft? Вы сможете в любое время привязать ее обратно.");
if (!confirmed) return;

ApiManager.disconnectMinecraft().then(refetch);
ApiManager.disconnectMinecraft().then(refetch).catch(console.error);
}

const setValidAPI = (state: boolean): Promise<void> => {
Expand Down Expand Up @@ -339,19 +339,21 @@ const Safety = () => {
))
);
setLoading(false);
})
}).catch(console.error);
}, []);

const logoutSession = (session_id: number) => {
if (!confirm(`Выйти с этого устройства?`)) return;
ApiManager.logoutSession(session_id).then(() => setSessions(sessions.filter(session_ => session_.id !== session_id)))
ApiManager.logoutSession(session_id)
.then(() => setSessions(sessions.filter(session_ => session_.id !== session_id)))
.catch(response => alert(response.data.message_ru || response.data.message));
}

const logoutSessionAll = () => {
if (!confirm('Выйти со всех устройств, кроме этого?')) return;
ApiManager.logoutAllSessions()
.then(() => setSessions(sessions.filter(session_ => session_.is_self)))
.catch(response => alert(response.data.message));
.catch(response => alert(response.data.message_ru || response.data.message));
}

const sessions_elements = sessions.map(session =>
Expand Down
2 changes: 1 addition & 1 deletion src/app/me/stars/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Main = () => {
}

useEffect(() => {
ApiManager.getMeStars().then(data => setData(data.reverse()));
ApiManager.getMeStars().then(data => setData(data.reverse())).catch(console.error);
}, []);


Expand Down
1 change: 1 addition & 0 deletions src/app/modules/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const StarElement = ({ el }: { el: Bandage }) => {
if (logged && starred != last) {
ApiManager.setStar(el.external_id, { set: starred })
.then(data => setStarsCount(data.new_count))
.catch(console.error)
.finally(() => setLast(starred));
}
}, [starred]);
Expand Down
10 changes: 6 additions & 4 deletions src/app/modules/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ const Header = (): JSX.Element => {
};

const logout = () => {
ApiManager.logout().then(() => {
deleteCookie('sessionId');
window.location.assign('/');
});
ApiManager.logout()
.then(() => {
deleteCookie('sessionId');
window.location.assign('/');
})
.catch(console.error);
}

interface AvatarMenuProps {
Expand Down
1 change: 1 addition & 0 deletions src/app/modules/components/NickSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const Searcher = ({ onChange }: SearchProps) => {
}
]);
})
.catch(console.error)
.finally(() => setLoading(false));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/components/ThemeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Menu = ({ initialValue, color_available, onChange }: MenuProps) => {

useEffect(() => {
onChange(theme);
firstLoad ? setFirstLoad(false) : ApiManager.setTheme({ theme });
firstLoad ? setFirstLoad(false) : ApiManager.setTheme({ theme }).catch(console.error);
}, [theme]);


Expand Down
2 changes: 1 addition & 1 deletion src/app/tutorials/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function Home() {
const [roles, setRoles] = useState<Role[]>([]);

useEffect(() => {
ApiManager.getRoles().then(setRoles);
ApiManager.getRoles().then(setRoles).catch(console.error);
}, [])

const roles_data = roles.map((role) => {
Expand Down
4 changes: 2 additions & 2 deletions src/app/workshop/[id]/components/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const EditElement = ({
const [accessLevel, setAccessLevel] = useState<number>(undefined);

useEffect(() => {
ApiManager.getCategories(true).then(setAllCategories);
ApiManager.getCategories(true).then(setAllCategories).catch(console.error);
}, []);

function capitalize(string: string) {
Expand Down Expand Up @@ -76,7 +76,7 @@ const EditElement = ({
if (!confirm('Заархивировать повязку? После архивации её будет невозможно изменить!')) return;
ApiManager.archiveBandage(bandage.external_id)
.then(window.location.reload)
.catch(err => alert(err.data.message));
.catch(err => alert(err.data.message_ru || err.data.message));
}

return <div style={{ display: "flex", flexDirection: "column", gap: ".8rem" }}>
Expand Down
46 changes: 24 additions & 22 deletions src/app/workshop/[id]/skinLoad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,32 @@ const SkinLoad = ({ onChange }: SkinLoadProps) => {
return;
}

ApiManager.getSkin(nickname).then(response => {
if (response.status !== 200) {
switch (response.status) {
case 404:
setError("Игрок с таким никнеймом не найден!");
break;
case 429:
setError("Сервера Mojang перегружены, пожалуйста, попробуйте через пару минут");
break;
default:
setError(`Не удалось получить ник! (${response.status})`);
break;
ApiManager.getSkin(nickname)
.then(response => {
if (response.status !== 200) {
switch (response.status) {
case 404:
setError("Игрок с таким никнеймом не найден!");
break;
case 429:
setError("Сервера Mojang перегружены, пожалуйста, попробуйте через пару минут");
break;
default:
setError(`Не удалось получить ник! (${response.status})`);
break;
}
return;
}
return;
}

const data = response.data as SkinResponse;
setData({
data: b64Prefix + data.data.skin.data,
slim: data.data.skin.slim,
cape: data.data.cape
});
setLoaded(true);
});
const data = response.data as SkinResponse;
setData({
data: b64Prefix + data.data.skin.data,
slim: data.data.skin.slim,
cape: data.data.cape
});
setLoaded(true);
})
.catch(console.error);
}

const setError = (err: string) => {
Expand Down
18 changes: 10 additions & 8 deletions src/app/workshop/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,17 @@ const Editor = ({
const [useOldMethod, setUseOldMethod] = useState<boolean>(false);

useEffect(() => {
ApiManager.getCategories(true).then(data => {
setAllCategories(data);
if (window.location.hash === '#colorable') {
const colorable_category = data.find(category => category.colorable);
if (colorable_category) {
setEnabledCategories([colorable_category]);
ApiManager.getCategories(true)
.then(data => {
setAllCategories(data);
if (window.location.hash === '#colorable') {
const colorable_category = data.find(category => category.colorable);
if (colorable_category) {
setEnabledCategories([colorable_category]);
}
}
}
});
})
.catch(console.error);
}, []);

const debouncedHandleColorChange = useCallback(
Expand Down
12 changes: 7 additions & 5 deletions src/app/workshop/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function Home() {
}

setFirstLoaded(true);
});
}).catch(console.error);

if (!workshopState) {
setFirstLoaded(true);
Expand All @@ -92,10 +92,12 @@ export default function Home() {
return;
}

ApiManager.getWorkshop(config).then(data => {
setData(data);
setTotalCount(data.totalCount);
});
ApiManager.getWorkshop(config)
.then(data => {
setData(data);
setTotalCount(data.totalCount);
})
.catch(console.error);

setLastConfig(config);
}, [page, search, take, filters, sort, firstLoaded])
Expand Down

0 comments on commit 9621d8b

Please sign in to comment.