-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: Configuration variable editor (WIP) #449
Draft
jamesread
wants to merge
3
commits into
main
Choose a base branch
from
feature-configuration-variables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
79 changes: 79 additions & 0 deletions
79
apps/frontend/src/components/settings/configuration-variable-editor.component.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,79 @@ | ||
'use client'; | ||
|
||
// import { FormProvider, SubmitHandler, useForm } from 'react-hook-form'; | ||
// import { useMemo } from 'react'; | ||
// import { classValidatorResolver } from '@hookform/resolvers/class-validator'; | ||
// import { SaveConfigurationVariableDto, SaveConfigurationVariablesDto } from '@gitroom/nestjs-libraries/dtos/settings/configuration-variables.dto.ts'; | ||
import { Button } from '@gitroom/react/form/button' | ||
import { useCallback, useState } from 'react' | ||
import useSWR from 'swr' | ||
import { useFetch } from '@gitroom/helpers/utils/custom.fetch' | ||
|
||
export const ConfigurationVariableEditorComponent = () => { | ||
// const resolver = useMemo(() => classValidatorResolver(SaveConfigurationVariableDto), []); | ||
|
||
const fetch = useFetch(); | ||
|
||
// const form = useForm({ resolver, values: { message: '' } }); | ||
|
||
const [state, setState] = useState(true); | ||
|
||
const fetchCvars = useCallback(async () => { | ||
const cvars = await ( | ||
await fetch('/settings/cvars/all', { | ||
method: 'GET', | ||
}) | ||
).json(); | ||
|
||
setState(cvars); | ||
return cvars; | ||
}, []) | ||
Check warning Code scanning / ESLint verifies the list of dependencies for Hooks like useEffect and similar Warning
React Hook useCallback has a missing dependency: 'fetch'. Either include it or remove the dependency array.
|
||
|
||
const { data, error, isLoading } = useSWR('/settings/cvars/all', fetchCvars) | ||
|
||
/* | ||
const submit: SubmitHandler<SaveConfigurationVariableDto> = async (data) => { | ||
|
||
await fetch(`/settings/cvars/${params.id}`, { | ||
method: 'POST', | ||
body: JSON.stringify(data), | ||
}); | ||
mutate(); | ||
form.reset(); | ||
} | ||
*/ | ||
|
||
if (isLoading) return <div>Loading...</div> | ||
if (error) return <div className = "text-red-700">Error loading data</div> | ||
|
||
return ( | ||
<div> | ||
<h3 className = "text-[20px] mb-[6px]">Configuration Variable Editor</h3> | ||
<p className = "mb-[12px]">This screen is only accessible and editable by super admins, it includes configuration that effects the entire app. </p> | ||
|
||
{data.configurationVariables.map((cvar) => ( | ||
<div className = "grid grid-cols-2 p-1" key={cvar.description}> | ||
<div> | ||
<label className = ""> | ||
<strong className = "font-bold"><abbr title = {cvar.key}>{cvar.title}</abbr></strong> | ||
</label> | ||
<p className = "text-customColor18">{cvar.description}</p> | ||
|
||
{cvar.docs && ( | ||
<a href = {cvar.docs} target = "_blank" className = "text-customColor4">More docs...</a> | ||
)} | ||
</div> | ||
<div className=""> | ||
{cvar.datatype === 'bool' ? ( | ||
<input type = "checkbox" name = "" value = "1"></input> | ||
) : ( | ||
<input className="bg-input border border-fifth rounded-[4px] text-inputText flex-grow p-1" name="{...form.register(cvar.key)}" value="?" autocomplete="off"></input> | ||
)} | ||
</div> | ||
</div> | ||
))} | ||
|
||
<Button className = "rounded-[4px]">Save</Button> | ||
|
||
</div> | ||
) | ||
} |
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
44 changes: 44 additions & 0 deletions
44
...s/nestjs-libraries/src/database/prisma/configuration/configuration.variable.repository.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,44 @@ | ||
import { PrismaRepository } from '@gitroom/nestjs-libraries/database/prisma/prisma.service'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class ConfigurationVariableRepository { | ||
constructor( | ||
private _configurationVariables: PrismaRepository<'configurationVariables'> | ||
) {} | ||
|
||
getOrDefault(key: string, defaultValue: string) { | ||
const dbVal = this._configurationVariables.model.configurationVariables.findFirst({ | ||
where: { | ||
key, | ||
}, | ||
}); | ||
|
||
if (dbVal) { | ||
return dbVal; | ||
} else { | ||
return defaultValue; | ||
} | ||
} | ||
|
||
isSet(key: string) { | ||
return !!this._configurationVariables.model.configurationVariables.findFirst({ | ||
where: { | ||
key, | ||
}, | ||
}); | ||
} | ||
|
||
set(key: string, value: string) { | ||
return this._configurationVariables.model.configurationVariables.create({ | ||
data: { | ||
key, | ||
value, | ||
}, | ||
}); | ||
} | ||
|
||
getAll() { | ||
return this._configurationVariables.model.configurationVariables.findMany(); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...ries/nestjs-libraries/src/database/prisma/configuration/configuration.variable.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,82 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { ConfigurationVariableRepository } from '@gitroom/nestjs-libraries/database/prisma/configuration/configuration.variable.repository'; | ||
|
||
@Injectable() | ||
export class ConfigurationVariableService { | ||
private cvars: { | ||
key: string, | ||
title: string, | ||
description: string, | ||
datatype: string, | ||
default: string | null, | ||
val: string | null, | ||
section: string[], | ||
docs?: string, | ||
}[]; | ||
|
||
constructor( | ||
private _configurationVariableRepository: ConfigurationVariableRepository, | ||
) { | ||
this.cvars = [ | ||
{ | ||
key: 'USER_REGISTRATION_DISABLED', | ||
title: 'Disable user registration', | ||
description: 'If user registration is disabled, only super admins can create new users', | ||
datatype: 'bool', | ||
default: 'true', | ||
val: 'true', | ||
section: ['Functionality'], | ||
}, | ||
{ | ||
key: 'MARKETPLACE_DISABLED', | ||
title: 'Disable marketplace', | ||
description: 'If the marketplace is disabled, users will not be able to buy or sell posts', | ||
datatype: 'bool', | ||
default: 'true', | ||
val: 'true', | ||
section: ['Functionality'], | ||
}, | ||
{ | ||
key: 'DISCORD_CLIENT_ID', | ||
title: 'Discord client ID', | ||
description: 'Used to authenticate with Discord with OAuth.', | ||
docs: 'https://docs.postiz.com/providers/discord', | ||
datatype: 'string', | ||
default: null, | ||
val: null, | ||
section: ['Providers', 'Discord'], | ||
}, | ||
{ | ||
key: 'DISCORD_CLIENT_SECRET', | ||
title: 'Discord client secret', | ||
description: 'Used to authenticate with Discord with OAuth.', | ||
datatype: 'string', | ||
default: null, | ||
val: null, | ||
section: ['Providers', 'Discord'], | ||
}, | ||
] | ||
} | ||
|
||
getOrDefault(key: string, defaultValue: string) { | ||
return this._configurationVariableRepository.getOrDefault(key, defaultValue); | ||
} | ||
|
||
getOrEmpty(key: string) { | ||
return this._configurationVariableRepository.getOrDefault(key, ''); | ||
} | ||
|
||
isSet(key: string) { | ||
return this._configurationVariableRepository.isSet(key); | ||
} | ||
|
||
set(key: string, value: string) { | ||
return this._configurationVariableRepository.set(key, value); | ||
} | ||
|
||
getAll() { | ||
return this.cvars; | ||
// return this._configurationVariableRepository.getAll(); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
libraries/nestjs-libraries/src/dtos/settings/configuration-variables.dto.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,16 @@ | ||
import {IsDefined, IsString, ValidateNested} from 'class-validator'; | ||
import {Type} from 'class-transformer'; | ||
|
||
export class SaveConfigurationVariableDto { | ||
@IsString() | ||
key: string; | ||
|
||
@IsString() | ||
val: string; | ||
} | ||
|
||
export class SaveConfigurationVariablesDto { | ||
@ValidateNested({each: true}) | ||
@Type(() => ConfigurationVariableDto) | ||
configurationVariables: Record<string, ConfigurationVariableDto>; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / ESLint
Disallow unused variables Warning
Copilot Autofix AI 5 days ago
To fix the problem, we need to remove the unused
state
variable and its associateduseState
hook. This will eliminate the ESLint error and make the code cleaner and more efficient.const [state, setState] = useState(true);
line from the code.state
orsetState
are also removed, although in this case, there are none.