Some state management fixes
This commit is contained in:
32
state.ts
32
state.ts
@@ -7,9 +7,9 @@ import toast from 'react-hot-toast';
|
|||||||
const octokit = new Octokit();
|
const octokit = new Octokit();
|
||||||
|
|
||||||
interface File {
|
interface File {
|
||||||
name: string,
|
name: string;
|
||||||
language: string,
|
language: string;
|
||||||
content: string
|
content: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IState {
|
interface IState {
|
||||||
@@ -98,11 +98,20 @@ export const updateEditorSettings = (editorSettings: IState['editorSettings']) =
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const saveFile = (value: string) => {
|
export const saveFile = (value: string) => {
|
||||||
|
const editorModels = state.editorCtx?.getModels();
|
||||||
|
const currentModel = editorModels?.find(editorModel => editorModel.uri.path === `/${state.files[state.active].name}`);
|
||||||
|
console.log(currentModel?.getValue())
|
||||||
|
if (state.files.length > 0) {
|
||||||
|
console.log('häää')
|
||||||
|
state.files[state.active].content = currentModel?.getValue() || '';
|
||||||
|
}
|
||||||
|
console.log(state.files[state.active])
|
||||||
toast.success('Saved successfully', { position: 'bottom-center' })
|
toast.success('Saved successfully', { position: 'bottom-center' })
|
||||||
}
|
}
|
||||||
|
|
||||||
export const createNewFile = (name: string) => {
|
export const createNewFile = (name: string) => {
|
||||||
state.files.push({ name, language: 'c', content: "" })
|
const emptyFile: File = { name, language: 'c', content: "" };
|
||||||
|
state.files.push(emptyFile)
|
||||||
state.active = state.files.length - 1;
|
state.active = state.files.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,8 +144,19 @@ export const compileCode = async (activeId: number) => {
|
|||||||
});
|
});
|
||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
state.compiling = false;
|
state.compiling = false;
|
||||||
toast.success('Compiled successfully!');
|
if (!json.success) {
|
||||||
console.log(json)
|
state.logs.push({ type: 'error', message: json.message })
|
||||||
|
if (json.tasks && json.tasks.length > 0) {
|
||||||
|
json.tasks.forEach((task: any) => {
|
||||||
|
if (!task.success) {
|
||||||
|
state.logs.push({ type: 'error', message: task?.console })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return toast.error(`Couldn't compile!`, { position: 'bottom-center' });
|
||||||
|
}
|
||||||
|
state.logs.push({ type: 'log', message: 'Compiled successfully ✅' })
|
||||||
|
toast.success('Compiled successfully!', { position: 'bottom-center' });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
state.logs.push({ type: 'error', message: 'Error occured while compiling!' })
|
state.logs.push({ type: 'error', message: 'Error occured while compiling!' })
|
||||||
|
|||||||
Reference in New Issue
Block a user