Introduce react-query and axios. Refactor AdminThemeApi.

This commit is contained in:
Hikmet 2025-01-28 11:47:44 +03:00
parent 436d734544
commit 2e6b9b6eb2
13 changed files with 369 additions and 118 deletions

252
package-lock.json generated
View File

@ -16,9 +16,12 @@
"@emotion/styled": "^11.10.6",
"@mui/icons-material": "^5.11.11",
"@mui/material": "^5.11.13",
"@tanstack/react-query": "^5.65.0",
"axios": "^1.7.9",
"cookies-next": "^2.1.1",
"get-youtube-id": "^1.0.1",
"jose": "^4.13.1",
"mongoose": "^8.9.5",
"nanoid": "^3.3.6",
"next": "^14.2.3",
"react": "18.2.0",
@ -2584,6 +2587,14 @@
"@jridgewell/sourcemap-codec": "^1.4.14"
}
},
"node_modules/@mongodb-js/saslprep": {
"version": "1.1.9",
"resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.9.tgz",
"integrity": "sha512-tVkljjeEaAhCqTzajSdgbQ6gE6f3oneVwa3iXR6csiEwXXOFsiC6Uh9iAjAhXPtqa/XMDHWjjeNH/77m/Yq2dw==",
"dependencies": {
"sparse-bitfield": "^3.0.3"
}
},
"node_modules/@mui/base": {
"version": "5.0.0-beta.40",
"resolved": "https://registry.npmjs.org/@mui/base/-/base-5.0.0-beta.40.tgz",
@ -3718,6 +3729,30 @@
"tslib": "^2.4.0"
}
},
"node_modules/@tanstack/query-core": {
"version": "5.65.0",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.65.0.tgz",
"integrity": "sha512-Bnnq/1axf00r2grRT6gUyIkZRKzhHs+p4DijrCQ3wMlA3D3TTT71gtaSLtqnzGddj73/7X5JDGyjiSLdjvQN4w==",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/react-query": {
"version": "5.65.0",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.65.0.tgz",
"integrity": "sha512-qXdHj3SCT2xkFxgrBIe6y9Lkowlwm+tGcV++PBLFtyvEJR5Q+biTnzm5p0tdVwqA603xlju9mtV2Kd/2brobgA==",
"dependencies": {
"@tanstack/query-core": "5.65.0"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"peerDependencies": {
"react": "^18 || ^19"
}
},
"node_modules/@testing-library/dom": {
"version": "9.3.4",
"resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-9.3.4.tgz",
@ -4018,6 +4053,19 @@
"integrity": "sha512-/Ad8+nIOV7Rl++6f1BdKxFSMgmoqEoYbHRpPcx3JEfv8VRsQe9Z4mCXeJBzxs7mbHY/XOZZuXlRNfhpVPbs6ZA==",
"dev": true
},
"node_modules/@types/webidl-conversions": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz",
"integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA=="
},
"node_modules/@types/whatwg-url": {
"version": "11.0.5",
"resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.5.tgz",
"integrity": "sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==",
"dependencies": {
"@types/webidl-conversions": "*"
}
},
"node_modules/@types/yargs": {
"version": "17.0.32",
"resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.32.tgz",
@ -4465,8 +4513,7 @@
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==",
"dev": true
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/available-typed-arrays": {
"version": "1.0.7",
@ -4492,6 +4539,16 @@
"node": ">=4"
}
},
"node_modules/axios": {
"version": "1.7.9",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.9.tgz",
"integrity": "sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw==",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/axobject-query": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.2.1.tgz",
@ -4733,6 +4790,14 @@
"node-int64": "^0.4.0"
}
},
"node_modules/bson": {
"version": "6.10.1",
"resolved": "https://registry.npmjs.org/bson/-/bson-6.10.1.tgz",
"integrity": "sha512-P92xmHDQjSKPLHqFxefqMxASNq/aWJMEZugpCjf+AF/pgcUpMMQCg7t7+ewko0/u8AapvF3luf/FoehddEK+sA==",
"engines": {
"node": ">=16.20.1"
}
},
"node_modules/buffer-from": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
@ -4949,7 +5014,6 @@
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dev": true,
"dependencies": {
"delayed-stream": "~1.0.0"
},
@ -5176,7 +5240,6 @@
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
"integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
"dev": true,
"dependencies": {
"ms": "2.1.2"
},
@ -5294,7 +5357,6 @@
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"dev": true,
"engines": {
"node": ">=0.4.0"
}
@ -6310,6 +6372,25 @@
"integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==",
"dev": true
},
"node_modules/follow-redirects": {
"version": "1.15.9",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz",
"integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/for-each": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz",
@ -6323,7 +6404,6 @@
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dev": true,
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
@ -8553,6 +8633,14 @@
"node": ">=4.0"
}
},
"node_modules/kareem": {
"version": "2.6.3",
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.6.3.tgz",
"integrity": "sha512-C3iHfuGUXK2u8/ipq9LfjFfXFxAZMQJJq7vLS45r3D9Y2xQ/m4S8zaR4zMLFWh9AsNPXmcFfUDhTEO8UIC/V6Q==",
"engines": {
"node": ">=12.0.0"
}
},
"node_modules/keyv": {
"version": "4.5.4",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",
@ -8701,6 +8789,11 @@
"tmpl": "1.0.5"
}
},
"node_modules/memory-pager": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg=="
},
"node_modules/merge-stream": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
@ -8733,7 +8826,6 @@
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"dev": true,
"engines": {
"node": ">= 0.6"
}
@ -8742,7 +8834,6 @@
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"dev": true,
"dependencies": {
"mime-db": "1.52.0"
},
@ -8797,6 +8888,128 @@
"obliterator": "^1.6.1"
}
},
"node_modules/mongodb": {
"version": "6.12.0",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.12.0.tgz",
"integrity": "sha512-RM7AHlvYfS7jv7+BXund/kR64DryVI+cHbVAy9P61fnb1RcWZqOW1/Wj2YhqMCx+MuYhqTRGv7AwHBzmsCKBfA==",
"dependencies": {
"@mongodb-js/saslprep": "^1.1.9",
"bson": "^6.10.1",
"mongodb-connection-string-url": "^3.0.0"
},
"engines": {
"node": ">=16.20.1"
},
"peerDependencies": {
"@aws-sdk/credential-providers": "^3.188.0",
"@mongodb-js/zstd": "^1.1.0 || ^2.0.0",
"gcp-metadata": "^5.2.0",
"kerberos": "^2.0.1",
"mongodb-client-encryption": ">=6.0.0 <7",
"snappy": "^7.2.2",
"socks": "^2.7.1"
},
"peerDependenciesMeta": {
"@aws-sdk/credential-providers": {
"optional": true
},
"@mongodb-js/zstd": {
"optional": true
},
"gcp-metadata": {
"optional": true
},
"kerberos": {
"optional": true
},
"mongodb-client-encryption": {
"optional": true
},
"snappy": {
"optional": true
},
"socks": {
"optional": true
}
}
},
"node_modules/mongodb-connection-string-url": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.2.tgz",
"integrity": "sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA==",
"dependencies": {
"@types/whatwg-url": "^11.0.2",
"whatwg-url": "^14.1.0 || ^13.0.0"
}
},
"node_modules/mongodb-connection-string-url/node_modules/tr46": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-5.0.0.tgz",
"integrity": "sha512-tk2G5R2KRwBd+ZN0zaEXpmzdKyOYksXwywulIX95MBODjSzMIuQnQ3m8JxgbhnL1LeVo7lqQKsYa1O3Htl7K5g==",
"dependencies": {
"punycode": "^2.3.1"
},
"engines": {
"node": ">=18"
}
},
"node_modules/mongodb-connection-string-url/node_modules/whatwg-url": {
"version": "14.1.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.1.0.tgz",
"integrity": "sha512-jlf/foYIKywAt3x/XWKZ/3rz8OSJPiWktjmk891alJUEjiVxKX9LEO92qH3hv4aJ0mN3MWPvGMCy8jQi95xK4w==",
"dependencies": {
"tr46": "^5.0.0",
"webidl-conversions": "^7.0.0"
},
"engines": {
"node": ">=18"
}
},
"node_modules/mongoose": {
"version": "8.9.5",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.9.5.tgz",
"integrity": "sha512-SPhOrgBm0nKV3b+IIHGqpUTOmgVL5Z3OO9AwkFEmvOZznXTvplbomstCnPOGAyungtRXE5pJTgKpKcZTdjeESg==",
"dependencies": {
"bson": "^6.10.1",
"kareem": "2.6.3",
"mongodb": "~6.12.0",
"mpath": "0.9.0",
"mquery": "5.0.0",
"ms": "2.1.3",
"sift": "17.1.3"
},
"engines": {
"node": ">=16.20.1"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/mongoose"
}
},
"node_modules/mongoose/node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
},
"node_modules/mpath": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz",
"integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/mquery": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/mquery/-/mquery-5.0.0.tgz",
"integrity": "sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg==",
"dependencies": {
"debug": "4.x"
},
"engines": {
"node": ">=14.0.0"
}
},
"node_modules/mrmime": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/mrmime/-/mrmime-1.0.1.tgz",
@ -8809,8 +9022,7 @@
"node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
"dev": true
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"node_modules/nanoid": {
"version": "3.3.7",
@ -9432,6 +9644,11 @@
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
"integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ=="
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/psl": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz",
@ -9442,7 +9659,6 @@
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
"integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==",
"dev": true,
"engines": {
"node": ">=6"
}
@ -9904,6 +10120,11 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/sift": {
"version": "17.1.3",
"resolved": "https://registry.npmjs.org/sift/-/sift-17.1.3.tgz",
"integrity": "sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ=="
},
"node_modules/signal-exit": {
"version": "3.0.7",
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz",
@ -9987,6 +10208,14 @@
"node": ">=0.10.0"
}
},
"node_modules/sparse-bitfield": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
"integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==",
"dependencies": {
"memory-pager": "^1.0.2"
}
},
"node_modules/sprintf-js": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
@ -10656,7 +10885,6 @@
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
"integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==",
"dev": true,
"engines": {
"node": ">=12"
}

View File

@ -18,9 +18,12 @@
"@emotion/styled": "^11.10.6",
"@mui/icons-material": "^5.11.11",
"@mui/material": "^5.11.13",
"@tanstack/react-query": "^5.65.0",
"axios": "^1.7.9",
"cookies-next": "^2.1.1",
"get-youtube-id": "^1.0.1",
"jose": "^4.13.1",
"mongoose": "^8.9.5",
"nanoid": "^3.3.6",
"next": "^14.2.3",
"react": "18.2.0",

View File

@ -1,70 +0,0 @@
import ITheme from "../lib/theme/theme";
import { ApiResponse } from "./api_response";
import { BackendThemeService } from "../backend/services/theme_service";
export class AdminThemeApi {
fetchTheme = async (pathName: string) => {
const resObj = await fetch(`/api/admin/themes/${pathName}`);
const res = await (resObj.json() as ReturnType<
BackendThemeService["getTheme"]
>);
if (res.status === "success" && res.data) {
return res.data;
}
return undefined;
};
saveTheme = async (
theme: Pick<ITheme, "id" | "explanation" | "image" | "youtubeVideoUrl">
) => {
const resObj = await fetch(
`/api/admin/themes/${theme.id}?type=save-theme`,
{
method: "PUT",
body: JSON.stringify({
theme,
}),
headers: {
"Content-Type": "application/json",
},
}
);
return (await resObj.json()) as ApiResponse;
};
relocateTheme = async (oldThemeId: string, theme: ITheme) => {
const resObj = await fetch(
`/api/admin/themes/${oldThemeId}?type=relocate-theme`,
{
method: "PUT",
body: JSON.stringify({
theme,
}),
headers: {
"Content-Type": "application/json",
},
}
);
return (await resObj.json()) as ApiResponse;
};
createTheme = async (theme: ITheme) => {
const resObj = await fetch(`/api/admin/themes`, {
method: "POST",
body: JSON.stringify({
theme,
}),
headers: {
"Content-Type": "application/json",
},
});
return (await resObj.json()) as ApiResponse;
};
deleteTheme = async (themeId: string) => {
const resObj = await fetch(`/api/admin/themes/${themeId}`, {
method: "DELETE",
});
return (await resObj.json()) as ApiResponse;
};
}

View File

@ -0,0 +1,13 @@
import { apiAdmin } from "@/lib/axios";
import ITheme from "@/lib/theme/theme";
import { useMutation } from "@tanstack/react-query";
import { ApiResponse } from "../api_response";
export const useAdminCreateTheme = () => {
return useMutation({
mutationFn: async ({ theme }: { theme: ITheme }) => {
const { data } = await apiAdmin.post("/themes", { theme });
return data as ApiResponse;
},
});
};

View File

@ -0,0 +1,12 @@
import { apiAdmin } from "@/lib/axios";
import { useMutation } from "@tanstack/react-query";
import { ApiResponse } from "../api_response";
export const useAdminDeleteTheme = () => {
return useMutation({
mutationFn: async ({ themeId }: { themeId: string }) => {
const { data } = await apiAdmin.delete(`/themes/${themeId}`);
return data as ApiResponse;
},
});
};

View File

@ -0,0 +1,14 @@
import { apiAdmin } from "@/lib/axios";
import ITheme from "@/lib/theme/theme";
import { useMutation } from "@tanstack/react-query";
import { ApiResponse } from "../api_response";
export const useAdminRelocateTheme = () => {
return useMutation({
mutationFn: async ({ oldThemeId, theme }: { oldThemeId: string; theme: ITheme }) => {
const { data } = await apiAdmin.put(`/themes/${oldThemeId}?type=relocate-theme`, { theme });
return data as ApiResponse;
},
});
};

View File

@ -0,0 +1,14 @@
import { apiAdmin } from "@/lib/axios";
import { useQuery } from "@tanstack/react-query";
import { ApiResponse } from "../api_response";
export const useAdminTheme = ({ themeSlug }: { themeSlug: string }) => {
return useQuery({
queryKey: ["themes", themeSlug],
queryFn: async () => {
const { data } = await apiAdmin.get(`/themes/${themeSlug}`);
return data as ApiResponse;
},
enabled: !!themeSlug,
});
};

View File

@ -0,0 +1,18 @@
import { apiAdmin } from "@/lib/axios";
import ITheme from "@/lib/theme/theme";
import { useMutation } from "@tanstack/react-query";
import { ApiResponse } from "../api_response";
export const useAdminUpdateTheme = () => {
return useMutation({
mutationFn: async ({
theme,
}: {
theme: Pick<ITheme, "id" | "explanation" | "image" | "youtubeVideoUrl">;
}) => {
const { data } = await apiAdmin.put(`/themes/${theme.id}?type=save-theme`, theme);
return data as ApiResponse;
},
});
};

View File

@ -6,10 +6,12 @@ import { defaultLesson } from "@/lib/lesson/default_lesson";
import { ApiResponse } from "@/api/api_response";
import { defaultActivity } from "@/lib/activity/default_activity";
import { slugifyLaz } from "@/utils/slugify_laz";
import { AdminThemeApi } from "@/api/admin_theme_api";
import ILesson from "@/lib/lesson/lesson";
import { AdminLessonApi } from "@/api/admin_lesson_api";
import { AdminActivityApi } from "@/api/admin_activity_api";
import { useAdminDeleteTheme } from "@/api/theme/useAdminDeleteTheme";
import { useAdminUpdateTheme } from "@/api/theme/useAdminUpdateTheme";
import { useAdminRelocateTheme } from "@/api/theme/useAdminRelocateTheme";
export function useAdminViewModel(): IAdminViewModel {
const {
@ -27,7 +29,9 @@ export function useAdminViewModel(): IAdminViewModel {
changeActiveLesson,
} = useBaseViewModelContext()!;
const { replace } = useRouter();
const adminThemeApi = useRef(new AdminThemeApi());
const { mutateAsync: adminRelocateTheme } = useAdminRelocateTheme();
const { mutateAsync: adminUpdateTheme } = useAdminUpdateTheme();
const { mutateAsync: adminDeleteTheme } = useAdminDeleteTheme();
const adminLessonApi = useRef(new AdminLessonApi());
const adminActivityApi = useRef(new AdminActivityApi());
const [stalling, setStalling] = useState(false);
@ -72,14 +76,17 @@ export function useAdminViewModel(): IAdminViewModel {
const newId = slugifyLaz(newTitle);
withFeedback(
() =>
adminThemeApi.current.relocateTheme(id, {
id: newId,
title: newTitle,
explanation: newExplanation,
image: newImage,
youtubeVideoUrl: newYoutubeVideoUrl,
createdAt,
lessons,
adminRelocateTheme({
oldThemeId: id,
theme: {
id: newId,
title: newTitle,
explanation: newExplanation,
image: newImage,
youtubeVideoUrl: newYoutubeVideoUrl,
createdAt,
lessons,
},
}),
(res) => {
setId(newId);
@ -93,11 +100,13 @@ export function useAdminViewModel(): IAdminViewModel {
} else {
withFeedback(
() =>
adminThemeApi.current.saveTheme({
id,
explanation: newExplanation,
image: newImage,
youtubeVideoUrl: newYoutubeVideoUrl,
adminUpdateTheme({
theme: {
id,
explanation: newExplanation,
image: newImage,
youtubeVideoUrl: newYoutubeVideoUrl,
},
}),
(res) => {
setExplanation(newExplanation);
@ -110,7 +119,7 @@ export function useAdminViewModel(): IAdminViewModel {
const deleteTheme = async () => {
withFeedback(
() => adminThemeApi.current.deleteTheme(id),
() => adminDeleteTheme({ themeId: id }),
(res) => replace("/admin")
);
};

9
src/lib/axios.ts Normal file
View File

@ -0,0 +1,9 @@
import axios from "axios";
export const api = axios.create({
baseURL: "/api",
});
export const apiAdmin = axios.create({
baseURL: "/api/admin",
});

View File

@ -7,6 +7,7 @@ import "@/styles/feedback.scss";
import { CssBaseline, ThemeProvider, createTheme } from "@mui/material";
import type { AppProps } from "next/app";
import { Poppins } from "next/font/google";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
export const poppins = Poppins({
subsets: ["latin-ext"],
@ -24,12 +25,16 @@ const theme = createTheme({
});
export default function App({ Component, pageProps }: AppProps) {
const queryClient = new QueryClient();
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<main className={poppins.className}>
<Component {...pageProps} />
</main>
</ThemeProvider>
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={theme}>
<CssBaseline />
<main className={poppins.className}>
<Component {...pageProps} />
</main>
</ThemeProvider>
</QueryClientProvider>
);
}

View File

@ -1,13 +1,15 @@
import { LandingPageView } from "@/features/landing_page_view";
import { AdminThemeApi } from "@/api/admin_theme_api";
import { BackendThemeService } from "@/backend/services/theme_service";
import { defaultTheme } from "@/lib/theme/default_theme";
import { ThemeMetaDTO } from "@/lib/theme/theme_meta_dto";
import Head from "next/head";
import { useEffect, useRef, useState } from "react";
import { useAdminCreateTheme } from "@/api/theme/useAdminCreateTheme";
import { AdminThemeApi } from "@/api/admin_theme_api";
export default function AdminPage() {
const adminThemeApi = useRef(new AdminThemeApi());
const { mutateAsync: adminCreateTheme } = useAdminCreateTheme();
const [themeMetas, setThemeMetas] = useState<ThemeMetaDTO[]>();
const [stalling, setStalling] = useState(false);
@ -27,7 +29,7 @@ export default function AdminPage() {
const createTheme = async () => {
setStalling(true);
const res = await adminThemeApi.current.createTheme(defaultTheme());
const res = await adminCreateTheme({ theme: defaultTheme() });
if (res.status === "success") await fetchThemeMetas();
setStalling(false);
};

View File

@ -2,8 +2,8 @@ import TP from "@/features/theme_page";
import { useEffect, useRef, useState } from "react";
import { usePathname } from "next/navigation";
import dynamic from "next/dynamic";
import { AdminThemeApi } from "@/api/admin_theme_api";
import ITheme from "@/lib/theme/theme";
import { useAdminTheme } from "@/api/theme/useAdminTheme";
const AT = dynamic(() => import("@/features/admin_tools"), {
ssr: false,
@ -11,19 +11,13 @@ const AT = dynamic(() => import("@/features/admin_tools"), {
export default function ThemePage() {
const pathname = usePathname();
const adminService = useRef(new AdminThemeApi());
const { data: adminTheme } = useAdminTheme({ themeSlug: pathname ? pathname.split("/").pop() as string : "" });
const [themeData, setThemeData] = useState<ITheme>();
const fetchTheme = async (pathName: string) => {
setThemeData(await adminService.current.fetchTheme(pathName));
};
useEffect(() => {
if (!pathname) return;
const splitPathname = pathname.split("/");
const themePathName = splitPathname[splitPathname.length - 1];
fetchTheme(themePathName);
}, [pathname]);
if (!pathname || !adminTheme) return;
setThemeData(adminTheme?.data as ITheme);
}, [pathname, adminTheme]);
if (themeData) {
return <TP home="/admin" theme={themeData} adminTools={<AT />} />;