-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Recreate main settings in AdminCP
- Loading branch information
1 parent
394d820
commit 99052bd
Showing
26 changed files
with
574 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
apps/frontend/src/app/[locale]/admin/(auth)/core/settings/main/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { | ||
generateMetadataMainSettingsCoreAdmin, | ||
MainSettingsCoreAdminView, | ||
} from 'vitnode-frontend/views/admin/views/core/settings/main/main-settings-core-admin-view'; | ||
|
||
export const generateMetadata = generateMetadataMainSettingsCoreAdmin; | ||
|
||
export default function Page() { | ||
return <MainSettingsCoreAdminView />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { AuthAdminModule } from './auth/auth.module'; | ||
import { SettingsAdminModule } from './settings/settings.module'; | ||
import { StaffAdminModule } from './staff/staff.module'; | ||
|
||
@Module({ | ||
imports: [AuthAdminModule, StaffAdminModule], | ||
imports: [AuthAdminModule, StaffAdminModule, SettingsAdminModule], | ||
}) | ||
export class AdminModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/backend/src/core/admin/settings/services/edit.main.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { ABSOLUTE_PATHS } from '@/app.module'; | ||
import { configPath, ConfigType, getConfigFile } from '@/helpers/config'; | ||
import { InternalDatabaseService } from '@/utils/database/internal_database.service'; | ||
import { Injectable, InternalServerErrorException } from '@nestjs/common'; | ||
import { readFile, writeFile } from 'fs/promises'; | ||
import { join } from 'path'; | ||
import { MainSettingsAdminBody } from 'vitnode-shared/admin/settings.dto'; | ||
import { ManifestWithLang } from 'vitnode-shared/manifest.dto'; | ||
|
||
@Injectable() | ||
export class EditMainSettingsAdminService { | ||
constructor(private readonly databaseService: InternalDatabaseService) {} | ||
|
||
protected async updateDescription({ | ||
languages, | ||
site_description, | ||
}: { | ||
languages: { code: string }[]; | ||
site_description: MainSettingsAdminBody['site_description']; | ||
}) { | ||
const update = await Promise.all( | ||
(site_description ?? []).map(async el => { | ||
const item = | ||
el.value !== undefined | ||
? el | ||
: site_description?.find(el => el.language_code === 'en')?.value | ||
? site_description.find(el => el.language_code === 'en') | ||
: site_description?.find(el => el.value); | ||
|
||
// Still not found? | ||
if (!item) { | ||
throw new InternalServerErrorException(); | ||
} | ||
|
||
const path = join( | ||
ABSOLUTE_PATHS.uploads.public, | ||
'assets', | ||
item.language_code, | ||
'manifest.webmanifest', | ||
); | ||
const manifest: ManifestWithLang = JSON.parse( | ||
await readFile(path, 'utf8'), | ||
); | ||
const newData: ManifestWithLang = { | ||
...manifest, | ||
lang: el.language_code, | ||
description: item.value, | ||
}; | ||
|
||
await writeFile(path, JSON.stringify(newData, null, 2), 'utf8'); | ||
|
||
return el.language_code; | ||
}), | ||
); | ||
|
||
// Update rest of the languages | ||
await Promise.all( | ||
languages | ||
.filter(item => !update.includes(item.code)) | ||
.map(async item => { | ||
const value = | ||
site_description?.find(el => el.language_code === 'en')?.value ?? | ||
site_description?.[0]?.value ?? | ||
''; | ||
|
||
const path = join( | ||
ABSOLUTE_PATHS.uploads.public, | ||
'assets', | ||
item.code, | ||
'manifest.webmanifest', | ||
); | ||
const manifest: ManifestWithLang = JSON.parse( | ||
await readFile(path, 'utf8'), | ||
); | ||
const newData: ManifestWithLang = { | ||
...manifest, | ||
description: value, | ||
}; | ||
|
||
await writeFile(path, JSON.stringify(newData, null, 2), 'utf8'); | ||
}), | ||
); | ||
} | ||
|
||
async edit({ | ||
site_description, | ||
site_name, | ||
site_short_name, | ||
contact_email, | ||
}: MainSettingsAdminBody): Promise<MainSettingsAdminBody> { | ||
const config = getConfigFile(); | ||
const newData: ConfigType = { | ||
...config, | ||
settings: { | ||
...config.settings, | ||
main: { | ||
...config.settings.main, | ||
site_name, | ||
site_short_name, | ||
contact_email, | ||
}, | ||
}, | ||
}; | ||
await writeFile(configPath, JSON.stringify(newData, null, 2), 'utf8'); | ||
const languages = | ||
await this.databaseService.db.query.core_languages.findMany({ | ||
columns: { | ||
code: true, | ||
}, | ||
}); | ||
|
||
await this.updateDescription({ | ||
languages, | ||
site_description, | ||
}); | ||
|
||
return { | ||
site_description, | ||
site_name, | ||
site_short_name, | ||
contact_email, | ||
}; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/backend/src/core/admin/settings/settings.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Body, Controller, Put } from '@nestjs/common'; | ||
import { ApiSecurity, ApiTags } from '@nestjs/swagger'; | ||
import { MainSettingsAdminBody } from 'vitnode-shared/admin/settings.dto'; | ||
|
||
import { EditMainSettingsAdminService } from './services/edit.main.service'; | ||
|
||
@ApiTags('Admin') | ||
@Controller('admin/settings') | ||
@ApiSecurity('admin') | ||
export class SettingsAdminController { | ||
constructor(private readonly editMainService: EditMainSettingsAdminService) {} | ||
|
||
@Put('/main') | ||
async editMainSettings( | ||
@Body() body: MainSettingsAdminBody, | ||
): Promise<MainSettingsAdminBody> { | ||
return await this.editMainService.edit(body); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/backend/src/core/admin/settings/settings.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { EditMainSettingsAdminService } from './services/edit.main.service'; | ||
import { SettingsAdminController } from './settings.controller'; | ||
|
||
@Module({ | ||
providers: [EditMainSettingsAdminService], | ||
controllers: [SettingsAdminController], | ||
}) | ||
export class SettingsAdminModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/frontend/src/components/form/fields/text-language-input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { AutoFormComponentProps } from '@/components/form/auto-form'; | ||
import { FormControl, FormMessage } from '@/components/ui/form'; | ||
import { StringLanguageInput } from '@/components/ui/text-language-input'; | ||
|
||
import { AutoFormInputWrapper } from './common/input-wrapper'; | ||
import { AutoFormLabel } from './common/label'; | ||
import { AutoFormTooltip } from './common/tooltip'; | ||
import { AutoFormWrapper } from './common/wrapper'; | ||
|
||
export function AutoFormStringLanguageInput({ | ||
field, | ||
label, | ||
theme, | ||
description, | ||
isRequired, | ||
isDisabled, | ||
hideOptionalLabel, | ||
zodInputProps, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
overrideOptions: _, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
shape: _shape, | ||
wrapper, | ||
classNameWrapper, | ||
...props | ||
}: AutoFormComponentProps & | ||
Omit< | ||
React.ComponentProps<typeof StringLanguageInput>, | ||
'name' | 'onChange' | 'value' | ||
>) { | ||
return ( | ||
<AutoFormWrapper className={classNameWrapper} theme={theme}> | ||
{label && ( | ||
<AutoFormLabel | ||
description={description} | ||
hideOptionalLabel={hideOptionalLabel} | ||
isRequired={isRequired} | ||
label={label} | ||
theme={theme} | ||
/> | ||
)} | ||
|
||
<AutoFormInputWrapper field={field} Wrapper={wrapper}> | ||
<FormControl> | ||
<StringLanguageInput | ||
{...props} | ||
{...zodInputProps} | ||
disabled={isDisabled || props.disabled} | ||
onBlur={field.onBlur || props.onBlur} | ||
onChange={e => { | ||
field.onChange(e); | ||
}} | ||
value={field.value ?? []} | ||
/> | ||
</FormControl> | ||
</AutoFormInputWrapper> | ||
|
||
{description && theme === 'vertical' && ( | ||
<AutoFormTooltip description={description} /> | ||
)} | ||
<FormMessage /> | ||
</AutoFormWrapper> | ||
); | ||
} |
Oops, something went wrong.