Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis-Dinkov committed Nov 5, 2024
1 parent 6f055a8 commit 2e52271
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/services/examplesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from 'axios';
import { SERVER_URL } from '@constants/auth';

interface GistFile {
filename?: string;
content: string;
}

Expand All @@ -14,7 +15,8 @@ interface UploadCustomExampleData {

const createExample = async (data: UploadCustomExampleData) => {
const files: { [key: string]: GistFile } = {
[data.exampleName]: {
'example': {
filename: data.exampleName,
content: data.code,
},
};
Expand Down Expand Up @@ -56,7 +58,6 @@ const updateExampleInfo = async (id: string, name: string, description: string)
const { data } = await axios.patch(`${SERVER_URL}/gists/edit-info/${id}`, body);

return data;

};

const updateExampleContent = async (id: string, name: string, data: string) => {
Expand Down
62 changes: 32 additions & 30 deletions src/stores/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,28 @@ const baseStore = create<StoreInterface>()((set, get) => ({
...initialState,
actions: {
createExample: async (data) => {
set({ loading: { ...get().loading, isCreatingExample: true } });
set((state) => ({ loading: { ...state.loading, isCreatingExample: true } }));

if (!data.code || !data.description || !data.exampleName) {
console.log(null, 'Invalid input data: Missing code, description, or exampleName');
set({ loading: { ...get().loading, isCreatingExample: false } });
console.error('Invalid input data: Missing code, description, or exampleName');
set((state) => ({ loading: { ...state.loading, isCreatingExample: false } }));
return;
}

if (get().examples.some((example) => example.name === data.exampleName)) {
toast.error('Example with this name already exists');
set((state) => ({ loading: { ...state.loading, isCreatingExample: false } }));
return;
}

try {
const { name, id } = await gistService.createExample(data);
set({
const { name, id, description } = await gistService.createExample(data);
set((state) => ({
examples: [
...get().examples,
{ name, id },
...state.examples,
{ name, id, description },
],
});
}));

if (id) {
busDispatch<IUploadExampleModalClose>({
Expand All @@ -89,21 +95,21 @@ const baseStore = create<StoreInterface>()((set, get) => ({

toast.success('Snippet uploaded');
} catch (error) {
console.log('Error uploading snippet', error);
console.error('Error uploading snippet', error);
} finally {
set({ loading: { ...get().loading, isCreatingExample: false } });
set((state) => ({ loading: { ...state.loading, isCreatingExample: false } }));
}
},

getExamples: async () => {
set((state) => ({ loading: { ...state.loading, isGettingExamples: true } }));
try {
set({ loading: { ...get().loading, isGettingExamples: true } });
const data = await gistService.getUserExamples();
set({ examples: data });
} catch (error) {
console.log('Error getting examples', error);
console.error('Error getting examples', error);
} finally {
set({ loading: { ...get().loading, isGettingExamples: false } });
set((state) => ({ loading: { ...state.loading, isGettingExamples: false } }));
}
},

Expand All @@ -130,7 +136,7 @@ const baseStore = create<StoreInterface>()((set, get) => ({
});

} catch (error) {
console.log('Error loading example content', error);
console.error('Error loading example content', error);
throw error;
} finally {
busDispatch({ type: '@@-monaco-editor-hide-loading' });
Expand All @@ -143,34 +149,32 @@ const baseStore = create<StoreInterface>()((set, get) => ({
return;
}

set({ loading: { ...get().loading, isSavingInfo: true } });
set((state) => ({ loading: { ...state.loading, isSavingInfo: true } }));

try {
const editedGist = await gistService.updateExampleInfo(id, exampleName, description);

console.log('editedGist', editedGist);
const updatedExamples = get().examples.map((example) =>
example.id === id ? { ...example, ...editedGist } : example,
);

set({ examples: updatedExamples });

if (get().selectedExample.id === id) {
set({ selectedExample: editedGist });
}

set({ examples: updatedExamples });
busDispatch<IEditExampleInfoModalClose>('@@-close-edit-example-modal');
toast.success('Example Information Updated!');
} catch (error) {
console.log('Error updating snippet', error);
console.error('Error updating snippet', error);
toast.error('Failed to update example information');
} finally {
set({ loading: { ...get().loading, isSavingInfo: false } });
set((state) => ({ loading: { ...state.loading, isSavingInfo: false } }));
}
},

updateExampleContent: async (data: string) => {
set({ loading: { ...get().loading, isSavingContent: true } });
set((state) => ({ loading: { ...state.loading, isSavingContent: true } }));
const { name, id } = get().selectedExample;

if (!id || !data || !name) {
Expand All @@ -186,9 +190,9 @@ const baseStore = create<StoreInterface>()((set, get) => ({
});
toast.success('Example Content Updated!');
} catch (error) {
console.log('Error updating snippet', error);
console.error('Error updating snippet', error);
} finally {
set({ loading: { ...get().loading, isSavingContent: false } });
set((state) => ({ loading: { ...state.loading, isSavingContent: false } }));
}
},

Expand All @@ -198,9 +202,9 @@ const baseStore = create<StoreInterface>()((set, get) => ({
return;
}

try {
set({ loading: { ...get().loading, isDeleting: true } });
set((state) => ({ loading: { ...state.loading, isDeleting: true } }));

try {
const updatedExampleList = get().examples.filter((example) => example.id !== id);
set({ examples: updatedExampleList });

Expand All @@ -211,15 +215,13 @@ const baseStore = create<StoreInterface>()((set, get) => ({
await gistService.deleteExample(id);
toast.success('Snippet deleted');
} catch (error) {
console.log('Error deleting snippet', error);
console.error('Error deleting snippet', error);
} finally {
set({ loading: { ...get().loading, isDeleting: false } });
set((state) => ({ loading: { ...state.loading, isDeleting: false } }));
}
},

resetStore: () => (
set({ ...initialState })
),
resetStore: () => set({ ...initialState }),
},
init: async () => {
get().actions.getExamples();
Expand Down
4 changes: 1 addition & 3 deletions src/views/codeEditor/components/selectExample/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ export const SelectExample = () => {
)}

>
<div
data-title="Default"
>
<div data-title="Default">
<DefaultExamplesList handleClose={handleClose} />
</div>
<div data-title="Custom">
Expand Down

0 comments on commit 2e52271

Please sign in to comment.