2022-11-25 20:25:03 +00:00
|
|
|
import { useState } from 'react';
|
2020-11-09 02:09:22 +00:00
|
|
|
import rotateDatabasePassword from '@/api/server/databases/rotateDatabasePassword';
|
2019-07-27 22:17:50 +00:00
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2020-11-09 02:09:22 +00:00
|
|
|
import { ServerDatabase } from '@/api/server/databases/getServerDatabases';
|
2019-07-27 22:17:50 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import Button from '@/components/elements/Button';
|
2020-07-05 00:15:49 +00:00
|
|
|
import tw from 'twin.macro';
|
2019-07-27 22:17:50 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
export default ({ databaseId, onUpdate }: { databaseId: string; onUpdate: (database: ServerDatabase) => void }) => {
|
|
|
|
const [loading, setLoading] = useState(false);
|
2019-07-27 22:17:50 +00:00
|
|
|
const { addFlash, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2022-11-25 20:25:03 +00:00
|
|
|
const server = ServerContext.useStoreState(state => state.server.data!);
|
2019-07-27 22:17:50 +00:00
|
|
|
|
|
|
|
if (!databaseId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rotate = () => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes();
|
|
|
|
|
|
|
|
rotateDatabasePassword(server.uuid, databaseId)
|
2022-11-25 20:25:03 +00:00
|
|
|
.then(database => onUpdate(database))
|
|
|
|
.catch(error => {
|
2019-07-27 22:17:50 +00:00
|
|
|
console.error(error);
|
|
|
|
addFlash({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Error',
|
|
|
|
message: httpErrorToHuman(error),
|
|
|
|
key: 'database-connection-modal',
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2020-07-05 00:15:49 +00:00
|
|
|
<Button isSecondary color={'primary'} css={tw`mr-2`} onClick={rotate} isLoading={loading}>
|
2019-07-27 22:17:50 +00:00
|
|
|
Rotate Password
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
};
|