-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
follow this style and please help correct it back
- Loading branch information
Showing
9 changed files
with
218 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,29 +1,103 @@ | ||
import { useSchema } from "@/context/schema-provider"; | ||
import TriggerEditor, { TriggerEditorProps } from "../trigger-editor"; | ||
import TriggerEditor from "../trigger-editor"; | ||
import { useTabsContext } from "../windows-tab"; | ||
import { LucideTableProperties } from "lucide-react"; | ||
import { useEffect, useState } from "react"; | ||
import { DatabaseTriggerSchema } from "@/drivers/base-driver"; | ||
import { useDatabaseDriver } from "@/context/driver-provider"; | ||
import OpacityLoading from "../loading-opacity"; | ||
import { produce } from "immer"; | ||
import { TriggerController } from "../trigger-editor/trigger-controller"; | ||
|
||
export default function TriggerTab(props: TriggerEditorProps) { | ||
import { isEqual } from "lodash"; | ||
|
||
export interface TriggerTabProps { | ||
name: string; | ||
tableName?: string; | ||
schemaName: string; | ||
} | ||
|
||
const EMPTY_DEFAULT_TRIGGER: DatabaseTriggerSchema = { | ||
name: "", | ||
operation: "INSERT", | ||
when: "BEFORE", | ||
tableName: "", | ||
whenExpression: "", | ||
statement: "", | ||
schemaName: "", | ||
}; | ||
|
||
export default function TriggerTab({ | ||
name, | ||
schemaName, | ||
tableName, | ||
}: TriggerTabProps) { | ||
const { databaseDriver } = useDatabaseDriver(); | ||
const { refresh: refreshSchema } = useSchema(); | ||
const { replaceCurrentTab } = useTabsContext(); | ||
|
||
const onSave = (trigger: TriggerEditorProps) => { | ||
// If name is specified, it means the trigger is already exist | ||
const [loading, setLoading] = useState(!!name); | ||
|
||
// Loading the inital value | ||
const [initialValue, setInitialValue] = useState<DatabaseTriggerSchema>( | ||
() => { | ||
return produce(EMPTY_DEFAULT_TRIGGER, (draft) => { | ||
draft.tableName = tableName ?? ""; | ||
draft.schemaName = schemaName ?? ""; | ||
}); | ||
} | ||
); | ||
const [value, setValue] = useState<DatabaseTriggerSchema>(initialValue); | ||
|
||
const hasChanged = !isEqual(initialValue, value); | ||
|
||
// Loading the trigger | ||
useEffect(() => { | ||
if (name && schemaName) { | ||
databaseDriver | ||
.trigger(schemaName, name) | ||
.then((triggerValue) => { | ||
setValue(triggerValue); | ||
setInitialValue(triggerValue); | ||
}) | ||
.finally(() => setLoading(false)); | ||
} | ||
}, [name, schemaName, databaseDriver]); | ||
|
||
const onSave = () => { | ||
refreshSchema(); | ||
console.log(trigger) | ||
replaceCurrentTab({ | ||
component: ( | ||
<TriggerTab | ||
tableName={trigger.tableName} | ||
schemaName={trigger.schemaName} | ||
name={trigger.name ?? ''} | ||
tableName={value.tableName} | ||
schemaName={value.schemaName} | ||
name={value.name ?? ""} | ||
/> | ||
), | ||
key: 'trigger-' + trigger.name || '', | ||
identifier: 'trigger-' + trigger.name || '', | ||
title: trigger.name || '', | ||
key: "trigger-" + value.name || "", | ||
identifier: "trigger-" + value.name || "", | ||
title: value.name || "", | ||
icon: LucideTableProperties, | ||
}); | ||
}; | ||
|
||
if (loading) { | ||
return <OpacityLoading />; | ||
} | ||
|
||
return <TriggerEditor {...props} onSave={onSave} /> | ||
return ( | ||
<div className="flex flex-col overflow-hidden w-full h-full"> | ||
<TriggerController | ||
onSave={() => { | ||
// @adam do something here | ||
}} | ||
onDiscard={() => { | ||
setValue(initialValue); | ||
}} | ||
/> | ||
|
||
<TriggerEditor value={value} onChange={setValue} /> | ||
</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
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
Oops, something went wrong.