Skip to content
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

#334 ✨ 🐛 update active bot image #410

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
🔧🐛Fix:fixes active bot image
  • Loading branch information
adamswonder committed Apr 20, 2023
commit 5de3ee0d7c90e66ad1c006bdbc8b75c782cff8c3
Original file line number Diff line number Diff line change
@@ -1,61 +1,73 @@
import { Injectable, OnDestroy } from "@angular/core";
import { Router } from "@angular/router";
import { MatDialog } from "@angular/material/dialog";
import { Injectable, OnDestroy } from '@angular/core';
import { Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';

import { SubSink } from 'subsink';

import { take, map, tap } from "rxjs/operators";
import { take, map, tap } from 'rxjs/operators';

import { uniqueNamesGenerator, adjectives, colors, animals } from "unique-names-generator";
import {
uniqueNamesGenerator,
adjectives,
colors,
animals,
} from 'unique-names-generator';

import { ToastService } from '@iote/bricks-angular';
import { TranslateService } from '@ngfi/multi-lang';

import { Story } from "@app/model/convs-mgr/stories/main";
import { EndStoryAnchorBlock } from "@app/model/convs-mgr/stories/blocks/messaging";
import { StoryBlockTypes } from "@app/model/convs-mgr/stories/blocks/main";

import { ActiveOrgStore } from "@app/state/organisation";
import { StoriesStore } from "@app/state/convs-mgr/stories";
import { FileStorageService } from "@app/state/file";
import { StoryBlocksStore } from "@app/state/convs-mgr/stories/blocks";
import { Story } from '@app/model/convs-mgr/stories/main';
import { EndStoryAnchorBlock } from '@app/model/convs-mgr/stories/blocks/messaging';
import { StoryBlockTypes } from '@app/model/convs-mgr/stories/blocks/main';

import { ActiveOrgStore } from '@app/state/organisation';
import { StoriesStore } from '@app/state/convs-mgr/stories';
import { FileStorageService } from '@app/state/file';
import { StoryBlocksStore } from '@app/state/convs-mgr/stories/blocks';

/** Service which can create new stories. */
@Injectable()
export class NewStoryService implements OnDestroy {
private _sbS = new SubSink();

constructor(private _org$$: ActiveOrgStore,
private _stories$$: StoriesStore,
private _blocksStore$$: StoryBlocksStore,
private _fileStorageService$$: FileStorageService,
private _router: Router,
private _toast: ToastService,
private _translate: TranslateService,
private _dialog: MatDialog
) { }
constructor(
private _org$$: ActiveOrgStore,
private _stories$$: StoriesStore,
private _blocksStore$$: StoryBlocksStore,
private _fileStorageService$$: FileStorageService,
private _router: Router,
private _toast: ToastService,
private _translate: TranslateService,
private _dialog: MatDialog
) {}

async saveStoryWithImage(bot: Story, imageFile: File, imagePath: string) {
await this.saveBotImage(bot, imageFile, imagePath)
await this.saveBotImage(bot, imageFile, imagePath);
}

async saveImage(bot:Story, imageFile: File, imagePath: string) {
const res = await this._fileStorageService$$.uploadSingleFile(imageFile, imagePath)
this._sbS.sink = res.pipe(tap((url) => {
bot.imageField = url
})).subscribe()

return bot
async saveImage(bot: Story, imageFile: File, imagePath: string) {
const res = await this._fileStorageService$$.uploadSingleFile(
imageFile,
imagePath
);
this._sbS.sink = res
.pipe(
tap((url) => {
bot.imageField = url;
})
)
.subscribe();

return bot;
}

saveImagelessStory(bot: Story) {
this.addStoryToDb(bot)
this.addStoryToDb(bot);
}

async saveBotImage(bot: Story, storyImage?: File, storyImagePath?: string) {
if (storyImagePath) {
if(storyImage){
if (storyImage) {
this.saveImage(bot, storyImage, storyImagePath);
}

Expand All @@ -64,45 +76,59 @@ export class NewStoryService implements OnDestroy {
}

addStoryToDb(bot: Story) {
this._org$$.get().pipe(take(1)).subscribe(async (org) => {
if (org) {
bot.orgId = org.id as string;
this._stories$$.add(bot).subscribe((story) => {
if (story) {
this._dialog.closeAll();
this._router.navigate(['/stories', story.id]),
this.createStoryEndBlock(story.orgId, story.id!);
}
});
}
})
this._org$$
.get()
.pipe(take(1))
.subscribe(async (org) => {
if (org) {
bot.orgId = org.id as string;
this._stories$$.add(bot).subscribe((story) => {
if (story) {
this._dialog.closeAll();
this._router.navigate(['/stories', story.id]),
this.createStoryEndBlock(story.orgId, story.id!);
}
});
}
});
}

deleteImage(imagePath: string) {
this._fileStorageService$$.deleteSingleFile(imagePath);
}

async update(bot: Story, storyImage?: File, storyImagePath?: string) {

if (storyImage) {
//delete the image if any
if (bot.imageField && bot.imageField != '') {
this.deleteImage(bot.imageField);
bot.imageField = '';
}

const res = await this._fileStorageService$$.uploadSingleFile(storyImage, storyImagePath as string)
this._sbS.sink = res.pipe(tap((url) => {
bot.imageField = url
})).subscribe()
const res = await this._fileStorageService$$.uploadSingleFile(
storyImage,
storyImagePath as string
);
this._sbS.sink = res.pipe(take(1), tap((url) => {
bot.imageField = url;
this.patchFunction(bot);
})).subscribe();
} else {
this.patchFunction
}
}

patchFunction(bot: Story) {
this._stories$$.update(bot).subscribe((botSaved) => {
if (botSaved) {
this._dialog.closeAll();
this._toast.doSimpleToast(this._translate.translate('TOAST.EDIT-BOT.SUCCESSFUL'));
this._toast.doSimpleToast(
this._translate.translate('TOAST.EDIT-BOT.SUCCESSFUL')
);
} else {
this._toast.doSimpleToast(this._translate.translate('TOAST.EDIT-BOT.FAIL'));
this._toast.doSimpleToast(
this._translate.translate('TOAST.EDIT-BOT.FAIL')
);
}
});
}
Expand All @@ -111,13 +137,13 @@ export class NewStoryService implements OnDestroy {
this._sbS.sink = this._stories$$.remove(story).subscribe({
error: () => {
this._toast.doSimpleToast(
this._translate.translate("TOAST.DELETE-BOT.FAIL")
);
},
complete: () => {
this._dialog.closeAll()
this._toast.doSimpleToast(
this._translate.translate("TOAST.DELETE-BOT.SUCCESSFUL")
this._translate.translate('TOAST.DELETE-BOT.FAIL')
);
},
complete: () => {
this._dialog.closeAll();
this._toast.doSimpleToast(
this._translate.translate('TOAST.DELETE-BOT.SUCCESSFUL')
);
},
});
Expand All @@ -127,18 +153,24 @@ export class NewStoryService implements OnDestroy {
const endBlock: EndStoryAnchorBlock = {
id: 'story-end-anchor',
type: StoryBlockTypes.EndStoryAnchorBlock,
position: {x: 200, y: 100},
position: { x: 200, y: 100 },
deleted: false,
blockTitle: 'End here',
blockIcon: ''
}
blockIcon: '',
};
this._blocksStore$$.createEndBlock(orgId, storyId, endBlock);
}

private _getOrgId$ = () => this._org$$.get().pipe(take(1), map(o => o.id));
private _getOrgId$ = () =>
this._org$$.get().pipe(
take(1),
map((o) => o.id)
);

generateName() {
const defaultName = uniqueNamesGenerator({ dictionaries: [adjectives, colors, animals] });
const defaultName = uniqueNamesGenerator({
dictionaries: [adjectives, colors, animals],
});
return defaultName;
}

Expand Down